Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support fifo pipes on unix #5318

Closed
Darksonn opened this issue Dec 27, 2022 · 5 comments
Closed

Support fifo pipes on unix #5318

Darksonn opened this issue Dec 27, 2022 · 5 comments
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. E-help-wanted Call for participation: Help is requested to fix this issue. M-net Module: tokio/net

Comments

@Darksonn
Copy link
Contributor

Currently, we provide the UnixStream type for reading from a unix domain socket on Linux, but Linux also provides pipes. Domain sockets and pipes are not the same (see here). Furthermore, opening a named pipe with UnixStream::connect will fail with an error. Currently the only way to asynchronously read from a pipe is to use AsyncFd.

I would like to see a type in tokio::net for use with fifo pipes.

@Darksonn Darksonn added E-help-wanted Call for participation: Help is requested to fix this issue. A-tokio Area: The main tokio crate M-net Module: tokio/net C-feature-request Category: A feature request. labels Dec 27, 2022
@satakuma
Copy link
Member

I would like to work on this.

@Noah-Kennedy
Copy link
Contributor

I'd be quite interested in this as well. @satakuma if you don't have the time for this LMK so I can jump in and help out.

@satakuma
Copy link
Member

satakuma commented Jan 2, 2023

I investigated this subject a bit and here are my thoughts.

Proposed solution

Expose a new module tokio::net::pipe with Sender and Receiver types.

These types will be created using either a fn new() -> (Sender, Receiver) function (anonymous pipe) or methods Sender::open(path) and Receiver::open(path) to open a named pipe (FIFO).

Sender type should implement AsyncWrite and corresponding methods writable, try_writeetc., Receiver similarly should implement AsyncRead etc.

Motivation

  • Mio already has relevant types which will facilitate the implementation.
  • Since on a Unix system anonymous pipes and named pipes (FIFOs) are only different in the way they are created (pipe() syscall vs. mkfifo + open), tokio will be able to handle both cases with one set of types.
  • Type system will prevent using pipes in the wrong direction, i.e. no writable method on the Receiver etc. Unlike bidirectional TcpStream and UnixStream, which can handle both reads and writes unless divided with the split method, a Unix pipe is unidirectional and therefore should have separate ends by default.

Non-blocking open

The Sender::open method using a non-blocking open syscall will succeed only if there already is an open reading end, and immediately fail otherwise. This means that there will be no way for a sender to asynchronously wait for a reader to connect besides blocking open syscall and spawn_blocking, which is not good.

On Linux, there is a workaround which opens the writing end with access to both read and write, but in general behavior of such operation is not defined. I guess the Sender type could have an additional method, for example Sender::open_dangling, which would allow opening a FIFO without waiting for a receiver, with a note warning about possible issues on other platforms.

Please let me know what you think @Darksonn and whether I should open a PR.

@Darksonn
Copy link
Contributor Author

Darksonn commented Jan 2, 2023

That sounds like a similar issue as the ERROR_PIPE_BUSY error on the Windows named pipe open method. For that method, we literally just recommend that you use a sleep loop to repeatedly try to open it.

We should simply provide the various methods that are possible, and then the user will just have to deal with the fact that Linux doesn't provide a good way to do this. It might be worth to include a method based on spawn_blocking, if it is the most reliable option.

The rest of your proposal sounds good.

@satakuma
Copy link
Member

Support for FIFO pipes was added in #5351, closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. E-help-wanted Call for participation: Help is requested to fix this issue. M-net Module: tokio/net
Projects
None yet
Development

No branches or pull requests

3 participants