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

Change wait_with_output to borrow instead of taking ownership #127862

Closed

Conversation

Binlogo
Copy link
Contributor

@Binlogo Binlogo commented Jul 17, 2024

Context & Motivation

To implement the timeout feature for the xshell crate, as discussed in this pull request, the child process will use wait_with_output until the timeout occurs, at which point it will be terminated.

        let out_res = if let Some(timeout) = self.data.timeout {
            let (tx, rx) = mpsc::channel();
            let handle = thread::spawn(move || {
                let output = child.wait_with_output();
                let _ = tx.send(output);
            });
            handle.join().unwrap();
            match rx.recv_timeout(timeout) {
                Ok(output) => output,
                Err(err) => {
                    // FIXME: Kill the child, borrow of moved value: `child`
                    // child.kill();
                    return Err(Error::new_timeout(self, err));
                }
            }
        } else {
            child.wait_with_output()
        };

Since .wait_with_output need the child's ownership, when spawned, I can't use it to call .kill anymore. I tried Arc way, but it still failed:

error[E0507]: cannot move out of dereference of MutexGuard<'_, Child>

It appears that both wait and try_wait only require borrowing &mut self. We could consider refactoring wait_with_output to require borrowing instead of taking ownership.

Breaking change?

This is my first time submitting a PR to the Rust standard library. It appears this change could be breaking. I'm unsure if we can refactor this method in this manner. If any additional actions are required, I will follow through.

r? @matklad

@rustbot
Copy link
Collaborator

rustbot commented Jul 17, 2024

Failed to set assignee to matklad: invalid assignee

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

@rustbot
Copy link
Collaborator

rustbot commented Jul 17, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @matklad (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 17, 2024
@matklad
Copy link
Member

matklad commented Jul 17, 2024

std::Process is a stable API, so we can't change it at all.

What we could potentially do is to add a new API which allows this use-case (concurrently reading process output and killing it), but that needs to go through the standard process with API change proposal:

https://std-dev-guide.rust-lang.org/development/feature-lifecycle.html

I personally don't know what would be the right shape for a new API here, or whether the new API is warranted (the user-code can "just" re-implement its own wait_with_output, though that requires re-impelmenting the read2 utility) .

@rust-log-analyzer

This comment has been minimized.

@Binlogo
Copy link
Contributor Author

Binlogo commented Jul 17, 2024

std::Process is a stable API, so we can't change it at all.

What we could potentially do is to add a new API which allows this use-case (concurrently reading process output and killing it), but that needs to go through the standard process with API change proposal:

https://std-dev-guide.rust-lang.org/development/feature-lifecycle.html

I personally don't know what would be the right shape for a new API here, or whether the new API is warranted (the user-code can "just" re-implement its own wait_with_output, though that requires re-impelmenting the read2 utility) .

Oh, making a change like this seems challenging. Thank you for your advice and guidance. Proposing a new API appears to be a lengthy process, yet I view it as an opportunity to learn and grow within the Rust community. I'll take some time to delve into it.

@Binlogo Binlogo force-pushed the wait_with_output-own-to-borrow branch from 69bff3a to 862eb3f Compare July 17, 2024 13:10
@Binlogo Binlogo force-pushed the wait_with_output-own-to-borrow branch from 862eb3f to b41677b Compare July 17, 2024 13:18
@Binlogo Binlogo closed this Jul 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants