Commit e6c9dd4
## Summary
Fixes #283 — a second Neovim instance failing to start the WebSocket
server with `Failed to listen on port 48811: EADDRINUSE: address already
in use` when another instance is already running.
## Root cause
Three contributing factors; only the first is new:
1. **Regression (introduced in #282).** `find_available_port` was
rewritten from a shuffle (`utils.shuffle_array`, which seeded the PRNG
via `math.randomseed(os.time())` as a side effect) into a bare,
**unseeded** `math.random`. Under LuaJIT's fixed default seed, every
fresh Neovim process draws the identical offset and selects the
*identical* port, so parallel instances always collide.
2. **Broken probe (pre-existing).** The throwaway bind probe cannot
detect an active listener: libuv's `uv_tcp_bind` defers `EADDRINUSE`
into `delayed_error` and returns success, so the error only surfaces at
`listen()` — which is why the message reads "Failed to listen", not
"Failed to bind".
3. **No retry (pre-existing).** `create_server` committed to a single
pre-probed port and gave up on failure.
The lost seeding turned a previously-rare same-second collision into a
deterministic, every-time failure.
## Fix
- Seed the PRNG once per process in `tcp.lua` (`os.time` plus a
sub-second `hrtime` jitter; `hrtime` is guarded because some test stubs
omit it), restoring per-process port spread.
- `create_server` now binds **and** listens on a single fresh handle per
candidate port and advances to the next candidate on failure, so racing
instances recover instead of erroring out. The bind-only
`find_available_port` is retained as a best-effort pre-filter only (it
cannot detect a live listener).
- Removed the now-orphaned `utils.shuffle_array` (dead code since #282).
- The candidate iterator stays a closure rather than materializing the
~55k-entry range, preserving the startup-cost improvement from #282.
## Verification
- `mise run all` — 658 tests pass, luacheck clean (0 warnings),
formatted.
- New regression tests in `tests/unit/server/tcp_spec.lua`: retry past a
busy candidate, range exhaustion (clear error, every handle closed), the
bind-success/`listen()`-`EADDRINUSE` case, and listening-handle
identity.
- `scripts/repro_issue_283.sh` (added in the first commit) now **exits
0** — two parallel instances start on *different* ports instead of
colliding; it exited 1 before the fix.
A full AI-generated triage with the mechanism analysis (including libuv
`delayed_error` source references) is posted on #283.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Signed-off-by: Thomas Kosiewski <tk@coder.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 7644451 commit e6c9dd4
6 files changed
Lines changed: 658 additions & 59 deletions
File tree
- fixtures/issue-283
- lua/claudecode/server
- scripts
- tests/unit/server
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
17 | 68 | | |
18 | 69 | | |
19 | 70 | | |
| |||
25 | 76 | | |
26 | 77 | | |
27 | 78 | | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
| 79 | + | |
36 | 80 | | |
37 | 81 | | |
38 | 82 | | |
| |||
47 | 91 | | |
48 | 92 | | |
49 | 93 | | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
50 | 132 | | |
51 | 133 | | |
52 | 134 | | |
53 | 135 | | |
54 | 136 | | |
55 | 137 | | |
56 | 138 | | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
| 139 | + | |
| 140 | + | |
66 | 141 | | |
67 | | - | |
| 142 | + | |
68 | 143 | | |
69 | | - | |
70 | | - | |
| 144 | + | |
| 145 | + | |
71 | 146 | | |
72 | 147 | | |
73 | 148 | | |
| |||
76 | 151 | | |
77 | 152 | | |
78 | 153 | | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
90 | 167 | | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
| 168 | + | |
98 | 169 | | |
99 | 170 | | |
100 | | - | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
101 | 176 | | |
102 | 177 | | |
103 | 178 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
386 | 386 | | |
387 | 387 | | |
388 | 388 | | |
389 | | - | |
390 | | - | |
391 | | - | |
392 | | - | |
393 | | - | |
394 | | - | |
395 | | - | |
396 | | - | |
397 | | - | |
398 | | - | |
399 | | - | |
400 | | - | |
401 | | - | |
402 | | - | |
403 | | - | |
404 | | - | |
405 | | - | |
406 | | - | |
407 | | - | |
408 | 389 | | |
409 | 390 | | |
410 | 391 | | |
| |||
0 commit comments