Skip to content

Comments

fix: properly cleanup Windows process tree on Obsidian exit#34

Merged
mtymek merged 5 commits intomtymek:mainfrom
Gerkinfeltser:fix/windows-process-cleanup-static
Feb 23, 2026
Merged

fix: properly cleanup Windows process tree on Obsidian exit#34
mtymek merged 5 commits intomtymek:mainfrom
Gerkinfeltser:fix/windows-process-cleanup-static

Conversation

@Gerkinfeltser
Copy link
Contributor

Problem

On Windows, when Obsidian closes with the OpenCode panel open, the server process (node.exe) becomes orphaned because:

  1. shell: true spawns cmd.exe → node.exe
  2. Killing cmd.exe leaves node.exe orphaned

Solution

All changes in WindowsProcess.ts:

  • Store process in static currentProcess field
  • Register beforeunload handler for window close cleanup
  • Use PowerShell Get-CimInstance to find and kill child processes
  • Kill child processes (actual node.exe) before parent (cmd.exe)

Changes

Single file modified: src/server/process/WindowsProcess.ts

Testing

Tested on Windows 11:

  • Server starts correctly
  • Server stops when clicking panel close button
  • Server stops when closing Obsidian window
  • No orphaned node.exe processes after exit

Breaking Changes

None. Minimal changes to existing architecture.

### Fixed
- Replaced unreliable taskkill /T with PowerShell Get-CimInstance for child process detection in WindowsProcess.ts
- Fixed orphaned node.exe processes when Obsidian closes by killing child processes before parent
- Added proper cleanup when shell: true creates cmd.exe -> node.exe process tree

### Added
- Static currentProcess field to track active process for cleanup during window close
- Static cleanupHandlerRegistered flag to prevent duplicate event handlers
- beforeunload event handler for synchronous cleanup when Obsidian window closes
- killProcessSync method for immediate process termination without async delays
- registerCleanupHandler method to set up window close event listener

### Changed
- Updated start method to store process reference and register cleanup handler
- Modified stop method to use PowerShell child lookup before killing parent process
- Enhanced error handling with try/catch blocks for PowerShell and taskkill operations
Changed hardcoded path /etc/profiles/per-user/mat/bin/ls to /bin/ls
which exists on most Unix systems. The previous path was specific to
the original developer's machine and failed on CI runners.
The static cleanup handler was interfering with test lifecycle,
causing the server to be killed during database migration.
Skip registration when VITEST environment variable is set.
Check for CI environment variable (set by GitHub Actions and most CI
systems) instead of VITEST to skip beforeunload handler registration
during automated tests.
Remove broken caching for OpenCode binary and add explicit PATH
configuration to ensure opencode command is available on Ubuntu runners.
The cache wasn't properly restoring the binary to PATH.
@mtymek mtymek merged commit aaa71df into mtymek:main Feb 23, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants