PowerShell Universal Bug Report: `ArgumentOutOfRangeException` in `UDRunspaceFactory.GetRunspace()` when `SessionTimeout = 0`
Product: PowerShell Universal Version: 5.6.12
Summary
We experienced intermittent API failures in PowerShell Universal where API endpoints would return HTTP 400 and log a System.ArgumentOutOfRangeException during runspace creation. The exception is thrown inside PowerShell Universal (UDRunspaceFactory.GetRunspace()), before any endpoint script code is executed.
The issue was resolved by changing SessionTimeout from 0 (infinite) to a positive integer (e.g., 60 minutes).
Symptoms
System.ArgumentOutOfRangeException: The added or subtracted value results in an un-representable DateTime. (Parameter 'value')Stack Trace (excerpt)
System.ArgumentOutOfRangeException: The added or subtracted value results in an un-representable DateTime. (Parameter 'value') at System.DateTime.ThrowDateArithmetic(Int32 param) at System.DateTime.AddMinutes(Double value) at UniversalDashboard.Services.UDRunspaceFactory.GetRunspace() in ...\UDRunspaceFactory.cs:line 37 at UniversalAutomation.Host.PowerShellApiService.ExecuteRequest(ApiRequest request) in ...\ApiService.cs:line 171
Configuration that triggers the issue
In .universal/settings.ps1:
$Parameters = @{
# ...
SessionTimeout = 0
# ...
}
Set-PSUSetting @Parameters
Expected behavior
SessionTimeout = 0 (“no limit / infinite”) should not cause API failures.ArgumentOutOfRangeException.Actual behavior
SessionTimeout = 0, PowerShell Universal intermittently fails to create a runspace.DateTime.AddMinutes(...) (seen in stack trace) and triggers a DateTime overflow (ArgumentOutOfRangeException).Workaround / Fix
Changing SessionTimeout to a positive value resolves the issue:
$Parameters = @{
# ...
SessionTimeout = 60
# ...
}
Set-PSUSetting @Parameters
After this change:
ArgumentOutOfRangeException stopped occurring.Why this appears to be a platform bug
UDRunspaceFactory.GetRunspace()), not to endpoint scripts.SessionTimeout=0 is considered a special value, the platform should handle it gracefully and not crash during runspace creation.Request
Please:
SessionTimeout = 0 is a supported configuration.DateTime.AddMinutes overflow in UDRunspaceFactory.GetRunspace() when SessionTimeout=0.0 should not be used.