Skip to content

epoll_fd leaks to child processes #3809

@Tim-Zhang

Description

@Tim-Zhang

Version
>= 0.3

Platform
Linux k 5.8.0-38-generic #43~20.04.1-Ubuntu SMP Tue Jan 12 16:39:47 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Description

I tried this code:

use nix::unistd;
use std::ffi::CString;

fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
    let rt = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()?;

    rt.block_on(async {
        let path = CString::new("/usr/bin/sleep").unwrap();
        let args = vec![
            CString::new("sleep").unwrap(),
            CString::new("500000").unwrap(),
        ];
        let envs: Vec<&CString> = Vec::new();
        unsafe {
            match unistd::fork().unwrap() {
                unistd::ForkResult::Parent { child: _, .. } => {
                    println!("{}", "hehe");
                }
                unistd::ForkResult::Child => match unistd::execve(&path, &args, &envs) {
                    Err(_e) => {
                        std::process::exit(1);
                    }
                    _ => unreachable!(),
                },
            };
        }

        tokio::time::sleep(std::time::Duration::from_secs(10000)).await;
    });

    Ok(())
}

start the program then check fds.

tim@k:~$ ps aux|grep sleep
tim       279808  0.0  0.0   2516   592 pts/0    S+   13:44   0:00 sleep 500000
tim       281141  0.0  0.0   2516   584 ?        S    13:46   0:00 sleep 180
tim       281419  0.0  0.0  17672   728 pts/5    S+   13:47   0:00 grep --color=auto sleep

tim@k:~$ ls -l /proc/279808/fd
total 0
lrwx------ 1 tim tim 64 May 25 13:44 0 -> /dev/pts/0
lrwx------ 1 tim tim 64 May 25 13:44 1 -> /dev/pts/0
...
lrwx------ 1 tim tim 64 May 25 13:44 7 -> 'anon_inode:[eventpoll]'
...

The fd of epoll was mistakenly leaked to the child process

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-tokioArea: The main tokio crateC-bugCategory: This is a bug.M-ioModule: tokio/io

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions