You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Updating to v10.0.1 causes OpenConnectionAsync to hang forever #6467
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.
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".
Okay, I was able to replicate it with the following code on 10.0.1:
classProgram{staticasyncTaskMain(string[]args){stringconnString="Host=localhost;Port=5432;Username=pgusr;Password=pwd;Database=db";NpgsqlConnectionStringBuilderconnStringBuilder=new(connString);awaitusing(NpgsqlConnectionconn=awaitNpgsqlDataSource.Create(connStringBuilder).OpenConnectionAsync()){NpgsqlCommandcommand=conn.CreateCommand();command.CommandText="SELECT * FROM table";NpgsqlDataReaderresults=awaitcommand.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.
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:
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.
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.
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.
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).
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.
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).
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)
Activity
vonzshik commentedon Mar 5, 2026
Could you try disabling gss encryption in connection string via
GSS Encryption Mode=Disable?doomchild commentedon Mar 5, 2026
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".
doomchild commentedon Mar 11, 2026
Okay, I was able to replicate it with the following code on 10.0.1:
Downgrading to 9.0.4 fixes it. Adding
;GSS Encryption Mode=Disableto 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.vonzshik commentedon Mar 11, 2026
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:
npgsql/src/Npgsql/Internal/NpgsqlConnector.cs
Lines 607 to 621 in aa86a1d
doomchild commentedon Mar 11, 2026
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:
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.
jmbryan4 commentedon Mar 30, 2026
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:
GSS Encryption Mode=Disabletests passI 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=Disableas a current workaround works for me.doomchild commentedon Mar 30, 2026
Setting
GSS Encryption Mode=Disableworks, but that seems like a breaking change that should be documented if it's intended.jmbryan4 commentedon Mar 30, 2026
@doomchild I agree.
vonzshik commentedon Mar 30, 2026
Not sure what breaking change you're talking about, given that we shouldn't hang even without
GSS Encryption Mode=Disablein the first place. The original stacktrace indicates that we're stuck somewhere inNegotiateAuthentication.GetOutgoingBlob, which is .NET runtime's method and it should be possible to reproduce without npgsql.jmbryan4 commentedon Mar 30, 2026
I think it is documented here. https://www.npgsql.org/doc/security.html#gss-session-encryption-gss-api
Is there an issue on https://github.com/dotnet/runtime for this?
This was the closest issue I could find dotnet/runtime#103536
doomchild commentedon Mar 30, 2026
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).
vonzshik commentedon Mar 30, 2026
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:
Does this code deadlock for anyone?
doomchild commentedon Mar 30, 2026
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
statusCodeis set toGenericFailure, anddataisnull.vonzshik commentedon Mar 31, 2026
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).
jmbryan4 commentedon Mar 31, 2026
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
vonzshik commentedon Mar 31, 2026
@jmbryan4 is it possible to get the stacktrace for when it hangs for you?
jmbryan4 commentedon Mar 31, 2026
I have not been able to reproduce today.
pholly commentedon Aug 4, 2026
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)