error[E0277]: `std::sync::mpsc::Sender<T>` cannot be shared between threads safely

Hello, everyone!

Upon completing Chapter 20 of the Rust book successfully, I decided I would continue expanding and building upon the newly-created web server to fit my own needs.

As such, my first goal was to refactor existing code into separate modules (and files) for future proofing. In doing so, however, I managed to get the following error, which I am unable to fix.

error[E0277]: `std::sync::mpsc::Sender<net::jobs::threading::Message>` cannot be shared between threads safely
--> src/net.rs:68:30
   |
68 |             self.worker_pool.execute(move || {
   |                              ^^^^^^^ `std::sync::mpsc::Sender<net::jobs::threading::Message>` cannot be shared between threads safely
   |

Full error (I am aware of the 2nd error thrown, but I can easily fix that):
Gist

Please see the gists of the files below (put into one gist due to new forum users limited to 2 links / post):
Links gist

Can you please point me in a direction as to what I'm doing wrong? It seems to me that all I've done is move code around in files, however, I obviously broke something somewhere.

I appreciate your time!

The error exists because handle_connection takes &self and is executed in another thread, forcing a Sync bound on Self.
Removing &self from the argument list of handle_connection and calling it via Self::handle_connection fixed it:

By the way, you can add more files to a gist by clicking Add File on the bottom left

1 Like

Thank you for your quick reply, @ldesgoui!

You were right, making handle_connection an associated, static function fixed my issue.
Also, thank you for the Gist tip! I rarely used it in the past, but I'll make sure to keep this feature in mind.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.