Rust book chapter 20: Multi-threaded web server unwrap fails?

Hi! I have been working my way through the Rust Book and have found it to be an incredibly helpful, but as I am implementing the multi-threaded web server project from Chapter 20, I am getting errors that don't seem to occur in the examples, specifically listing 20-20. I have used diff to make sure that my code matches the listings in the book, but the unwrap for the Receiver seems to fail for any worker but the first.

Here is my terminal output when I use cargo run:

~/projects/rust_book/hello𓃯  cargo run
warning: field is never read: `workers`
 --> src/lib.rs:7:5
  |
7 |     workers: Vec<Worker>,
  |     ^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: field is never read: `id`
  --> src/lib.rs:48:5
   |
48 |     id: usize,
   |     ^^^^^^^^^

warning: field is never read: `thread`
  --> src/lib.rs:49:5
   |
49 |     thread: thread::JoinHandle<()>,
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: 3 warnings emitted

    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/main`
Worker 1 got a job; executing.
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: RecvError', src/lib.rs:55:23
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: "PoisonError { inner: .. }"', src/lib.rs:55:23
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: "PoisonError { inner: .. }"', src/lib.rs:55:23
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: "PoisonError { inner: .. }"', src/lib.rs:55:23
Worker 0 got a job; executing.
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: RecvError', src/lib.rs:55:23
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: "PoisonError { inner: .. }"', src/lib.rs:55:23
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: "PoisonError { inner: .. }"', src/lib.rs:55:23
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: "PoisonError { inner: .. }"', src/lib.rs:55:23
thread 'Worker 0 got a job; executing.
<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: RecvError', src/lib.rs:55:23
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: "PoisonError { inner: .. }"', src/lib.rs:55:23
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: "PoisonError { inner: .. }"', src/lib.rs:55:23
Worker 0 got a job; executing.
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: RecvError', src/lib.rs:55:23
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: "PoisonError { inner: .. }"', src/lib.rs:55:23
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: "PoisonError { inner: .. }"', src/lib.rs:55:23
Worker 0 got a job; executing.
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: RecvError', src/lib.rs:55:23
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: "PoisonError { inner: .. }"', src/lib.rs:55:23
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: "PoisonError { inner: .. }"', src/lib.rs:55:23
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: "PoisonError { inner: .. }"', src/lib.rs:55:23
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: "PoisonError { inner: .. }"', src/lib.rs:55:23
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: "PoisonError { inner: .. }"', src/lib.rs:55:23

I am currently running the following OS and rust version:

stable-x86_64-apple-darwin (default)
rustc 1.45.2 (d3fb005a3 2020-07-31)

Is this a new issue?

First RecvError error you get means that the sender has been dropped. Double-check what happens on the sending side — is seems to be quitting early. Maybe it's not waiting for that unused thread::JoinHandle?

The rest of the errors are just a consequence of the first one, which caused a panic under a mutex.

1 Like

Thank you so much!
The lib.rs code was fine, but in my main function, I was creating a new thread pool for each request.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.