Repository navigation
Commit e19c574
committed
fix(broker): shut down the app-server broker after an idle timeout
A broker outlives the client that spawned it. Nothing in the protocol tells it
the client is gone for good, so a spawned broker stays resident forever holding
its socket dir -- roughly 40MB of RSS each, until the machine reboots.
This is reproducible from this repo's own test suite. A single
`node --test tests/runtime.test.mjs` run leaves ~58 brokers behind: each test
creates a fresh temp workspace, ensureBrokerSession spawns a broker per
workspace, and nothing ever tears them down. Three suite runs on one machine
left 174 idle brokers alive, all at 00:00:00 CPU with no connections, holding
~2.9GB of resident memory between them.
The broker now shuts itself down after being idle for --idle-timeout
milliseconds (default 30 minutes, overridable with
CODEX_COMPANION_BROKER_IDLE_MS, 0 to disable). Idle means no connected sockets
and nothing in flight, so an open connection -- including a long streaming turn
-- always holds it open.
The decision is made by the broker rather than by an external sweep because the
broker is the only party that can observe whether it is serving anyone. Scanning
the job registry from outside and then killing is racy by construction: a job
can start between the scan and the kill.
An idle exit leaves broker.json behind, which is already handled --
ensureBrokerSession probes the recorded endpoint and, when it does not answer,
tears the session down and respawns. The broker deliberately does not clear
broker.json itself: a replacement broker may already have rewritten that record,
and clearing it would delete the live broker's endpoint.
Blank and whitespace-only values fall back to the default rather than parsing as
0, so idle shutdown can only be disabled explicitly.1 parent db52e28 commit e19c574
2 files changed
Lines changed: 227 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 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 | + | |
14 | 41 | | |
15 | 42 | | |
16 | 43 | | |
| |||
48 | 75 | | |
49 | 76 | | |
50 | 77 | | |
51 | | - | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
52 | 81 | | |
53 | 82 | | |
54 | 83 | | |
55 | | - | |
| 84 | + | |
56 | 85 | | |
57 | 86 | | |
58 | 87 | | |
| |||
63 | 92 | | |
64 | 93 | | |
65 | 94 | | |
| 95 | + | |
66 | 96 | | |
67 | 97 | | |
68 | 98 | | |
69 | 99 | | |
70 | 100 | | |
71 | 101 | | |
72 | 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 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
73 | 139 | | |
74 | 140 | | |
75 | 141 | | |
| |||
116 | 182 | | |
117 | 183 | | |
118 | 184 | | |
| 185 | + | |
119 | 186 | | |
120 | 187 | | |
121 | 188 | | |
| |||
225 | 292 | | |
226 | 293 | | |
227 | 294 | | |
| 295 | + | |
228 | 296 | | |
229 | 297 | | |
230 | 298 | | |
231 | 299 | | |
232 | 300 | | |
| 301 | + | |
233 | 302 | | |
234 | 303 | | |
235 | 304 | | |
| |||
243 | 312 | | |
244 | 313 | | |
245 | 314 | | |
246 | | - | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
247 | 318 | | |
248 | 319 | | |
249 | 320 | | |
| |||
| 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 | + | |
| 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 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
0 commit comments