Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SocketException 22 while setting ReceiveTimeout #27668

Closed
saurabh500 opened this issue Oct 18, 2018 · 6 comments
Closed

SocketException 22 while setting ReceiveTimeout #27668

saurabh500 opened this issue Oct 18, 2018 · 6 comments
Labels
area-System.Net.Sockets question Answer questions and provide assistance, not an issue with source code or documentation.
Milestone

Comments

@saurabh500
Copy link
Contributor

I got an error report from a product which uses SqlClient from .Net Core on macOS

The stack shows a SocketException with code 22 when SqlClient is setting the Socket timeout for receive to a particular value.
My ask is what does the error code 22 for SocketException mean and in what scenarios can it be thrown? And how should this error be handled in case of setting the timeout?
While reading the code, I found that the error could be EFBIG EINVALID from https://github.com/dotnet/corefx/blob/a10890f4ffe0fadf090c922578ba0e606ebdd16c/src/Native/Unix/System.Native/pal_errno.c#L67

EFBIG is thrown when the size of file being written is too large. However in this case, a timeout is being set on the socket, and no large amount of data is being written to the socket. ~~

"System.Net.Sockets.SocketException (22): Invalid argument\n   at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)"
System.Net.Sockets.dll!System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(System.Net.Sockets.SocketError error, string callerName) (Unknown Source:0)
System.Net.Sockets.dll!System.Net.Sockets.Socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel optionLevel, System.Net.Sockets.SocketOptionName optionName, int optionValue, bool silent) (Unknown Source:0)
System.Net.Sockets.dll!System.Net.Sockets.Socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel optionLevel, System.Net.Sockets.SocketOptionName optionName, int optionValue) (Unknown Source:0)
System.Net.Sockets.dll!System.Net.Sockets.Socket.ReceiveTimeout.set(int value) (Unknown Source:0)
System.Data.SqlClient.dll!System.Data.SqlClient.SNI.SNITCPHandle.Receive(out System.Data.SqlClient.SNI.SNIPacket packet, int timeoutInMilliseconds) (Unknown Source:0)
System.Data.SqlClient.dll!System.Data.SqlClient.SNI.TdsParserStateObjectManaged.ReadSyncOverAsync(int timeoutRemaining, out uint error) (Unknown Source:0)

cc @kburtram

EDIT: The error code is EINVAL.

@saurabh500
Copy link
Contributor Author

Related ADS issue microsoft/azuredatastudio#2914

@stephentoub
Copy link
Member

stephentoub commented Oct 18, 2018

22 is EINVAL, which maps to the "Invalid argument" the OS is giving you back (that error string comes from the OS). What is the "particular value" that's being set? Is it possible the socket has been closed?

@saurabh500
Copy link
Contributor Author

saurabh500 commented Oct 18, 2018

@stephentoub Thanks for the correction about error codes.

I chatted with @kburtram offline to understand this better.
The value is being set to 30 seconds. There is a possibility that there was a connection close before the timeout was set and that causes this exception. The connection was sitting idle for 5 minutes, and then an operations was performed on the connection.

I looked at the wireshark traces, it looks like, due to absence of Keep Alive settings on the Sockets, the server (or perhaps another network device) is dropping the idle connection. I know that the Keep Alive Option for Unix was added using dotnet/corefx#29963 and will be available in vNext.

However is there any setting (env var) that clients, on macOS and running on .Net Core 2.1, use, to set the Keep Alive for the .Net Core application?

@stephentoub
Copy link
Member

However is there any setting (env var) that clients, on macOS and running on .Net Core 2.1, use, to set the Keep Alive for the .Net Core application?

You're asking if the operating system exposes an environment variable or other global configuration that would enable keep-alive by default on all sockets? Not that I'm aware of, but maybe @wfurt or @tmds knows?

@wfurt
Copy link
Member

wfurt commented Oct 18, 2018

I don't think there is env variable. Most systems do have option to tune this globally.

sysctl -A | grep net.inet.tcp.*keep
et.inet.tcp.keepidle: 7200000
net.inet.tcp.keepintvl: 75000
net.inet.tcp.keepinit: 75000
net.inet.tcp.keepcnt: 8
net.inet.tcp.always_keepalive: 0

The close should pop up if we try to read from the socket.

@saurabh500
Copy link
Contributor Author

Thanks @wfurt

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 3.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Net.Sockets question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
None yet
Development

No branches or pull requests

4 participants