diff options
| author | JP Dillingham <jp@dillingham.me> | 2024-05-28 09:50:58 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-28 09:50:58 -0500 |
| commit | b55e5545586f60419aada5bafaf90c34d76cbf67 (patch) | |
| tree | ccc62a58a3b94bda64d9f1ddfbb0666f1884bcda | |
| parent | e2e8fb86f74fdbd1c39820a7ac3adc91d4a5c2a8 (diff) | |
| parent | befac9735c93e4b5efb13472c8fdd96e4f234524 (diff) | |
| download | slskd-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
| -rw-r--r-- | config/slskd.example.yml | 1 | ||||
| -rw-r--r-- | docs/config.md | 30 | ||||
| -rw-r--r-- | src/slskd/Application.cs | 56 | ||||
| -rw-r--r-- | src/slskd/Core/Options.cs | 9 |
4 files changed, 62 insertions, 34 deletions
diff --git a/config/slskd.example.yml b/config/slskd.example.yml index f1e1b4ab..2552e081 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -14,6 +14,7 @@ # experimental: false # volatile: false # case_sensitive_reg_ex: false +# legacy_windows_tcp_keepalive: false # relay: # enabled: false # mode: controller # controller (default), agent, or debug (for local development) diff --git a/docs/config.md b/docs/config.md index 5c4eb2bb..87fb916b 100644 --- a/docs/config.md +++ b/docs/config.md @@ -1072,20 +1072,21 @@ feature: Several additional feature flags are provided to change the application's runtime behavior, which is helpful during development. Available feature flags are: -| Flag | Environment Variable | Description | -| ------------------------ | ---------------------------- | ---------------------------------------------------------------- | -| `-d\|--debug` | `SLSKD_DEBUG` | Run the application in debug mode. Produces verbose log output. | -| `--experimental` | `SLSKD_EXPERIMENTAL` | Run the application in experimental mode. YMMV. | -| `-n\|--no-logo` | `SLSKD_NO_LOGO` | Don't show the application logo on startup | -| `-x\|--no-start` | `SLSKD_NO_START` | Bootstrap the application, but don't start | -| `--no-config-watch` | `SLSKD_NO_CONFIG_WATCH` | Don't watch the configuration file for changes | -| `--no-connect` | `SLSKD_NO_CONNECT` | Start the application, but don't connect to the server | -| `--no-share-scan` | `SLSKD_NO_SHARE_SCAN` | Don't perform a scan of shared directories on startup | -| `--force-share-scan` | `SLSKD_FORCE_SHARE_SCAN` | Force a scan of shared directories on startup | -| `--no-version-check` | `SLSKD_NO_VERSION_CHECK` | Don't perform a version check on startup | -| `--log-sql` | `SLSKD_LOG_SQL` | Log SQL queries generated by Entity Framework | -| `--volatile` | `SLSKD_VOLATILE` | Use volatile data storage (all data will be lost at shutdown) | -| `--case-sensitive-regex` | `SLSKD_CASE_SENSITIVE_REGEX` | User-defined regular expressions are case sensitive | +| Flag | Environment Variable | Description | +| -------------------------------- | ------------------------------------ | ---------------------------------------------------------------- | +| `-d\|--debug` | `SLSKD_DEBUG` | Run the application in debug mode. Produces verbose log output. | +| `--experimental` | `SLSKD_EXPERIMENTAL` | Run the application in experimental mode. YMMV. | +| `-n\|--no-logo` | `SLSKD_NO_LOGO` | Don't show the application logo on startup | +| `-x\|--no-start` | `SLSKD_NO_START` | Bootstrap the application, but don't start | +| `--no-config-watch` | `SLSKD_NO_CONFIG_WATCH` | Don't watch the configuration file for changes | +| `--no-connect` | `SLSKD_NO_CONNECT` | Start the application, but don't connect to the server | +| `--no-share-scan` | `SLSKD_NO_SHARE_SCAN` | Don't perform a scan of shared directories on startup | +| `--force-share-scan` | `SLSKD_FORCE_SHARE_SCAN` | Force a scan of shared directories on startup | +| `--no-version-check` | `SLSKD_NO_VERSION_CHECK` | Don't perform a version check on startup | +| `--log-sql` | `SLSKD_LOG_SQL` | Log SQL queries generated by Entity Framework | +| `--volatile` | `SLSKD_VOLATILE` | Use volatile data storage (all data will be lost at shutdown) | +| `--case-sensitive-regex` | `SLSKD_CASE_SENSITIVE_REGEX` | User-defined regular expressions are case sensitive | +| `--legacy-windows-tcp-keepalive` | `SLSKD_LEGACY_WINDOWS_TCP_KEEPALIVE` | Use a legacy TCP keepalive strategy for older Windows versions | #### **YAML** ```yaml @@ -1102,6 +1103,7 @@ flags: experimental: false volatile: false case_sensitive_reg_ex: false + legacy_windows_tcp_keepalive: false ``` # Commands 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> |
