Skip to content

ArgumentOutOfRangeException when verifying timeout range in SshCommand ctor #1700

@nikolamilekic

Description

@nikolamilekic

When constructing an SshCommand via Client.CreateCommand(string) an ArgumentOutOfRangeException is sometimes thrown with the following message:

The timeout must represent a value between -1 and Int32.MaxValue milliseconds, inclusive

It happens very often, but as mentioned, it DOES NOT happen all the time.

Here's the call stack:

Renci.SshNet.dll!Renci.SshNet.Common.TimeSpanExtensions.AsTimeout(System.TimeSpan timeSpan, string paramName) Line 23
    at /_/src/Renci.SshNet/Common/TimeSpanExtensions.cs(23)
Renci.SshNet.dll!Renci.SshNet.SshCommand.SshCommand(Renci.SshNet.ISession session, string commandText, System.Text.Encoding encoding) Line 224
    at /_/src/Renci.SshNet/SshCommand.cs(224)
Renci.SshNet.dll!Renci.SshNet.SshClient.CreateCommand(string commandText, System.Text.Encoding encoding) Line 204
    at /_/src/Renci.SshNet/SshClient.cs(204)
Renci.SshNet.dll!Renci.SshNet.SshClient.CreateCommand(string commandText) Line 196
    at /_/src/Renci.SshNet/SshClient.cs(196)

If I'm interpreting the code correctly, the only place where this is possible is in the CommandTimeout setter, which calls
value.EnsureValidTimeout(nameof(CommandTimeout)); that then calls TimeSpanExtensions.AsTimeout (I'm guessing EnsureValidTimeout along with the property setter is inlined, which is why it doesn't show up in the call stack).

To make this even weirder, it checks against InfiniteTimeSpan.TotalMilliseconds, which is a static readonly field and should be -1, falling within the range.

Version: 2025.0.0
.NET Framework: 4.8.9290

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions