Skip to content

Commit 1539987

Browse files
authored
[commands::run_server_process]: fix race condition (#2077)
1 parent 6f0d3c3 commit 1539987

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/commands.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ fn run_server_process(startup_timeout: Option<Duration>) -> Result<ServerStartup
8989
let runtime = Runtime::new()?;
9090
let exe_path = env::current_exe()?;
9191
let workdir = exe_path.parent().expect("executable path has no parent?!");
92+
93+
// Spawn a blocking task to bind the Unix socket. Note that the socket
94+
// must be bound before spawning `_child` below to avoid a race between
95+
// the parent binding the socket and the child connecting to it.
96+
let listener = {
97+
let _guard = runtime.enter();
98+
tokio::net::UnixListener::bind(&socket_path)?
99+
};
100+
92101
let _child = process::Command::new(&exe_path)
93102
.current_dir(workdir)
94103
.env("SCCACHE_START_SERVER", "1")
@@ -97,7 +106,6 @@ fn run_server_process(startup_timeout: Option<Duration>) -> Result<ServerStartup
97106
.spawn()?;
98107

99108
let startup = async move {
100-
let listener = tokio::net::UnixListener::bind(&socket_path)?;
101109
let (socket, _) = listener.accept().await?;
102110

103111
read_server_startup_status(socket).await

0 commit comments

Comments
 (0)