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

Unable to use the Writer from a different thread. #29

Open
duggaraju opened this issue Dec 12, 2022 · 5 comments
Open

Unable to use the Writer from a different thread. #29

duggaraju opened this issue Dec 12, 2022 · 5 comments

Comments

@duggaraju
Copy link

I craeate a tokio pipe. in the thread where the pipe is created, I am reading from the read end till end of file. In another thread where I pass the writeer as a raw fd, I try to create a new PipeWrite (PipeWrite::from_raw_rd_checked(fd)). However that call fails because the creation of AsyncFd() seems to be failing since the file descriptor is already registerered?
Is there a way to safely separate the writing and reading across two different threads?

@duggaraju
Copy link
Author

Note that I can't pass the created PipeWriter directly to the other thread (the only shared state is the file descriptor number)

@NobodyXu
Copy link
Contributor

You need to either call <PipeWrite as IntoRawFd>::into_raw_fd, which will deregister the fd from tokio, or you need to dup the fd before recreating the PipeWrite.

@duggaraju
Copy link
Author

Thanks. The only issue i see is into_raw_fd() does mem::forget(). Is there a way to close the fd after i read everything from the pipe? Just as a safety to avoid leaking since the methods in PipeFd are not public

@NobodyXu
Copy link
Contributor

Thanks. The only issue i see is into_raw_fd() does mem::forget().

It forget the PipeFd, which closes the fd on drop.

Is there a way to close the fd after i read everything from the pipe?

If you created a PipeRead, then it will contains a PipeFd.

Just as a safety to avoid leaking since the methods in PipeFd are not public

If you are worried about leaking the fd when passing to a different thread, you can use std::os::unix::io::OwnedFd.

@yskszk63
Copy link
Owner

yskszk63 commented Jan 8, 2023

@duggaraju
(Sorry for the late response...)

In my environment, it seems to be possible to write across threads with PipeWrite::from_raw_rd_checked.
Here is the code I wrote to try it out.
https://github.com/yskszk63/tokio-pipe/blob/add-example/examples/inter-thread.rs

If there are any misunderstandings, I would appreciate it if you could point them out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants