Skip to content

Updating to v10.0.1 causes OpenConnectionAsync to hang forever #6467

Open

Description

@doomchild

Code that works just fine in v9.0.4 causes the application to hang indefinitely in v10.0.1.

Reproduction:

string connString = "Host=localhost;Port=5432;Username=user;Password=pwd;Database=database";

NpgsqlConnectionStringBuilder connectionStringBuilder = new(connString);
NpgsqlDataSource? dataSource = NpgsqlDataSource.Create(connectionStringBuilder);

await using (NpgsqlConnection pConnection = await dataSource!.OpenConnectionAsync()) ← permanent hang

When I pause in the debugger, I can see one thread with a pretty huge call stack that appears to be somewhere down in the security end of things. Downgrading to 9.0.4 fixes the problem.

Image

Activity

  1. vonzshik commented on Mar 5, 2026

    @vonzshik
    Contributor

    Could you try disabling gss encryption in connection string via GSS Encryption Mode=Disable?

  2. doomchild commented on Mar 5, 2026

    @doomchild
    Author

    After a reboot, I can't make it happen again. I'm going to close this and just file it under "one of those weird things that will never be perfectly explained".

  3. doomchild commented on Mar 11, 2026

    @doomchild
    Author

    Okay, I was able to replicate it with the following code on 10.0.1:

    class Program
    {
      static async Task Main(string[] args)
      {
        string connString = "Host=localhost;Port=5432;Username=pgusr;Password=pwd;Database=db";
        NpgsqlConnectionStringBuilder connStringBuilder = new(connString);
    
        await using (NpgsqlConnection conn = await NpgsqlDataSource.Create(connStringBuilder).OpenConnectionAsync())
        {
          NpgsqlCommand command = conn.CreateCommand();
    
          command.CommandText = "SELECT * FROM table";
          NpgsqlDataReader results = await command.ExecuteReaderAsync();
    
          Console.WriteLine(results.Rows);
        }
        
        Console.WriteLine("Hello, World!");
      }
    }

    Downgrading to 9.0.4 fixes it. Adding ;GSS Encryption Mode=Disable to the connection string fixes the problem. Is that required now? I can't find anything in the documentation saying that, but I could have missed it.

  4. reopened this on Mar 11, 2026
  5. vonzshik commented on Mar 11, 2026

    @vonzshik
    Contributor

    Well, npgsql 10 added support for gss encryption, which we attempt automatically (this is the same behavior psql has). The problem seems to be that we're getting stuck somewhere in .NET internals. You should be able to reproduce the issue without npgsql, just using the exact same code as we do:

    var targetName = $"{KerberosServiceName}/{Host}";
    var clientOptions = new NegotiateAuthenticationClientOptions { TargetName = targetName };
    NegotiateOptionsCallback?.Invoke(clientOptions);
    var authentication = new NegotiateAuthentication(clientOptions);
    try
    {
    byte[]? data;
    NegotiateAuthenticationStatusCode statusCode;
    try
    {
    data = authentication.GetOutgoingBlob(ReadOnlySpan<byte>.Empty, out statusCode)!;

  6. doomchild commented on Mar 11, 2026

    @doomchild
    Author

    Well, I'm eventually seeing a TimeoutException, which, based on the comments, makes me think it's happening somewhere in here.

    The stack trace looks like this:

    Unhandled exception. Npgsql.NpgsqlException (0x80004005): The operation has timed out
     ---> System.TimeoutException: The operation has timed out.
       at Npgsql.ThrowHelper.ThrowNpgsqlExceptionWithInnerTimeoutException(String message)
       at Npgsql.Util.NpgsqlTimeout.Check()
       at Npgsql.Util.NpgsqlTimeout.CheckAndGetTimeLeft()
       at Npgsql.Internal.NpgsqlConnector.ConnectAsync(NpgsqlTimeout timeout, CancellationToken cancellationToken)
       at Npgsql.Internal.NpgsqlConnector.RawOpen(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
       at Npgsql.Internal.NpgsqlConnector.<Open>g__OpenCore|209_0(NpgsqlConnector conn, String username, SslMode sslMode, GssEncryptionMode gssEncMode, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
       at Npgsql.Internal.NpgsqlConnector.<Open>g__OpenCore|209_0(NpgsqlConnector conn, String username, SslMode sslMode, GssEncryptionMode gssEncMode, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
       at Npgsql.Internal.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
       at Npgsql.PoolingDataSource.OpenNewConnector(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
       at Npgsql.PoolingDataSource.<Get>g__RentAsync|33_0(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
       at Npgsql.NpgsqlConnection.<Open>g__OpenAsync|42_0(Boolean async, CancellationToken cancellationToken)
       at Npgsql.NpgsqlDataSource.OpenConnectionAsync(CancellationToken cancellationToken)
       at Npgsql.NpgsqlDataSource.OpenConnectionAsync(CancellationToken cancellationToken)
       at NpgsqlHangTest.Program.Main(String[] args) in /Users/username/code/sandbox/NpgsqlHangTest/NpgsqlHangTest/Program.cs:line 12
       at NpgsqlHangTest.Program.<Main>(String[] args)
    

    I apologize for not attaching it earlier, but I had been killing the process when it was obviously hanging, so I didn't see it until a few minutes ago.

  7. jmbryan4 commented on Mar 30, 2026

    @jmbryan4

    I ran into this as well on macOS/arm64 with OrbStack + Testcontainers PostgreSql (postgres:18-alpine), upgrading to Npgsql 10.0.2 https://www.npgsql.org/doc/release-notes/10.0.html#gssapi-session-encryption

    Observed behavior:

    • Npgsql 10.0.2 with default connection settings: OpenAsync hangs indefinitely during integration test DB bootstrap
    • Npgsql 10.0.2 with GSS Encryption Mode=Disable tests pass
    • Npgsql 9.x: tests pass

    I also verified that plain TCP and psql connectivity to the mapped Postgres port worked while NpgsqlConnection.OpenAsync was hanging, so this does not look like a generic container/network readiness issue
    This makes me suspect the problem is in the GSS negotiation path. The fallback logic in NpgsqlConnector.OpenCore only retries without GSS when an exception is thrown

    GSS Encryption Mode=Disable as a current workaround works for me.

  8. doomchild commented on Mar 30, 2026

    @doomchild
    Author

    Setting GSS Encryption Mode=Disable works, but that seems like a breaking change that should be documented if it's intended.

  9. jmbryan4 commented on Mar 30, 2026

    @jmbryan4

    @doomchild I agree.

  10. vonzshik commented on Mar 30, 2026

    @vonzshik
    Contributor

    Not sure what breaking change you're talking about, given that we shouldn't hang even without GSS Encryption Mode=Disable in the first place. The original stacktrace indicates that we're stuck somewhere in NegotiateAuthentication.GetOutgoingBlob, which is .NET runtime's method and it should be possible to reproduce without npgsql.

  11. doomchild commented on Mar 30, 2026

    @doomchild
    Author

    The line "The default mode is Prefer, which allows GSS-API session encryption but does not require it" gives me the impression that it shouldn't hang if the encryption fails or is not supported for some reason. And while it might be a bug in the .NET runtime, it could also be a bug in how Npgsql is calling into that API (I honestly have no idea; I know nothing about the internals of Npgsql, which is why I reported this).

  12. vonzshik commented on Mar 30, 2026

    @vonzshik
    Contributor

    Well then, let's test it out, When I checked on both linux and windows, it was working fine. Essentially it all boils down to:

    var clientOptions = new NegotiateAuthenticationClientOptions { TargetName = "postgres/localhost" };
    var authentication = new NegotiateAuthentication(clientOptions);
    var data = authentication.GetOutgoingBlob(ReadOnlySpan<byte>.Empty, out var statusCode);

    Does this code deadlock for anyone?

  13. doomchild commented on Mar 30, 2026

    @doomchild
    Author

    I don't get any exception when I run that code. It sits for a good 30 seconds or so (I assume that's negotiation and retries), then returns without an exception. The statusCode is set to GenericFailure, and data is null.

  14. vonzshik commented on Mar 31, 2026

    @vonzshik
    Contributor

    Huh. Well, while it's definitely interesting that it doesn't hang, it also shouldn't take anywhere close to 30 seconds (we're speaking about a few ms at most, since there is no negotiation with anything at that point, just creating the first token to start negotiation process).
    I feel like this should be enough to raise an issue with the runtime folks given that something completely wrong is going on with the .NET API and kerberos (and to be honest, I have 0 clue at what even look for to debug such issues).

  15. jmbryan4 commented on Mar 31, 2026

    @jmbryan4

    I did not get a deadlock either. Completed in around 32ms with StatusCode: GenericFailure and Data: null

    macOS 26.4.0 (Arm64)
    .NET Version: 10.0.5
    GSS/Kerberos library: libgssapi_krb5.dylib

  16. vonzshik commented on Mar 31, 2026

    @vonzshik
    Contributor

    @jmbryan4 is it possible to get the stacktrace for when it hangs for you?

  17. jmbryan4 commented on Mar 31, 2026

    @jmbryan4

    I have not been able to reproduce today.

  18. pholly commented on Aug 4, 2026

    @pholly

    I also experienced this problem using Azure App Service and Azure Postgresql Flexible Server. I put my comment on another issue, but I think it should have gone here. #6416 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions