{"record":{"id":"9ac7d59b04fc2573","repo":"PowerShell/PowerShell","slug":"the-request-was-canceled-due-to-the-configured-ope","errorCode":null,"errorMessage":"The request was canceled due to the configured OperationTimeout of {readTimeout.TotalSeconds} seconds elapsing","messagePattern":"The request was canceled due to the configured OperationTimeout of (.+?) seconds elapsing","errorType":"exception","errorClass":"TimeoutException","httpStatus":null,"severity":"warning","filePath":"src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs","lineNumber":281,"sourceCode":"    {\n        internal static async Task<int> ReadAsync(this Stream stream, Memory<byte> buffer, TimeSpan readTimeout, CancellationToken cancellationToken)\n        {\n            if (readTimeout == Timeout.InfiniteTimeSpan)\n            {\n                return await stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);\n            }\n\n            using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);\n            try\n            {\n                cts.CancelAfter(readTimeout);\n                return await stream.ReadAsync(buffer, cts.Token).ConfigureAwait(false);\n            }\n            catch (TaskCanceledException ex)\n            {\n                if (cts.IsCancellationRequested)\n                {\n                    throw new TimeoutException($\"The request was canceled due to the configured OperationTimeout of {readTimeout.TotalSeconds} seconds elapsing\", ex);\n                }\n                else\n                {\n                    throw;\n                }\n            }\n        }\n\n        internal static async Task CopyToAsync(this Stream source, Stream destination, TimeSpan perReadTimeout, CancellationToken cancellationToken)\n        {\n            if (perReadTimeout == Timeout.InfiniteTimeSpan)\n            {\n                // No timeout - use fast path\n                await source.CopyToAsync(destination, cancellationToken).ConfigureAwait(false);\n                return;\n            }\n\n            byte[] buffer = ArrayPool<byte>.Shared.Rent(StreamHelper.ChunkSize);","sourceCodeStart":263,"sourceCodeEnd":299,"githubUrl":"https://github.com/PowerShell/PowerShell/blob/3ff3c711bf54a18f8440f3c5190c3ac91cdc5852/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs#L263-L299","documentation":"Thrown by the web cmdlet stream reader when a single ReadAsync call does not complete within the configured OperationTimeout. A linked CancellationTokenSource cancels after readTimeout; if the cancellation fires (cts.IsCancellationRequested), the TaskCanceledException is converted to a TimeoutException with the elapsed seconds in the message.","triggerScenarios":"Invoke-WebRequest/Invoke-RestMethod against a slow server that stalls mid-response (the connection opens but bytes stop arriving), with a finite -OperationTimeout or -TimeoutSec.","commonSituations":"Overloaded or hung upstream servers; network brownouts; large response bodies where the server trickles data slower than the per-read timeout; reverse proxies/proxies that hold the connection without streaming.","solutions":["Increase -OperationTimeout / -TimeoutSec to tolerate slow streams","Investigate the upstream server's response streaming (chunked encoding, buffering) and fix the stall","Add retry with exponential backoff around the call for transient stalls","If streaming huge payloads, switch to a streaming download approach that tolerates slow reads"],"exampleFix":"# before\nInvoke-WebRequest -Uri $u -TimeoutSec 5\n\n# after\nInvoke-WebRequest -Uri $u -OperationTimeout (New-TimeSpan -Minutes 2)","handlingStrategy":"retry","validationCode":"$null","typeGuard":"null","tryCatchPattern":"try { Invoke-WebRequest -Uri $u -OperationTimeout $ts } catch [System.TimeoutException] { Write-Warning 'Read timed out; retrying.'; <# retry w/ backoff #> }","preventionTips":["Size -OperationTimeout to the slowest expected server response","Investigate streaming stalls on the upstream; retry transient timeouts with backoff"],"tags":["invoke-webrequest","timeout","streaming","http"],"backgroundTag":null,"analyzedSha":"3ff3c711bf54a18f8440f3c5190c3ac91cdc5852","analyzedAt":"2026-08-13T10:39:10.759Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}