rust-tokio

Can I prevent task stealing in multithreaded Tokio?


As I understand it, with the multithreaded runtime a Tokio task may be moved from worker thread to worker thread during its execution via the task stealing mechanism

Is there any way to tell the runtime to keep a specific task on one thread only? That is, to prevent a particular task from being stolen by another worker once it has been started?


Solution

  • Unfortunately, work-stealing is an integral part of how Tokio works, and there does not appear to be a way to entirely turn it off, either globally or per-task.

    Tokio does feature a "current thread" runtime, but that is likely a non-starter for your use case. A better bet would be to use a thread-per-core runtime such as glommio, which ensures that tasks spawned on a particular thread stay on that thread (and thus do not have to implement Send).


    All that said, I admittedly don't know much about ZeroMQ, but since it has been used by multiple high-profile companies, including CERN, for at least a decade, it is highly unlikely to be inherently thread unsafe. I would guess that if your hard-to-reproduce bug is rooted in your ZeroMQ sockets, then I would hazard that this is because your Rust crate has a bug where it does not follow spec.