Commit 17f7ed0
committed
fix: harden dynamic tool handlers against deadlock, hangs, and runaway output
Four latent liveness/stability bugs on the MCP dynamic-tool execution path
would silently hang agent turns or crash the container. None surfaced visible
errors, which made them the worst kind of bug: the agent just stopped.
1. Pipe-buffer deadlock: executeShellHandler and executeScriptHandler drained
stdout then stderr sequentially. Any handler writing >64KB to stderr before
closing stdout (curl -v, git clone, npm install, verbose loggers) blocked
on its next stderr write while phantom waited for stdout EOF forever.
Fix: Promise.all over both streams via a new readStreamWithCap helper.
2. No subprocess timeout: Bun.spawn ran with no kill path. A hung handler
froze the agent turn indefinitely with no recovery. Fix: drainProcessWithLimits
schedules SIGTERM at HANDLER_TIMEOUT_MS (default 60s, env-overridable via
PHANTOM_DYNAMIC_HANDLER_TIMEOUT_MS) and escalates to SIGKILL after a 2s
grace. Timeouts report partial stderr so the agent has actionable signal.
3. No stdout/stderr size cap: new Response(stream).text() slurped unbounded
output, risking OOM of the 2GB container. Fix: readStreamWithCap enforces
a 1MB cap by default (PHANTOM_DYNAMIC_HANDLER_MAX_OUTPUT_BYTES), appends a
clear truncation notice, and continues draining-to-void so the child never
blocks on a full pipe buffer.
4. DynamicToolRegistry.registerAllOnServer had no per-tool guard. One tool
with a bad inputSchema would throw during the loop and silently skip every
subsequent tool on every agent query (MCP factory pattern recreates servers
per query). Fix: per-tool try/catch, warn with tool name, continue. Broken
tools are not auto-unregistered; the operator decides.
buildSafeEnv and the --env-file= pattern in executeScriptHandler are
unchanged, preserving the subprocess environment isolation boundary from
SECURITY.md. Tests spawn real subprocesses and include a 200KB-stderr
regression test that would hang under the old sequential-drain code.
Env-var cleanup in the new tests uses Reflect.deleteProperty(process.env, ...)
rather than `delete` (Biome noDelete) or `= undefined` (coerces to the string
"undefined" on process.env and does not actually unset the key). This matches
the pattern acknowledged as correct by the maintainer in #5.1 parent 58571e1 commit 17f7ed0
4 files changed
Lines changed: 308 additions & 15 deletions
File tree
- src/mcp
- __tests__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
159 | 159 | | |
160 | 160 | | |
161 | 161 | | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
162 | 271 | | |
163 | 272 | | |
164 | 273 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
215 | 215 | | |
216 | 216 | | |
217 | 217 | | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
218 | 269 | | |
219 | 270 | | |
220 | 271 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
4 | 18 | | |
5 | 19 | | |
6 | 20 | | |
| |||
16 | 30 | | |
17 | 31 | | |
18 | 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 | + | |
19 | 137 | | |
20 | 138 | | |
21 | 139 | | |
22 | 140 | | |
23 | 141 | | |
24 | 142 | | |
25 | 143 | | |
26 | | - | |
| 144 | + | |
27 | 145 | | |
28 | | - | |
| 146 | + | |
29 | 147 | | |
30 | 148 | | |
31 | 149 | | |
| |||
46 | 164 | | |
47 | 165 | | |
48 | 166 | | |
49 | | - | |
| 167 | + | |
| 168 | + | |
50 | 169 | | |
51 | 170 | | |
52 | 171 | | |
| |||
55 | 174 | | |
56 | 175 | | |
57 | 176 | | |
| 177 | + | |
| 178 | + | |
58 | 179 | | |
59 | 180 | | |
60 | 181 | | |
| |||
67 | 188 | | |
68 | 189 | | |
69 | 190 | | |
70 | | - | |
71 | | - | |
72 | | - | |
| 191 | + | |
73 | 192 | | |
74 | | - | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
75 | 198 | | |
76 | | - | |
| 199 | + | |
77 | 200 | | |
78 | 201 | | |
79 | 202 | | |
80 | 203 | | |
81 | 204 | | |
82 | 205 | | |
83 | 206 | | |
84 | | - | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
85 | 211 | | |
86 | 212 | | |
87 | 213 | | |
88 | 214 | | |
89 | 215 | | |
90 | 216 | | |
91 | | - | |
92 | | - | |
93 | | - | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
94 | 222 | | |
95 | | - | |
| 223 | + | |
96 | 224 | | |
97 | | - | |
| 225 | + | |
98 | 226 | | |
99 | 227 | | |
100 | 228 | | |
| |||
0 commit comments