aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2024-05-28 09:50:58 -0500
committerGitHub <noreply@github.com>2024-05-28 09:50:58 -0500
commitb55e5545586f60419aada5bafaf90c34d76cbf67 (patch)
treeccc62a58a3b94bda64d9f1ddfbb0666f1884bcda /src
parente2e8fb86f74fdbd1c39820a7ac3adc91d4a5c2a8 (diff)
parentbefac9735c93e4b5efb13472c8fdd96e4f234524 (diff)
downloadslskd-b55e5545586f60419aada5bafaf90c34d76cbf67.tar.gz
Merge pull request #1134 from slskd/sockets
Fix crash with "An unknown, invalid, or unsupported option or level was specified in a getsockopt or setsockopt call" on Windows 10 < 1709, Server 2012 R2, or earlier
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Application.cs56
-rw-r--r--src/slskd/Core/Options.cs9
2 files changed, 45 insertions, 20 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs
index 7f703eaa..0ad8c800 100644
--- a/src/slskd/Application.cs
+++ b/src/slskd/Application.cs
@@ -348,16 +348,9 @@ namespace slskd
inactivityTimeout: OptionsAtStartup.Soulseek.Connection.Timeout.Inactivity,
proxyOptions: proxyOptions);
- var configureKeepAlive = new Action<Socket>(socket =>
- {
- socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
- socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, OptionsAtStartup.Soulseek.Connection.Timeout.Inactivity / 1000);
- socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, OptionsAtStartup.Soulseek.Connection.Timeout.Inactivity / 1000);
- socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, 3);
- });
-
- var serverOptions = connectionOptions.With(configureSocketAction: configureKeepAlive);
- var distributedOptions = connectionOptions.With(configureSocketAction: configureKeepAlive);
+ // os-specific keepalive is configured for long-lived connections for the server and distributed parent/children
+ var serverOptions = connectionOptions.With(configureSocketAction: socket => ConfigureSocketKeepAlive(socket, OptionsAtStartup.Soulseek.Connection));
+ var distributedOptions = connectionOptions.With(configureSocketAction: socket => ConfigureSocketKeepAlive(socket, OptionsAtStartup.Soulseek.Connection));
var transferOptions = connectionOptions.With(
readBufferSize: OptionsAtStartup.Soulseek.Connection.Buffer.Transfer,
@@ -477,6 +470,37 @@ namespace slskd
return Task.CompletedTask;
}
+ private void ConfigureSocketKeepAlive(Socket socket, Options.SoulseekOptions.ConnectionOptions options)
+ {
+ /*
+ os-specific keepalive is configured for long-lived connections for the server and distributed parent/children
+ there may be legitimate reasons for these connections to go idle, and for that reason we can't use an inactivity watchdog
+ to determine when they are dead.
+
+ the intent, assuming the inactivity option is set to 15 seconds, is to send a keepalive probe after 15 seconds of inactivity,
+ and then again every 15 seconds until a total of 3 unanswered probes are sent, after which the socket will disconnect.
+ */
+ try
+ {
+ socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
+
+ if (OperatingSystem.IsWindows() && OptionsAtStartup.Flags.LegacyWindowsTcpKeepalive)
+ {
+ return;
+ }
+
+ // the following will throw an exception on operating systems prior to Windows 10, version 1709 (and whatever Server SKU that corresponds to)
+ // see: https://learn.microsoft.com/en-us/windows/win32/winsock/ipproto-tcp-socket-options
+ socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, 3); // TCP_KEEPCNT
+ socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, options.Timeout.Inactivity / 1000); // TCP_KEEPIDLE
+ socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, options.Timeout.Inactivity / 1000); // TCP_KEEPINTVL
+ }
+ catch (SocketException ex)
+ {
+ Log.Warning("Failed to configure connection keepalive settings: \"{Message}\". Performance is degraded. Set the configuration flag \"Legacy Windows TCP Keepalive\" to avoid this.", ex.Message);
+ }
+ }
+
// todo: consider moving this somewhere else; it's pretty long and complicated
private async Task EnqueueDownload(string username, IPEndPoint endpoint, string filename)
{
@@ -1290,16 +1314,8 @@ namespace slskd
inactivityTimeout: connection.Timeout.Inactivity,
proxyOptions: proxyPatch);
- var configureKeepAlive = new Action<Socket>(socket =>
- {
- socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
- socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, connection.Timeout.Inactivity / 1000);
- socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, connection.Timeout.Inactivity / 1000);
- socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, 3);
- });
-
- serverPatch = connectionPatch.With(configureSocketAction: configureKeepAlive);
- distributedPatch = connectionPatch.With(configureSocketAction: configureKeepAlive);
+ serverPatch = connectionPatch.With(configureSocketAction: socket => ConfigureSocketKeepAlive(socket, options: connection));
+ distributedPatch = connectionPatch.With(configureSocketAction: socket => ConfigureSocketKeepAlive(socket, options: connection));
transferPatch = connectionPatch.With(
readBufferSize: OptionsAtStartup.Soulseek.Connection.Buffer.Transfer,
diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs
index f2419c83..1b049ae1 100644
--- a/src/slskd/Core/Options.cs
+++ b/src/slskd/Core/Options.cs
@@ -425,6 +425,15 @@ namespace slskd
[Description("user-defined regular expressions are case sensitive")]
[RequiresRestart]
public bool CaseSensitiveRegEx { get; init; } = false;
+
+ /// <summary>
+ /// Gets a value indicating whether to use legacy TCP keepalive options, for Windows
+ /// versions prior to Windows 10, version 1709 (and associated Server SKUs).
+ /// </summary>
+ [Argument(default, "legacy-windows-tcp-keepalive")]
+ [EnvironmentVariable("LEGACY_WINDOWS_TCP_KEEPALIVE")]
+ [Description("use a legacy TCP keepalive strategy for older Windows versions")]
+ public bool LegacyWindowsTcpKeepalive { get; init; } = false;
}
/// <summary>