]> git.lizzy.rs Git - rust.git/commit - src/tools/miri
Rollup merge of #82943 - kornelski:threadstdio, r=joshtriplett
authorYuki Okushi <huyuumi.dev@gmail.com>
Sun, 14 Mar 2021 04:07:34 +0000 (13:07 +0900)
committerGitHub <noreply@github.com>
Sun, 14 Mar 2021 04:07:34 +0000 (13:07 +0900)
commitdda9d0589f6b8e6bf76102ea0d8c6930ea46fec3
treed3bcdc1035a24a7fc635c74de2fe9b1a283d7ce9
parent9ce0820eefaa6dbfeadbca75f99063ae668cb30d
parentce2d95cd75adb5f75921536191b4dcfa41be1eff
Rollup merge of #82943 - kornelski:threadstdio, r=joshtriplett

Demonstrate best practice for feeding stdin of a child processes

Documentation change.

It's possible to create a deadlock with stdin/stdout I/O on a single thread:

* the child process may fill its stdout buffer, and have to wait for the parent process to read it,
* but the parent process may be waiting until its stdin write finishes before reading the stdout.

Therefore, the parent process should use separate threads for writing and reading.

These examples are not deadlocking in practice, because they use short strings, but I think it's better to demonstrate code that works even for long writes. The problem is non-obvious and tricky to debug (it seems that even libstd has a similar issue: #45572).

This also demonstrates how to use stdio with threads: it's not obvious that `.take()` can be used to avoid fighting with the borrow checker.

I've checked that the modified examples run fine.
library/std/src/process.rs