dotnet / dotnet/runtime

Support cancellation on non-blocking fds in FileStream and RandomAccess on Unix

Open
#127,594 4 comments 0 reactions 0 assignees View on GitHub
area-System.IO
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

https://github.com/dotnet/runtime/pull/125512 added support to FileStream/RandomAccess for non-seekable files.
It does not include cancellation support on Unix.

For example, the following program hangs instead of throwing when the cancellation token times out:

```cs
using Microsoft.Win32.SafeHandles;
using System.Diagnostics;

// Launch an app that will never complete its output.
SafeFileHandle.CreateAnonymousPipe(out SafeFileHandle outputReadPipe, out SafeFileHandle outputWritePipe);
SafeFileHandle.CreateAnonymousPipe(out SafeFileHandle inputReadPipe, out SafeFileHandle inputWritePipe);
ProcessStartInfo producer = new("cat")
{
StandardInputHandle = inputReadPipe,
StandardOutputHandle = outputWritePipe
};
Process.Start(producer);
inputReadPipe.Dispose();
outputWritePipe.Dispose();

// Read the output with a timeout.
var reader = new StreamReader(new FileStream(outputReadPipe, FileAccess.Read));
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1));
await reader.ReadToEndAsync(cts.Token);
```

cc @adamsitnik @stephentoub @jkotas

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.