runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Setting keepalivetime and keepaliveinterval on Linux Sockets

Open keeratsingh opened this issue 8 years ago • 4 comments

This question is in regards to dotnet/runtime#25458 and as per diagnosis by @saurabh500 :

Windows impl of SqlClient sets some Keepalive values which Unix stack doesn’t SqlClient should set the keep alive values.

I was able to successfully set the SocketOption KeepAlive to Use Keep-Alives via SetSocketOption method _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); However I am having trouble setting keepalivetime and keepaliveinterval values.

It is my understanding that using Socket.IOControl method with IOControlCode.KeepAliveValues would be the correct method to set the above mentioned values. However, the comment in the IOControl method below seems the suggest that IOControl is used for Windows-specific IOCTL operations. https://github.com/dotnet/corefx/blob/master/src/System.Net.Sockets/src/System/Net/Sockets/Socket.cs#L1763

If that is indeed the case then:

  • Is it possible to set keepalivetime and keepaliveinterval values on Linux
  • If yes, then what would the correct format of struct to use on Linux

Regards, Keerat

keeratsingh avatar Mar 21 '18 21:03 keeratsingh

Duplicate of dotnet/runtime#24041

If it is 2.1 critical, please let me know.

karelz avatar Mar 22 '18 00:03 karelz

@karelz We were trying to get this fix into 2.1 until we encountered this issue. I am trying to get a confirmation if there is any such API for Linux at the moment that helps us to set keepalivetime and keepaliveinterval on Linux Sockets. I will let @saurabh500 answer the question regarding the 2.1 criticality.

Apologies in advance, however I can't seem to understand how this issue is a duplicate of dotnet/corefx#28343? [EDIT] it was supposed to be dotnet/runtime#24041 (EDIT by @karelz)

keeratsingh avatar Mar 22 '18 00:03 keeratsingh

Wrong bug number above - dotnet/runtime#24041 is the right one (I'll update above posts).

I don't think we have options to set it without the APIs - @geoffkizer @stephentoub @wfurt can you confirm?

karelz avatar Mar 22 '18 00:03 karelz

With SqlConnection, a connections can be made to servers which do not have any activity for extended periods of time, where a transaction request is sent and the client is waiting for server to finish a long running transaction and return after 15-20 minutes. As a result, the connection can go idle. This leads to network devices drop connections to Sql Server and is very prevalent in case of connections to Cloud Servers in the absence of a keep alive probe.

There is a workaround that can be recommended to the customers, i.e. to set the keep alive at the operating system level, however that can cause unexpected increase in network chatter from all TCP connections originating from the OS. Also Keep Alives cannot be controlled at the OS level in all environments where .Net Core applications can be deployed.

We do have cases like JDBC where Socket keep alives cannot be set at the Socket and hence Azure SQL DB asks Java customers to set it up in the OS. Recommended settings are below and the reference link is https://docs.microsoft.com/en-us/sql/connect/jdbc/connecting-to-an-azure-sql-database

Registry Setting Recommended Value
HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \ Tcpip \ Parameters \ KeepAliveTime 30000
HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \ Tcpip \ Parameters \ KeepAliveInterval 1000
HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \ Tcpip \ Parameters \ TcpMaxDataRetransmissions 10

The above is the justification for why SqlClient needs these settings.

About the need in netcore2.1

In terms of the reported issues, we only have had one customer issue so far because of the absence of setting Keep Alive, from Sql Ops Studio which is a tool using .Net Core to connect to Sql Servers. There are other tools being developed by Microsoft which can run workloads against SQL in the cloud (whether ours or someone elses) or on prem SQL Server using .Net Core on Linux and this problem can come up during long running transactions. We could recommend the OS level settings to customers facing these issues. However an API to allow socket level control would be way better solution. This gets out of control in environments where there is no access to OS settings. It may not always be possible for customers to control the Keep Alive in the Operating Systems in all environments where .Net Core can be deployed. Though I am leaning on the side of saying that this is not critical in netcore 2.1 based on the bug report, however on the other hand, since Sql Tools team has been releasing a bunch of products based on .Net core, I think it may be a matter of time before such issues start coming up.

Even if there are no APIs, is there any recommendations on how SqlClient could workaround this problem for its sockets, till a formal API is released, would be great.

saurabh500 avatar Mar 23 '18 18:03 saurabh500