Skip to content

Replace mpsc::sync_channel with mpsc::channel, to avoid timing bugs - #5197

Closed
fennewald wants to merge 1 commit into
nushell:mainfrom
fennewald:external_command_channel
Closed

fennewald wants to merge 1 commit into
nushell:mainfrom
fennewald:external_command_channel

Conversation

@fennewald

Copy link
Copy Markdown
Contributor

Description

Fixes #5156.

External commands have their output sent over a mpsc::sync_channel to a consumer. sync_channel has a static maximum queue size, and calls to a full channel block.

Seperately, within crate/nu-command/src/system/complete.rs, The entire stdout output is consumed, and then the entire stderr output is consumed.

The bug in #5156 arises when stderr's entire output does not fit into the stderr mpsc channel queue. The external command reads all of stdout before beginning to read stderr, but stdout will not close until the child process closes. The child process will not close until stderr can be completely written to the channel. Stderr cannot be written to the channel unless there is room in the channel's queue. The only way to have room in the channels queue is to consume the stderr stream, which loops all the way back around, and tada, deadlock.

Cheap fix is just to allow the queue to grow to arbitrary sizes. The other option to fix this is to actually create three threads, one for stdout, one for stderr, and one for joining those two, and handling exit codes. The three-thread option feels bloated and inefficient, but I understand that unbounded queue sizes is also a little scary.

Negative effects of this change is that the entirety of stderr is pushed into and out of memory, which could be problematic for large outputs.

Tests

  • cargo fmt --all -- --check to check standard code formatting (cargo fmt --all applies these changes)
  • cargo clippy --all --all-features -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect to check that you're using the standard code style
  • cargo build; cargo test --all --all-features to check that all the tests pass

@sophiajt

Copy link
Copy Markdown
Contributor

We recently had someone switch it to sync_channel to help deal with back pressure-related issues. This PR basically reverts that PR. Can we address the original reason for the first PR? #4986

One possible alternate approach: we could come up with a better way for complete to drain both streams without blocking on draining one before the other.

@fennewald

Copy link
Copy Markdown
Contributor Author

@jntrnr outside of busy-waiting and just checking each loop in turn, I don't believe there's support for polling multiple channels. We could pipe all command output over one channel, and wrap the buffers with some enum that distinguishes between stderr and stdout, but I think that would change too much of the interface with piping and the current ecosystem.

Crates like crossbeam would allow for selecting multiple channels. How would you feel about adding a dependency?

@fennewald

Copy link
Copy Markdown
Contributor Author

On that note, I just searched for crossbeam, and it's in my most recent Cargo.lock. It looks like nu-command uses rayon uses crossbeam, so that might make it a slightly easier call to include

@sophiajt

Copy link
Copy Markdown
Contributor

@fennewald - that's a thought. We have crossbeam-channel as a transitive dependency already, so we could try adding that.

I'm also okay with your multiplexing idea of sending both stderr and stdout through the same channel and demuxing them into separate streams at the receiver end.

@fennewald

Copy link
Copy Markdown
Contributor Author

@jntrnr I'm leaning towards crossbeam, but think it's worthwhile to try each. I'll do some rough impls and then loop back to this. Maybe in the process one will show itself to be a bad idea.

@fdncred
fdncred marked this pull request as draft April 15, 2022 12:21
@fdncred

fdncred commented Apr 15, 2022

Copy link
Copy Markdown
Contributor

making this a draft until we decide which way to go. thanks for your help figuring this out!

@fdncred

fdncred commented Apr 28, 2022

Copy link
Copy Markdown
Contributor

@fennewald any updates from your end? how's the testing going?

@fennewald

Copy link
Copy Markdown
Contributor Author

So sorry! Had gotten pulled away at work. Will follow up with updates

@fdncred

fdncred commented Jun 24, 2022

Copy link
Copy Markdown
Contributor

@jntrnr we have another vote to do this to revert the previous change. See #5625 for more details. Thoughts/opinions?

@fennewald
fennewald force-pushed the external_command_channel branch from b2bb417 to 75399d7 Compare June 27, 2022 19:22
@fennewald

Copy link
Copy Markdown
Contributor Author

I'm running into difficulties testing. I can't drain both stdout and strerr simultaneously without falling into one of these undesirable situations:

1.) Spawning 1 thread for stdout, 1 for stderr, and joining afterwards
2.) Using crossbeam, and checking if the channel is full before sending, in which case it switches to the other writer temporarily. The issue is that if neither channel is being read from, this will busy loop back and forth, which involves a lot of expensive atomic operations.
3.) Using the enum tagging method I suggested above, to poll stdout and stderr either involves busy-looping or adding significant extra code to get file descriptors from the streams and poll them (also the latter is only for unix).

At the end of the day, if we are adamant that we don't want to slurp the whole stream into memory, then we need to implement some type of polling, which means either:
1.) 1 thread per stream
2.) Add an async runtime, than 1 lightweight task per stream
3.) Add polling support (no clue what the windows equivalent is)

@fdncred

fdncred commented Jul 23, 2022

Copy link
Copy Markdown
Contributor

@jntrnr @fennewald Can we make some progress on this or should we just close it and figure out another way to approach this problem?

@fdncred

fdncred commented Aug 31, 2022

Copy link
Copy Markdown
Contributor

apparently this isn't going anywhere. thanks for the submission. we'd love to find a good solution.

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

Successfully merging this pull request may close these issues.

Capturing stderr hangs Nushell

3 participants