Session Timeouts
Session timeouts control how long SSH operations wait before giving up. This is different from keepalives - timeouts prevent operations from hanging indefinitely, while keepalives prevent idle connections from being closed.
Understanding Timeouts
By default, libssh2 has no timeout - operations will wait indefinitely until they complete or fail. Setting a timeout ensures operations don't hang forever.
Socket Timeout vs Session Timeout
NullOpsDevs.LibSsh provides two types of timeouts that serve different purposes:
Socket Timeout
Socket timeout controls the low-level TCP network operations (send and receive). This is configured during connection:
Key characteristics:
Set during
Connect()orConnectAsync()Affects underlying TCP socket send and receive operations
Default: No timeout (infinite) if not specified
Cannot be changed after connection is established
Controls how long the socket waits for network data
Session Timeout
Session timeout controls SSH protocol operations (commands, file transfers, authentication). This is configured after connection:
Key characteristics:
Set after
Connect()usingSetSessionTimeout()Affects SSH protocol operations (commands, authentication, file transfers)
Default: No timeout (infinite)
Can be changed at any time after connection
Controls how long SSH operations wait before giving up
Comparison Table
Feature | Socket Timeout | Session Timeout |
|---|---|---|
Layer | TCP/Network | SSH Protocol |
When set | During | After |
Controls | TCP send/receive operations | SSH operations (commands, transfers) |
Default | No timeout (infinite) | No timeout (infinite) |
Can change | No (fixed at connection) | Yes (anytime) |
Configuration |
|
|
Use case | Prevent network I/O hangs | Prevent SSH operation hangs |
When to use | Only when facing network timeout issues | For general operation timeout control |
Using Both Together
For comprehensive timeout protection in problematic network environments, use both:
When to use each:
Socket timeout: Only when you are facing network-level timeout problems (e.g., extremely slow networks, network infrastructure that causes hangs at the TCP level)
Session timeout: Use for general operation timeout control (e.g., long-running commands, large file transfers)
Both: Only in production environments with known network issues
Setting a Timeout
Configure the timeout after connecting but before or after authenticating:
Disabling Timeouts
To restore the default behavior (wait indefinitely):
When to Use Timeouts
Use Timeouts When:
Executing commands that might hang
session.SetSessionTimeout(TimeSpan.FromMinutes(5)); var result = session.ExecuteCommand("./unpredictable-script.sh");Transferring large files
session.SetSessionTimeout(TimeSpan.FromMinutes(30)); using var stream = File.OpenRead("large-file.zip"); session.WriteFile("/remote/path/large-file.zip", stream);In production environments
// Prevent operations from hanging indefinitely session.SetSessionTimeout(TimeSpan.FromMinutes(10));When connecting to unreliable servers
session.Connect("unreliable-server.com", 22); session.SetSessionTimeout(TimeSpan.FromSeconds(30));
Don't Use Timeouts When:
Running truly long operations - Use cancellation tokens instead
In development - Let operations complete naturally to see actual behavior
Operations are already bounded - If the command itself has a timeout
Timeout vs. Keepalive
These are different mechanisms with different purposes:
Feature | Session Timeout | Keepalive |
|---|---|---|
Purpose | Prevent operations from hanging | Prevent idle disconnection |
When active | During active operations | During idle periods |
What it does | Aborts operation if too slow | Sends periodic messages |
Error on failure |
| Connection drops |
Configuration |
|
|
Using Both Together
For robust connection management, use both timeouts and keepalives:
Handling Timeout Errors
When an operation times out, you'll receive an SshException with SshError.Timeout:
Timeout Scope
The timeout applies to blocking libssh2 operations, including:
Command execution (
ExecuteCommand)File transfers (
ReadFile,WriteFile)Authentication (
Authenticate)Keepalive messages (
SendKeepAlive)
Best Practices
Set reasonable timeouts:
Too short: Operations fail unnecessarily
Too long: Hung operations waste resources
Typical: 1-5 minutes for most operations
Adjust for operation type:
// Short timeout for quick commands session.SetSessionTimeout(TimeSpan.FromSeconds(30)); var result = session.ExecuteCommand("whoami"); // Longer timeout for file transfers session.SetSessionTimeout(TimeSpan.FromMinutes(30)); session.WriteFile("/path/large.zip", stream);Consider network conditions:
Slower networks need longer timeouts
High-latency connections need more time
Unreliable networks may need retries
Log timeout events:
try { var result = session.ExecuteCommand(command); } catch (SshException ex) when (ex.Error == SshError.Timeout) { logger.LogWarning($"Command timed out: {command}"); throw; }Use cancellation tokens for long operations:
// Prefer cancellation tokens for user-initiated cancellation using var cts = new CancellationTokenSource(); var result = await session.ExecuteCommandAsync(command, cancellationToken: cts.Token);
Example: Configuring for Different Environments
See Also
SshSession.SetSessionTimeout()(SshSession.cs:276) - Set operation timeoutSshSession.DisableSessionTimeout()(SshSession.cs:259) - Disable timeoutKeeping Connection Alive - Prevent idle disconnections
Error Handling - Handling timeout errors