Backend always exits with code 1 on Windows when trying to exit with non-zero code
Bug Description:
On Windows, the Theia backend process always exits with code 1, whatever exit code the application requested with process.exit(code).
BackendApplication registers process.on('exit', () => this.onStop()), and onStop() calls ProcessUtils.terminateProcessTree(process.pid) to clean up child processes. On Windows, winTerminateProcessTree() runs:
taskkill.exe /f /t /pid <process.pid>taskkill /T kills the root PID along with its descendants, so the backend kills itself from inside its own exit handler. taskkill /F uses TerminateProcess(handle, 1), which overrides the exit code Node.js was about to report with 1.
The Unix implementation does not have this problem because unixTerminateProcessTree() explicitly skips process.pid (// Prevent killing the current process). The Windows implementation has no such guard.
As a result, on Windows a Theia-based application cannot report success or a specific failure code to its caller, for example when the backend runs as a CLI or in a script or CI job. Anything that checks %ERRORLEVEL% / $LASTEXITCODE sees 1.
A possible fix is to have winTerminateProcessTree() handle ppid === process.pid the same way the Unix path does: enumerate the direct children of the current process (e.g. with Get-CimInstance Win32_Process -Filter 'ParentProcessId=<pid>'), run taskkill /f /t on each child's tree, and leave the current process to finish exiting on its own.
Steps to Reproduce:
- On Windows, add a backend contribution to a Theia application (or use a CLI entry point that starts the backend) that calls
process.exit(0)(or any code other than1) after startup, e.g. inonStart(). - Start the backend from a terminal, e.g.
node lib/backend/main.jsinexamples/browser. - After the process exits, check the exit code:
echo %ERRORLEVEL%(cmd) or$LASTEXITCODE(PowerShell).
Expected: the code passed to process.exit().
Actual: 1.
Run the same steps on Linux or macOS and the requested exit code is reported correctly.
Additional Information
- Operating System: Windows
- Theia Version: 1.72.3 (the code is unchanged on
master/ 1.76.0)
Source: eclipse-theia/theia