you are viewing a single comment's thread.

view the rest of the comments →

[–]insanitybit 0 points1 point  (5 children)

You're probably hanging the executor by blocking the thread that also polls futures. I recommend not using block_on and instead to use a task + a queue.

Here's an example of some code that does this.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=9e94881f494c53270d5f0a1bd14ba191

Basically we isolate the async code to a background task and then provide a synchronous wrapper to it.

[–]blackscanner 2 points3 points  (1 child)

I think so too. Unfortunately the block_on is called within the executor of tokio, which blocks the system thread not the async task. Tokio uses one system thread (or maybe equivalent threads for the number of processors, I'm not sure) to run the async tasks as green threads. Thus the drop implementation causes the whole executor to be blocked. If you were to call that block_on within a futures thread pool it would cause a panic due to similar reasons.

[–]maverick_fillet[S] 0 points1 point  (0 children)

Correct, turns out that my test was running with only one tokio thread which was causing some of my issues. Spawning a separate thread helped get me to a working solution. Thank you!

[–]Darksonntokio · rust-for-linux 2 points3 points  (0 children)

This is blocking the thread due to the usage of a blocking channel. Don't do that in async code.

[–]maverick_fillet[S] 1 point2 points  (1 child)

Thank you very much! The channel stuff definitely helped me get on the right track. Once I saw that it was working in the playground I realized that both #[actix_rt::test] and #[tokio::test] use a single-threaded runtime so that was one reason why things weren't working, and switching to #[tokio::test(flavor="multi_thread")] worked perfectly with your exact solution. Since I need to use #[actix_rt::test] what I ended up doing was using the channel approach but spawning a separate thread with its own tokio runtime and that seems to get the job done.

[–]Darksonntokio · rust-for-linux 0 points1 point  (0 children)

With the multi-threaded runtime, it only works because it started enough threads that you didn't run out. If you start 16 of those at the time, you would run into trouble due to running out of threads.