fix(v2ray): race-free config cleanup, replace fixed 2s setTimeout#22
Merged
Sentinel-Bluebuilder merged 1 commit intomasterfrom Apr 27, 2026
Merged
fix(v2ray): race-free config cleanup, replace fixed 2s setTimeout#22Sentinel-Bluebuilder merged 1 commit intomasterfrom
Sentinel-Bluebuilder merged 1 commit intomasterfrom
Conversation
The V2Ray config file at <tmp>/sentinel-v2ray/config.json contains the session UUID. Old code deleted it via a fixed 2s setTimeout right after spawn(). Two problems: 1. Race against next loop iteration. cfgPath is the SAME path every attempt (config.json, not config.<n>.json). If outbound A's V2Ray fails fast and the loop advances to outbound B which overwrites cfgPath in <2s, the STALE 2s timer from A then deletes B's running config. 2. Pending timer holds the event loop open if the process exits before the timer fires. Fix: delete the moment V2Ray has demonstrably loaded the config (SOCKS port is up) OR when the process exits, whichever first. A 5s unref'd safety-net timer handles the edge case where neither happens before abort. Idempotent guard (cfgDeleted) prevents double-unlink warnings. On Windows, V2Ray may still have the file open when unlink runs — the existing try/catch swallows EBUSY, and the exit handler picks it up once the proc dies. No regression vs. the old timer there.
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.
Summary
The V2Ray config at
<tmpdir>/sentinel-v2ray/config.jsoncontains the per-session UUID and was being deleted by a fixedsetTimeout(..., 2000)right afterspawn(). Two problems with that:1. Race against the next loop iteration.
cfgPathis the same path every attempt (config.json, not unique-per-attempt). If outbound A's V2Ray dies fast and the loop iterates to outbound B which overwritescfgPath, the stale 2s timer from A then deletes B's freshly-written running config. Hard to repro, but real.2. Dangling timer holds the event loop open if the process exits in the gap before it fires.
Fix
connection/tunnel.js:563-583Replace the unconditional 2s timer with:
deleteCfg()guarded bycfgDeletedproc.once('exit', deleteCfg)so the cfg is removed when V2Ray dies (covers fast-fail path)deleteCfg()right afterwaitForPort(...)succeeds — at that point V2Ray has fully parsed the configunref()'d safety-netsetTimeoutfor the edge case where neither happens before abortWindows note: V2Ray may still have the file open when unlink runs. The existing
try { } catch {}swallowsEBUSY, and the exit handler picks it up once the process dies. No regression vs. the old timer behaviour there.Test plan
<tmpdir>/sentinel-v2ray/config.jsonis gone