Repository navigation
fix(backend): verify the port is actually free after killing its owner - #1971
Open
carlosjarenom wants to merge 4 commits into
Open
carlosjarenom wants to merge 4 commits into
carlosjarenom wants to merge 4 commits into
Conversation
killProcessOnPort() sent a single kill, slept a fixed 500ms, and returned whatever one checkPortAvailable() call said. Process teardown is asynchronous, so a process that had not yet released the socket was reported as released, and the backend then failed to bind at startup with no useful diagnostic. Three things went wrong on Linux: - `fuser -k` sends SIGTERM. A process that does not act on it keeps the port bound, and nothing ever retried. Escalate to SIGKILL and poll for release. - `fuser` ships with psmisc, which minimal images often omit. The `|| true` made a missing tool indistinguishable from a successful kill, so the port was reported free while the old owner still held it. Distinguish exit 127 (missing) from exit 1 (nothing holding the port) and fall back to `lsof`, which the macOS path already relies on. - The single 500ms check has no room for a slow shutdown. Poll with backoff until the port is free or the attempts are exhausted, and log why when it is not. The port helpers move to electron/main/utils/port.ts. They had no test coverage, and they could not get any while they lived in init.ts: that module calls util.promisify, which the jsdom test environment replaces with a browser shim that does not provide it. The extracted module takes only child_process, net and electron-log, and the tests cover the escalation, the lsof fallback, the exit-1 case, and the failure path. Closes eigent-ai#1668 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
Author
|
Rebased onto current
The linked issue is still open, and this is ready for a look whenever you have time. |
3 of 8 tasks
This branch has not been deployed
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.
What Problem This Solves
On Linux,
killProcessOnPort()could report a port as released while the previous owner still held it. The backend then failed to bind at startup, with the port-kill step looking like it had succeeded.The function sent one kill, slept a fixed 500ms, and returned whatever a single
checkPortAvailable()call said. Process teardown is asynchronous, so a process that had not yet let go of the socket read as free.Three distinct problems fed into that:
fuser -ksends SIGTERM. A process that does not act on it keeps the port bound, and nothing ever retried.fuseris not always installed. It ships withpsmisc, which minimal images frequently omit. The|| truemade a missing tool indistinguishable from a successful kill, so the port was reported free while the old owner still had it.Solution Details
fuserexits 127 when it is not installed and 1 when no process holds the port. The first falls back tolsof— what the macOS path already relies on. The second is already the desired end state and must not trigger a second kill path. This is what|| truewas hiding.falseis now returned only after the port genuinely could not be freed, and the reason is logged.taskkill /F.Why the helpers moved to
electron/main/utils/port.tsThey had no test coverage and could not get any where they were.
init.tscallsutil.promisifyat module load, and the jsdom test environment resolvesutilto a browser shim withoutpromisify, so the module cannot be imported in a test at all. The extracted module depends only onchild_process,netandelectron-log, and avoidspromisify.init.tsre-exports both helpers, so existing importers are unaffected.Evidence
Eight tests in
test/unit/electron/main/port.test.ts:escalates to SIGKILL when SIGTERM does not free the portfalls back to lsof when fuser is not installeddoes not fall back to lsof when fuser simply found nothingreturns false when the port is never releasedlogs why the port could not be freedcheckPortAvailable(2)Reverting
killProcessOnPortto its previous implementation fails 4 of the 8 and passes the rest, so the suite is pinned to this behaviour rather than to the new code shape.vitest run test/unit/electron/main/port.test.ts— 8 passednpm run type-check— cleaneslinton the three touched files — clean (prettier and the repo's own pre-commit hooks also pass)test/unit/electron/has 22 pre-existing failures inbuildConfig,install-depsandenvUtil. Those fail identically onmainwithout this branch, so they are unrelated.User Impact
Eigent starts reliably when a previous backend instance left a port bound, including on minimal Linux installs without
psmisc. When a port genuinely cannot be freed — usually a process owned by another user — the log now says so instead of reporting success.Closes #1668