fix(go-core): stop the server when the TUI exits or its parent dies - #1131
Merged
Conversation
go-core-server kept running on Windows after the TUI closed and had to be
killed from Task Manager. stopGoCore existed but nothing called it, and
Windows never signals a child when its parent exits. Each new TUI then
found the orphan on 43001 and started another server on the next port.
Two layers:
- The spawner registers a once("exit") hook on the host process that
calls stopGoCore, and passes GO_CORE_PARENT_PID to the child.
- The Go server watches that PID every 2s (OpenProcess/GetExitCodeProcess
on Windows, kill(pid, 0) elsewhere) and runs the existing graceful
shutdown when the parent is gone, covering crashes and forced kills.
Also clears the errcheck and unused findings in cmd/server/main.go.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Problem
On Windows,
go-core-server.exestays alive after the TUI closes and has to be killed from Task Manager.stopGoCore()exists inpackages/core/src/router/go-core.tsbut nothing calls it, and Windows never signals a child when its parent exits. Each new TUI then finds the orphan on port 43001 (findAvailablePort) and starts yet another server on the next port, so orphans accumulate.Fix
Two independent layers, so the server also goes away when the TUI crashes or is force-killed:
installExitHook(process, stopGoCore)registers a singleonce("exit")hook per host process;childEnvpassesGO_CORE_PARENT_PIDto the child alongsideGO_CORE_PORT.internal/parentwatch.Alive(pid)usesOpenProcess+GetExitCodeProcesson Windows andkill(pid, 0)elsewhere.Watchpolls every 2s and cancels the server context when the parent is gone, which runs the existing gracefulShutdown. The watchdog only starts whenGO_CORE_PARENT_PIDis set, so standalone runs (Docker/VPS) are unaffected.cmd/server/main.goalso loses its errcheck and unused findings (server.Shutdown,os.Remove, twofmt.Fprintf,errorPool). Remaining golangci-lint findings infs_handlers.go,health.go,metrics_handlers.go,session_crud_handlers.goandsession_handlers_test.goare pre-existing and untouched.Verification
go test ./cmd/server/ ./internal/parentwatch/: pass (4 new tests: own pid alive, exited child not alive, watch fires for exited parent, watch stops on cancel).GOOS=linux go vetalso clean.bun test packages/core/test/router/go-core.test.ts: 3 pass.go-core-server.exe, started it withGO_CORE_PARENT_PIDpointing to a PowerShell process that exits after 4s./healthreturned 200 while the parent lived; log showsparent process 6620 exited, shutting downand the process was gone 2s after the parent.🤖 Generated with Claude Code