Skip to content

aalmada/Stationary / .agents/skills/dotnet-pipelines

dotnet-pipelines

Verified

Design, implement, debug, test, and review high-throughput byte-stream processing with System.IO.Pipelines. USE FOR: Pipe, PipeReader, PipeWriter, ReadResult, FlushResult, ReadOnlySequence, SequenceReader, buffer lifecycle, AdvanceTo, GetMemory, Advance, FlushAsync, stream and socket adapters, framing protocols, incremental parsing, backpressure, cancellation, and Pipes-to-Channels handoff. DO NOT USE FOR: item-level producer-consumer queues, durable/distributed messaging, ordinary small file I/O with no measured bottleneck, generic Stream APIs with no buffer-management concern, or ASP.NET middleware pipelines unrelated to System.IO.Pipelines.

references/writer-flow-control-and-completion.md

View on GitHub
# Writer Flow Control And Completion

## Writer Loop

Reserve memory, write bytes, commit the written count, then flush. `FlushAsync` publishes bytes and applies the pipe's pause/resume thresholds.

```csharp
try
{
    while (true)
    {
        Memory<byte> destination = writer.GetMemory(minimumBufferSize);
        int bytesRead = await source.ReadAsync(destination, cancellationToken);
        if (bytesRead == 0)
        {
            break;
        }

        writer.Advance(bytesRead);
        FlushResult flush = await writer.FlushAsync(cancellationToken);
        if (flush.IsCompleted || flush.IsCanceled)
        {
            break;
        }
    }
}
finally
{
    await writer.CompleteAsync();
}
```

Do not call `Advance` with bytes not initialized by the producer. Do not retain writer-provided memory after a flush or completion.

## Flow Control

| Setting | Effect |
| --- | --- |
| `PauseWriterThreshold` | Buffered-byte level where `FlushAsync` starts waiting |
| `ResumeWriterThreshold` | Buffered-byte level where a blocked flush may continue |
| `MinimumSegmentSize` | Minimum rented buffer size for writer requests |
| `UseSynchronizationContext` | Whether continuations capture the current synchronization context |

- Tune thresholds only after measurement. They constrain buffered bytes but do not replace protocol message limits.
- Await every `FlushAsync`; ignoring its pending result defeats flow control and hides completion/cancellation.
- Keep pause thresholds comfortably above typical frames but bounded by a real memory budget.

## Completion And Cancellation

- The component owning the full write loop calls `CompleteAsync`, including an exception when it owns a terminal fault.
- Completing the writer tells the reader no more bytes will arrive; readers must still drain the existing buffer.
- `ReadAsync`/`FlushAsync` accept cancellation tokens. `CancelPendingRead` and `CancelPendingFlush` produce a result with `IsCanceled` instead of throwing and are useful for a controlled loop exit.
- Stop writing when `FlushResult.IsCompleted` is true: the reader no longer accepts data.

## Sources

- [System.IO.Pipelines: Pipe](https://learn.microsoft.com/dotnet/standard/io/pipelines#pipe)
- [PipeOptions](https://learn.microsoft.com/dotnet/api/system.io.pipelines.pipeoptions)
- [PipeWriter](https://learn.microsoft.com/dotnet/api/system.io.pipelines.pipewriter)
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 322kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 322kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 322kUpdated Claude CodeCodexVerifiedInstall
skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.Stars: 198kUpdated Claude CodeCodexVerifiedInstall