Tokio threaded scheduler not exiting daemonized process

The code below prints "Bye!" as expected, but leaves a process running in the background:

use nix::unistd::daemon; // nix 0.17.0
use tokio::runtime::Builder; // tokio 0.2.11 features rt-threaded

fn main() {
        //.basic_scheduler() // clean exit
        .threaded_scheduler() // process keeps running
        .block_on(async {
            daemon(true, true).unwrap();

On my computer (Arch Linux, Rust 1.41), the program will correctly print "Bye!" and cleanly exit if the threaded scheduler is replaced with the basic scheduler, or if the process is not daemonized.

What could possibly be the reasons that the threaded scheduler does not play nicely with daemon()?

daemon(3) forks, and the child process only contains a copy of the single thread that called fork.

1 Like

In general multithreaded program doesn't play nicely with fork. If you want fork, do it only within main() before any other threads are spawned.

1 Like

Yeah, if you really must do any forking, do it as the very very first thing. It's rather dangerous.

1 Like

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