npgsql: SocketException: Invalid argument was supplied on open connection

Steps to reproduce

Sorry, don’t know how to reproduce at this point.

The issue

Very occasionally (2 or 3 times per day on 10,000s connections per day) we get the following exception:

System.Net.Sockets.SocketException (0x80004005): An invalid argument was supplied
   at System.Net.Sockets.Socket.SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, Int32 optionValue, Boolean silent)
   at System.Net.Sockets.Socket.SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, Int32 optionValue)
   at Npgsql.NpgsqlConnector.SetSocketOptions(Socket socket)
   at Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
   at Npgsql.NpgsqlConnector.<RawOpen>d__148.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Npgsql.NpgsqlConnector.<Open>d__145.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Npgsql.ConnectorPool.<AllocateLong>d__24.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ValueTaskAwaiter`1.GetResult()
   at Npgsql.NpgsqlConnection.<Open>d__28.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Npgsql.NpgsqlConnection.Open()

Further technical details

Npgsql version: 3.2.7. PostgreSQL version: 9.6.1 Operating system: Win8

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 39 (20 by maintainers)

Most upvoted comments

Follow-up. Eventually, we gave up and ditched the PgBouncer and Pooling=false option for time being, and these errors do not appear anymore. We plan to replace PgBouncer with Odyssey in near future so I will provide another update after testing. Cheers!

As an “FYI” we’ve just finished upgrading to Server 2019 and this issue is still happening intermittantly. Judging by https://referencesource.microsoft.com/#System/net/System/Net/Sockets/Socket.cs,6469 I think it’s most likely the error is within Winsock32 and being bubbled out. It’s possible that more information might be available in the “GlobalLog” however we’ve not been able to get this into a reproducible state still.

BTW the first obvious thing to do is also to look at the PostgreSQL logs around the time the client exception occurs - PostgreSQL may say something interesting there.