PowerShell/PowerShell · warning · TimeoutException

The request was canceled due to the configured OperationTime

Error message

The request was canceled due to the configured OperationTimeout of {perReadTimeout.TotalSeconds} seconds elapsing

What it means

Thrown by CopyToAsync during response-body streaming when a read within the copy loop exceeds perReadTimeout and the linked CancellationTokenSource cancels. Equivalent to the per-read timeout but on the streaming-copy path; cts.IsCancellationRequested distinguishes the timeout from an external cancellation.

Source

Thrown at src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs:325

                        cts.Dispose();
                        cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
                    }

                    cts.CancelAfter(perReadTimeout);
                    int bytesRead = await source.ReadAsync(buffer, cts.Token).ConfigureAwait(false);
                    if (bytesRead == 0)
                    {
                        break;
                    }

                    await destination.WriteAsync(buffer.AsMemory(0, bytesRead), cancellationToken).ConfigureAwait(false);
                }
            }
            catch (TaskCanceledException ex)
            {
                if (cts.IsCancellationRequested)
                {
                    throw new TimeoutException($"The request was canceled due to the configured OperationTimeout of {perReadTimeout.TotalSeconds} seconds elapsing", ex);
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                cts.Dispose();
                ArrayPool<byte>.Shared.Return(buffer);
            }
        }
    }

    internal static class StreamHelper
    {
        #region Constants

View on GitHub (pinned to 3ff3c711bf)

Solutions

  1. Raise the per-read timeout for large/slow downloads
  2. Stream to a resumable destination (file with Range requests) so a timeout can be retried from the last offset
  3. Retry the whole request with backoff if the stall is transient
  4. Move the download off the interactive request path to a background job

Example fix

# before
Invoke-WebRequest -Uri $u -OutFile big.zip -TimeoutSec 10

# after
Invoke-WebRequest -Uri $u -OutFile big.zip -OperationTimeout (New-TimeSpan -Minutes 5)
Defensive patterns

Strategy: retry

Validate before calling

$null

Type guard

null

Try / catch

try { Invoke-WebRequest -Uri $u -OutFile $f -OperationTimeout $ts } catch [System.TimeoutException] { Write-Warning 'Stream copy timed out; resuming.'; <# resume from offset or retry #> }

Prevention

When it happens

Trigger: Streaming a response body to disk/pipe where an individual read in the copy loop stalls longer than perReadTimeout, while the overall operation is otherwise progressing.

Common situations: Slow/metered upstream during large file downloads; intermediate proxy buffering then pausing; high-latency or lossy links causing read stalls.

Understand the failure class

Related errors


AI-assisted analysis of PowerShell/PowerShell@3ff3c711bf (2026-08-13). Data as JSON: /api/errors/fcdbc34fc181c35d. Report an issue: GitHub.