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
Exception storm due to GSS encryption in version 10 #6416
We experienced memory buildup and service crashes because of exception storms in our dockerized services. I know now this is due to a change in version 10 setting GSS session encryption ssl mode to 'prefer'. We found that in a high traffic environment, we got around 50k exceptions of 'Exception while performing GSS encryption' in the first minute, causing a memory buildup and cpu spikes. This happened in all of our services more or less (APIs and worker services). Disabling it via the connection string makes the problem disappear, however, I wouln't say 'the error is harmless', as stated in the docs.
Now 2 things could be going on here if there are no GSS libraries present in the container:
the error gets thrown internally repeatedly
we configured something wrong with npgsql and something gets reinitialized
We've been using npgsql (and with ef core) for years in prod without issues:
Could you please clarify what actual exception were you getting (Exception while performing GSS encryption should have an inner exception). As for the docs, it's saying that about Cannot load library libgssapi_krb5.so.2, which is indeed harmless.
Could you please clarify what actual exception were you getting (Exception while performing GSS encryption should have an inner exception). As for the docs, it's saying that about Cannot load library libgssapi_krb5.so.2, which is indeed harmless.
I used dotnet-trace inside a container to get exception traces, but those did not include the innerexceptions unfortunately. I have little doubt it's from unable finding the library, but if you really need it, then I will see if i can and spin up a test container with encrytion enabled in the connection string (it happened in a high traffic prod environment which i rather not do again).
The problem however was there were 56,040 of these exceptions:
ExceptionType="Npgsql.NpgsqlException" ExceptionMessage="Exception while performing GSS encryption".
With encryption off (or using version 10-rc2) there are none of those.
Cannot load library libgssapi_krb5.so.2 is an error from .NET runtime, which is logged to stderr. It's not actually thrown as an exception.
This error only happens once, as afterwards . NET caches the fact that this library is not available
And most importantly, this particular error happens only for the first access to GSS API, which incidentally happens outside of try/catch where we do throw Exception while performing GSS encryption. Not exactly true, see comment below.
Which is why I find it much more likely that instead you do have that library installed, your postgresql instance does support GSS authentication, and it just fails because postgresql doesn't authenticate the current user.
Now, we might be able to somewhat reduce memory/cpu impact for GSS encryption, but before investigating that I would prefer to understand completely what exactly you encountered.
A follow-up to my previous comment. I experimented a bit with docker and kerberos, and it seems like in case when the native library doesn't exist .NET runtime throws TypeInitializationException and we indeed wrap it in NpgsqlException. This means that instead of continuing with SSL/TLS we're recreating the connection from scratch (as at that point we do not know the state of connection and there is no fallback from GSS encryption once it starts). This can lead to about twice (though probably less as .NET can reuse memory) the amount of memory usage.
While I'll submit a pr to handle that more gracefully, it'll still well be great if you can indeed confirm that it was TypeInitializationException and not something else.
Sorry, I'll get back to you with a repro this tomorrow as I am away atm. But I did see a lot of TypeInitializationException
this is from the logs i still have: NpgsqlException 56,040 TypeInitializationException 14,010
Alright. We merged the fix to the main and 10.0.2 so it should be alleviated (at least to some extent). Would be great to get a confirmation after the release that it indeed fixes the issue.
Is there any estimate as to when 10.0.2 will be released?
We see the same errors in stderr and we think the exception might cause our healthchecks to fail.
@geferon what exact error are you talking about? For 10.0.2 we made it so we catch TypeInitializationException and instead of throwing another exception we retry without gss. This still means that TypeInitializationException is thrown by .NET, but should have helped quite a bit.
There's also a pr #6593 I submitted last week where in case TypeInitializationException is thrown, we'll set a flag that gss library isn't available, so any later request will just check that flag beforehand and not trigger an exception. But that's something we'll merge for 10.0.4.
I can confirm upgrading to version 10 causes excess exceptions. I upgraded on June 16th and finally dug into Application Insights logs after having intermittent performance issues. I discovered tons of postgresql connect timeout errors. E.g:
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)
and
Npgsql.NpgsqlException (0x80004005): Failed to connect to x.x.x.x:5432
---> System.Net.Sockets.SocketException (10061): No connection could be made because the target machine actively refused it.
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
at Npgsql.Internal.NpgsqlConnector.ConnectAsync(NpgsqlTimeout timeout, CancellationToken cancellationToken)
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(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
I can confirm this happened using Dapper and Entity Framework.
I added GSS Encryption Mode=Disable to the connection string and the connection timeout exceptions stopped.
Environment
Azure AppService (Windows App Service Plan I believe)
Azure Manged Postgresql Flexible Server
.NET 10
Npgsql 10.0.0
SSLMode=VerifyFull
16. I can confirm upgrading to version 10 causes excess exceptions. I upgraded on June 16th and finally dug into Application Insights logs after having intermittent performance issues. I discovered tons of postgresql connect timeout errors. E.g: 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)
and
```
Npgsql.NpgsqlException (0x80004005): Failed to connect to x.x.x.x:5432
---> System.Net.Sockets.SocketException (10061): No connection could be made because the target machine actively refused it.
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
at Npgsql.Internal.NpgsqlConnector.ConnectAsync(NpgsqlTimeout timeout, CancellationToken cancellationToken)
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(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
```
I can confirm this happened using Dapper and Entity Framework.
I added `GSS Encryption Mode=Disable` to the connection string and the connection timeout exceptions stopped.
### Environment
Azure AppService (Windows App Service Plan I believe)
Azure Manged Postgresql Flexible Server
.NET 10
Npgsql 10.0.0
SSLMode=VerifyFull
We can confirm the same issue with Npgsql 10.0.3, Azure Managed Postgresql Flexible Server on dotnet 10.
Activity
vonzshik commentedon Jan 21, 2026
Could you please clarify what actual exception were you getting (
Exception while performing GSS encryptionshould have an inner exception). As for the docs, it's saying that aboutCannot load library libgssapi_krb5.so.2, which is indeed harmless.elgerm commentedon Jan 22, 2026
I used dotnet-trace inside a container to get exception traces, but those did not include the innerexceptions unfortunately. I have little doubt it's from unable finding the library, but if you really need it, then I will see if i can and spin up a test container with encrytion enabled in the connection string (it happened in a high traffic prod environment which i rather not do again).
The problem however was there were 56,040 of these exceptions:
ExceptionType="Npgsql.NpgsqlException" ExceptionMessage="Exception while performing GSS encryption".
With encryption off (or using version 10-rc2) there are none of those.
vonzshik commentedon Jan 22, 2026
I do, because as we learned in #6360:
Cannot load library libgssapi_krb5.so.2is an error from .NET runtime, which is logged to stderr. It's not actually thrown as an exception.And most importantly, this particular error happens only for the first access to GSS API, which incidentally happens outside of try/catch where we do throwNot exactly true, see comment below.Exception while performing GSS encryption.Which is why I find it much more likely that instead you do have that library installed, your postgresql instance does support GSS authentication, and it just fails because postgresql doesn't authenticate the current user.
Now, we might be able to somewhat reduce memory/cpu impact for GSS encryption, but before investigating that I would prefer to understand completely what exactly you encountered.
vonzshik commentedon Jan 22, 2026
A follow-up to my previous comment. I experimented a bit with docker and kerberos, and it seems like in case when the native library doesn't exist .NET runtime throws
TypeInitializationExceptionand we indeed wrap it inNpgsqlException. This means that instead of continuing with SSL/TLS we're recreating the connection from scratch (as at that point we do not know the state of connection and there is no fallback from GSS encryption once it starts). This can lead to about twice (though probably less as .NET can reuse memory) the amount of memory usage.While I'll submit a pr to handle that more gracefully, it'll still well be great if you can indeed confirm that it was
TypeInitializationExceptionand not something else.Improve fallback handling for GSS session encryption when native libr…
Improve fallback handling for GSS session encryption when native libr…
elgerm commentedon Jan 23, 2026
Sorry, I'll get back to you with a repro this tomorrow as I am away atm. But I did see a lot of
TypeInitializationExceptionthis is from the logs i still have:
NpgsqlException 56,040 TypeInitializationException 14,010vonzshik commentedon Jan 23, 2026
Alright. We merged the fix to the main and 10.0.2 so it should be alleviated (at least to some extent). Would be great to get a confirmation after the release that it indeed fixes the issue.
JTeeuwissen commentedon Jan 26, 2026
Is there any estimate as to when 10.0.2 will be released?
We see the same errors in stderr and we think the exception might cause our healthchecks to fail.
kfuglsang commentedon Jan 28, 2026
Seeing this as well. Its causing my .net core web api deployed as an Azure Container App to fail startup.
NinoFloris commentedon Mar 14, 2026
10.0.2 has been released. Let us know whether the fix solves the issue, or at least mitigates it well enough.
geferon commentedon Jun 3, 2026
Error is still present in 10.0.2
vonzshik commentedon Jun 3, 2026
@geferon what exact error are you talking about? For 10.0.2 we made it so we catch
TypeInitializationExceptionand instead of throwing another exception we retry without gss. This still means thatTypeInitializationExceptionis thrown by .NET, but should have helped quite a bit.There's also a pr #6593 I submitted last week where in case
TypeInitializationExceptionis thrown, we'll set a flag that gss library isn't available, so any later request will just check that flag beforehand and not trigger an exception. But that's something we'll merge for 10.0.4.pholly commentedon Aug 4, 2026
I can confirm upgrading to version 10 causes excess exceptions. I upgraded on June 16th and finally dug into Application Insights logs after having intermittent performance issues. I discovered tons of postgresql connect timeout errors. E.g:
and
I can confirm this happened using Dapper and Entity Framework.
I added
GSS Encryption Mode=Disableto the connection string and the connection timeout exceptions stopped.Environment
Azure AppService (Windows App Service Plan I believe)
Azure Manged Postgresql Flexible Server
.NET 10
Npgsql 10.0.0
SSLMode=VerifyFull
henryalliander commentedon Nov 5, 2026
We can confirm the same issue with Npgsql 10.0.3, Azure Managed Postgresql Flexible Server on dotnet 10.
Thanks for pointing out this flag!