Commit 263c061f authored by Tobias Koppers's avatar Tobias Koppers

Fix review findings: exit codes, crash handler, locking, guards

Process lifecycle:
- Propagate worst worker exit code instead of always exit(0) — CI now
  detects failed project builds
- Add daemonKilledByUs flag to suppress "exited unexpectedly" message
  when the orchestrator intentionally kills the daemon
- Use process.once instead of process.on for signal/exit handlers to
  prevent accumulation on reuse
- Skip --turbopack-daemon flag for webpack-only workers

Rust correctness:
- Add require_local() guards to project_trace_source,
  project_get_source_for_asset, project_get_source_map — these were
  routing to daemon which always returns "not implemented", now they
  fail fast with a clear message
- ProjectOnExit: use atomic remove pattern (matching ProjectShutdown)
  to eliminate TOCTOU race on concurrent calls
- project_on_exit_internal: graceful no-op on double call instead of
  panic — can happen when both project_on_exit and project_shutdown
  are called

Minor:
- Gate getProjectGroups() behind argv.includes check to avoid
  unconditional require on every next dev/build
- Fix rootPath computed twice in turbopack-analyze
- Add explanatory comments to cancel_subscription and run_daemon_server
Co-Authored-By: 's avatarClaude <noreply@anthropic.com>
parent 51f6735f
...@@ -192,6 +192,9 @@ impl DaemonClient { ...@@ -192,6 +192,9 @@ impl DaemonClient {
/// Cancel a subscription. /// Cancel a subscription.
pub async fn cancel_subscription(&self, callback_id: CallbackId) -> Result<()> { pub async fn cancel_subscription(&self, callback_id: CallbackId) -> Result<()> {
// Remove locally first: even if the send below fails (connection
// closed), we stop processing callbacks for this subscription.
// The daemon cleans up its side when the connection drops.
self.inner.subscriptions.lock().await.remove(&callback_id); self.inner.subscriptions.lock().await.remove(&callback_id);
let req = DaemonRequest::CancelSubscription { callback_id }; let req = DaemonRequest::CancelSubscription { callback_id };
let encoded = bincode::encode_to_vec(&req, bincode::config::standard())?; let encoded = bincode::encode_to_vec(&req, bincode::config::standard())?;
......
...@@ -65,8 +65,11 @@ impl DaemonState { ...@@ -65,8 +65,11 @@ impl DaemonState {
} }
} }
/// Run the daemon server, listening on the given socket path. /// Starts the daemon server and listens for connections on `socket_path`.
/// This function blocks indefinitely until the process is killed. ///
/// This function blocks indefinitely on Unix (infinite accept loop) and only
/// returns if there is a fatal error during socket setup. On Windows, the
/// behavior is analogous but uses named pipes.
pub async fn run_daemon_server(socket_path: &str) -> Result<()> { pub async fn run_daemon_server(socket_path: &str) -> Result<()> {
let state = DaemonState::new(); let state = DaemonState::new();
...@@ -328,12 +331,10 @@ async fn dispatch_request(req: DaemonRequest, state: &DaemonState) -> DaemonResp ...@@ -328,12 +331,10 @@ async fn dispatch_request(req: DaemonRequest, state: &DaemonState) -> DaemonResp
} }
DaemonRequest::ProjectOnExit { call_id, project } => { DaemonRequest::ProjectOnExit { call_id, project } => {
// OnExit stops turbo-tasks and removes the handle from the map, // OnExit stops turbo-tasks and removes the handle from the map.
// just like Shutdown. The difference is that OnExit is a fire-and-forget // Uses atomic remove to avoid TOCTOU races with concurrent Shutdown.
// call from the JS side (errors are swallowed by the caller). if let Some(proj) = state.projects.write().await.remove(&project) {
if let Some(proj) = state.get_project(project).await {
proj.turbo_tasks.stop_and_wait().await; proj.turbo_tasks.stop_and_wait().await;
state.projects.write().await.remove(&project);
} }
DaemonResponse::Ok { DaemonResponse::Ok {
call_id, call_id,
......
...@@ -907,10 +907,11 @@ pub async fn project_on_exit( ...@@ -907,10 +907,11 @@ pub async fn project_on_exit(
async fn project_on_exit_internal(project: &ProjectInstance) { async fn project_on_exit_internal(project: &ProjectInstance) {
let exit_receiver = project.exit_receiver.lock().await.take(); let exit_receiver = project.exit_receiver.lock().await.take();
exit_receiver if let Some(receiver) = exit_receiver {
.expect("`project.onExitSync` must only be called once") receiver.run_exit_handler().await;
.run_exit_handler() }
.await; // Silently ignore double calls — can happen if both project_on_exit
// and project_shutdown are called by the JS side.
} }
/// Runs `project_on_exit`, and then waits for turbo_tasks to gracefully shut down. /// Runs `project_on_exit`, and then waits for turbo_tasks to gracefully shut down.
...@@ -2586,24 +2587,7 @@ pub async fn project_trace_source( ...@@ -2586,24 +2587,7 @@ pub async fn project_trace_source(
frame: StackFrame, frame: StackFrame,
current_directory_file_url: String, current_directory_file_url: String,
) -> napi::Result<Option<StackFrame>> { ) -> napi::Result<Option<StackFrame>> {
if let Some(remote) = project.remote.as_ref() { project.require_local("project_trace_source")?;
let handle = remote.handle;
let proto_frame: next_api::ipc::protocol::StackFrame = frame.into();
let result = remote
.call(
|call_id| next_api::ipc::protocol::DaemonRequest::ProjectTraceSource {
call_id,
project: handle,
frame: proto_frame,
current_directory_file_url,
},
)
.await?;
return match result {
next_api::ipc::protocol::DaemonResult::StackFrame(opt) => Ok(opt.map(Into::into)),
_ => Err(napi::Error::from_reason("Unexpected daemon response")),
};
}
let container = project.container(); let container = project.container();
let ctx = &project.ctx(); let ctx = &project.ctx();
ctx.turbo_tasks() ctx.turbo_tasks()
...@@ -2627,23 +2611,7 @@ pub async fn project_get_source_for_asset( ...@@ -2627,23 +2611,7 @@ pub async fn project_get_source_for_asset(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>, #[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
file_path: RcStr, file_path: RcStr,
) -> napi::Result<Option<String>> { ) -> napi::Result<Option<String>> {
if let Some(remote) = project.remote.as_ref() { project.require_local("project_get_source_for_asset")?;
let handle = remote.handle;
let file_path_str = file_path.to_string();
let result = remote
.call(
|call_id| next_api::ipc::protocol::DaemonRequest::ProjectGetSourceForAsset {
call_id,
project: handle,
file_path: file_path_str,
},
)
.await?;
return match result {
next_api::ipc::protocol::DaemonResult::StringOption(opt) => Ok(opt),
_ => Err(napi::Error::from_reason("Unexpected daemon response")),
};
}
let container = project.container(); let container = project.container();
let ctx = &project.ctx(); let ctx = &project.ctx();
ctx.turbo_tasks() ctx.turbo_tasks()
...@@ -2677,23 +2645,7 @@ pub async fn project_get_source_map( ...@@ -2677,23 +2645,7 @@ pub async fn project_get_source_map(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>, #[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
file_path: RcStr, file_path: RcStr,
) -> napi::Result<Option<String>> { ) -> napi::Result<Option<String>> {
if let Some(remote) = project.remote.as_ref() { project.require_local("project_get_source_map")?;
let handle = remote.handle;
let file_path_str = file_path.to_string();
let result = remote
.call(
|call_id| next_api::ipc::protocol::DaemonRequest::ProjectGetSourceMap {
call_id,
project: handle,
file_path: file_path_str,
},
)
.await?;
return match result {
next_api::ipc::protocol::DaemonResult::StringOption(opt) => Ok(opt),
_ => Err(napi::Error::from_reason("Unexpected daemon response")),
};
}
let container = project.container(); let container = project.container();
let ctx = &project.ctx(); let ctx = &project.ctx();
ctx.turbo_tasks() ctx.turbo_tasks()
......
...@@ -1175,5 +1175,6 @@ ...@@ -1175,5 +1175,6 @@
"1174": "--port requires a valid port number (0-65535), got: %s", "1174": "--port requires a valid port number (0-65535), got: %s",
"1175": "--experimental-project requires a directory argument", "1175": "--experimental-project requires a directory argument",
"1176": "--port requires a valid port number (0-65535), got: %s%s", "1176": "--port requires a valid port number (0-65535), got: %s%s",
"1177": "Turbopack daemon did not become ready within %s%ss" "1177": "Turbopack daemon did not become ready within %s%ss",
"1178": "Worker exited with code %s"
} }
...@@ -141,6 +141,7 @@ function parseValidInspectAddress(value: string): DebugAddress { ...@@ -141,6 +141,7 @@ function parseValidInspectAddress(value: string): DebugAddress {
* shared Turbopack daemon. * shared Turbopack daemon.
*/ */
function getProjectGroups() { function getProjectGroups() {
if (!process.argv.includes('--experimental-project')) return []
const { parseProjectGroups } = const { parseProjectGroups } =
require('../lib/multi-project') as typeof import('../lib/multi-project') require('../lib/multi-project') as typeof import('../lib/multi-project')
return parseProjectGroups(process.argv) return parseProjectGroups(process.argv)
......
...@@ -57,7 +57,7 @@ export async function turbopackAnalyze( ...@@ -57,7 +57,7 @@ export async function turbopackAnalyze(
const project = await bindings.turbo.createProject( const project = await bindings.turbo.createProject(
{ {
rootPath: config.turbopack?.root || config.outputFileTracingRoot || dir, rootPath: rootPath,
projectPath: normalizePath(path.relative(rootPath, dir) || '.'), projectPath: normalizePath(path.relative(rootPath, dir) || '.'),
distDir, distDir,
nextConfig: config, nextConfig: config,
......
...@@ -3,7 +3,6 @@ import fs from 'fs' ...@@ -3,7 +3,6 @@ import fs from 'fs'
import os from 'os' import os from 'os'
import path from 'path' import path from 'path'
import crypto from 'crypto' import crypto from 'crypto'
import { once } from 'events'
/** Time to wait before sending SIGKILL when children don't respond to signals. */ /** Time to wait before sending SIGKILL when children don't respond to signals. */
const FORCE_KILL_TIMEOUT_MS = 5_000 const FORCE_KILL_TIMEOUT_MS = 5_000
...@@ -158,8 +157,9 @@ function spawnWorker( ...@@ -158,8 +157,9 @@ function spawnWorker(
} else if (group.webpack) { } else if (group.webpack) {
args.push('--webpack') args.push('--webpack')
} }
// Always pass the daemon socket; webpack-mode workers simply ignore it if (!group.webpack) {
args.push('--turbopack-daemon', socketPath) args.push('--turbopack-daemon', socketPath)
}
return spawn(process.execPath, args, { return spawn(process.execPath, args, {
stdio: 'inherit', stdio: 'inherit',
...@@ -182,10 +182,11 @@ export async function runMultiProject( ...@@ -182,10 +182,11 @@ export async function runMultiProject(
// Track workers so the daemon crash handler can kill them // Track workers so the daemon crash handler can kill them
const workers: ChildProcess[] = [] const workers: ChildProcess[] = []
let daemonKilledByUs = false
// If the daemon crashes unexpectedly, kill all workers // If the daemon crashes unexpectedly, kill all workers
daemon.on('exit', (code, signal) => { daemon.on('exit', (code, signal) => {
if (code !== 0 && code !== null) { if (!daemonKilledByUs && code !== 0 && code !== null) {
console.error( console.error(
`Turbopack daemon exited unexpectedly (code ${code}, signal ${signal}), killing workers` `Turbopack daemon exited unexpectedly (code ${code}, signal ${signal}), killing workers`
) )
...@@ -213,10 +214,11 @@ export async function runMultiProject( ...@@ -213,10 +214,11 @@ export async function runMultiProject(
} }
// Best-effort cleanup on unexpected exit // Best-effort cleanup on unexpected exit
process.on('exit', cleanupSocket) process.once('exit', cleanupSocket)
// 3. Forward signals to daemon + all workers // 3. Forward signals to daemon + all workers
const forwardSignal = (signal: NodeJS.Signals) => { const forwardSignal = (signal: NodeJS.Signals) => {
daemonKilledByUs = true
if (!daemon.killed) daemon.kill(signal) if (!daemon.killed) daemon.kill(signal)
workers.forEach((w) => { workers.forEach((w) => {
if (!w.killed) w.kill(signal) if (!w.killed) w.kill(signal)
...@@ -235,18 +237,38 @@ export async function runMultiProject( ...@@ -235,18 +237,38 @@ export async function runMultiProject(
process.exit(exitCodes[signal] ?? 143) process.exit(exitCodes[signal] ?? 143)
}, FORCE_KILL_TIMEOUT_MS).unref() }, FORCE_KILL_TIMEOUT_MS).unref()
} }
process.on('SIGINT', () => forwardSignal('SIGINT')) process.once('SIGINT', () => forwardSignal('SIGINT'))
process.on('SIGTERM', () => forwardSignal('SIGTERM')) process.once('SIGTERM', () => forwardSignal('SIGTERM'))
if (process.platform !== 'win32') { if (process.platform !== 'win32') {
process.on('SIGHUP', () => forwardSignal('SIGHUP')) process.once('SIGHUP', () => forwardSignal('SIGHUP'))
} }
// 4. Wait for all workers to exit, then kill daemon // 4. Wait for all workers to exit, then kill daemon
await Promise.allSettled(workers.map((w) => once(w, 'exit'))) const workerPromises = workers.map(
(w) =>
new Promise<number>((resolve, reject) => {
w.once('exit', (code) => {
code !== 0 && code !== null
? reject(new Error(`Worker exited with code ${code}`))
: resolve(0)
})
w.once('error', reject)
})
)
const results = await Promise.allSettled(workerPromises)
// Propagate the worst worker exit code so CI detects failures.
let exitCode = 0
for (const r of results) {
if (r.status === 'rejected') {
exitCode = 1
}
}
// Cleanup: kill daemon and socket file. // Cleanup: kill daemon and socket file.
// On Windows, named pipes are automatically cleaned up by the OS. // On Windows, named pipes are automatically cleaned up by the OS.
daemon.kill('SIGTERM') daemonKilledByUs = true
if (!daemon.killed) daemon.kill('SIGTERM')
cleanupSocket() cleanupSocket()
process.exit(0) process.exit(exitCode)
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment