Repository navigation
Commit 028a5fa
committed
fix: enforce the grep_file timeout, and stop stripping the pattern
Two defects in GrepFileTool, combined because the test for the second
depends on the harness the first one forced.
1. The 30s timeout could not fire.
The match ran in a ThreadPoolExecutor, but `re` does not release the GIL
while matching. A catastrophic pattern therefore froze every thread in the
interpreter, the event loop included, so asyncio.wait_for never got
scheduled to time anything out. Reproduced with `(a+)+$` against 32 "a"s
plus "!": a 0.5s timeout did not fire and an independent 50ms heartbeat
stopped dead until the process was killed. The tool's error message
("pattern took too long to match") was therefore unreachable in exactly
the case it described.
The match now runs in a single-use worker process, which has its own GIL,
so the loop stays responsive and the deadline is honored. Unlike a thread,
the runaway can also be killed, and it is -- otherwise the deadline would
bound only the caller's wait while the worker kept burning a core.
A per-call pool rather than a shared one: a shared pool saves ~100ms of
spawn per call but forces a timeout to recycle shared state, killing
workers belonging to unrelated greps and surfacing BrokenProcessPool to
callers that did nothing wrong. At ~100ms inside a multi-second model turn
that trade is not worth making.
2. The pattern was stripped before use.
args.get("pattern", "").strip() turned "foo " into "foo", which matches
"foobar" -- text the caller's pattern excludes. Whitespace is meaningful
in a regex. Trim only to decide emptiness and search with what was asked
for, which is the split skills.go already makes, so the two runtimes no
longer disagree on identical input.
The existing timeout test faked slowness with time.sleep, which *releases*
the GIL, so it passed while the real hazard went unguarded. It now runs a
genuinely pathological match and asserts the event loop kept ticking. A
second, fast test asserts the work leaves this process, because the
behavioral test hangs rather than fails if the offload regresses to a thread
and there is no pytest-timeout plugin here to cut it short.
Tests that patched grep_content were rewritten against real files: a
MagicMock cannot be pickled to a worker process, and the child imports the
real module regardless.
Reported by EItanya in review of #2267.
Signed-off-by: brandonkeung <[email protected]>1 parent 0b0bfe8 commit 028a5fa
2 files changed
Lines changed: 219 additions & 48 deletions
File tree
- python/packages/kagent-adk
- src/kagent/adk/tools
- tests/unittests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
| 14 | + | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
| |||
32 | 34 | | |
33 | 35 | | |
34 | 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 | + | |
35 | 112 | | |
36 | 113 | | |
37 | 114 | | |
| |||
189 | 266 | | |
190 | 267 | | |
191 | 268 | | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
196 | 272 | | |
197 | 273 | | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | 274 | | |
210 | 275 | | |
211 | 276 | | |
| |||
245 | 310 | | |
246 | 311 | | |
247 | 312 | | |
248 | | - | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
249 | 318 | | |
250 | 319 | | |
251 | 320 | | |
252 | 321 | | |
253 | | - | |
| 322 | + | |
254 | 323 | | |
255 | 324 | | |
256 | 325 | | |
257 | 326 | | |
258 | 327 | | |
259 | 328 | | |
260 | 329 | | |
261 | | - | |
262 | | - | |
263 | | - | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
273 | 338 | | |
274 | | - | |
| 339 | + | |
275 | 340 | | |
276 | 341 | | |
277 | 342 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
3 | 6 | | |
4 | 7 | | |
5 | 8 | | |
6 | | - | |
| 9 | + | |
7 | 10 | | |
8 | | - | |
| 11 | + | |
9 | 12 | | |
10 | 13 | | |
11 | 14 | | |
| |||
18 | 21 | | |
19 | 22 | | |
20 | 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 | + | |
21 | 52 | | |
22 | | - | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
23 | 62 | | |
24 | 63 | | |
25 | | - | |
| 64 | + | |
26 | 65 | | |
27 | 66 | | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
28 | 70 | | |
29 | | - | |
| 71 | + | |
| 72 | + | |
30 | 73 | | |
31 | | - | |
32 | | - | |
| 74 | + | |
33 | 75 | | |
34 | | - | |
35 | | - | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
36 | 81 | | |
37 | | - | |
| 82 | + | |
| 83 | + | |
38 | 84 | | |
39 | | - | |
| 85 | + | |
40 | 86 | | |
41 | 87 | | |
| 88 | + | |
| 89 | + | |
42 | 90 | | |
43 | 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 | + | |
44 | 149 | | |
45 | 150 | | |
46 | 151 | | |
47 | | - | |
| 152 | + | |
| 153 | + | |
48 | 154 | | |
49 | 155 | | |
50 | | - | |
| 156 | + | |
51 | 157 | | |
52 | 158 | | |
53 | 159 | | |
54 | 160 | | |
55 | | - | |
| 161 | + | |
56 | 162 | | |
57 | | - | |
| 163 | + | |
58 | 164 | | |
59 | 165 | | |
60 | 166 | | |
61 | | - | |
62 | | - | |
| 167 | + | |
| 168 | + | |
0 commit comments