Skip to content

Implement task-safe "current directory" #10463

Closed
@klutzy

Description

@klutzy

Currently we provide os::getcwd()/os::change_dir() via OS functions (libc::getcwd or GetCurrentDirectory), but curdir information is maintained per-process, which means it is not thread-safe nor task-safe.

Here is a sample code for current dir race:

use std::os;
use std::io;
use std::io::fs;

fn main() {
    let tmp_path = os::tmpdir();
    for i in range(0u, 20u) {
        let path = tmp_path.join(i.to_str());
        do spawn {
            io::result(|| fs::mkdir(&path, io::UserRWX));
            let _ret = os::change_dir(&path);
            let cur = os::getcwd();
            if cur != path {
                println!("expected {:s} but found {:s}",
                          path.as_str().unwrap(), cur.as_str().unwrap());
            }
        }
    }
}

So we must ban them and implement "current directory" using task-local storage.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions