Skip to content

An idea for supporting cancellation in RandomAccess on Unix #96530

@alexrp

Description

@alexrp

cc @adamsitnik @stephentoub @tmds (?)

I would very much like to switch to RandomAccess in my terminal driver code. One hard blocker (aside from #58381) for doing so is that it does not currently support read cancellation on Unix. I know that #51985 is one way that could be solved, but frankly, that seems far off at the moment.

In my project, I implemented a type backed by a pipe + poll to solve this. It's used like so when reading. (Obviously, it can be implemented much more efficiently, with fewer allocations and such, but as I only have two instances in an app, that wasn't a big concern for me.)

The idea is very simple:

  • Set up an anonymous pipe.
  • Set up a handler so that, when the CancellationToken is canceled, a byte is written into the anonymous pipe.
  • When we go to read from the file handle, first do a poll(pipe, file).
    • If the poll indicates data is available in the file, just read as normal.
    • If the poll indicates data is available in the pipe, read it and discard it, and throw OperationCanceledException.

I would assume the idea applies equally well to writes.

The strategy is not entirely dissimilar from the one .NET already uses for async-over-sync cancellation on Windows.

Has something like this already been considered? If not, would it make sense to adopt some variation of it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions