Commit 69acdc2
authored
fix(core): truncate large error stacks and messages to prevent OOM (#3405)
## Summary
Large error stacks and messages can OOM the worker process when
serialized into OTel spans or `TaskRunError` objects. This was reported
when throwing an error with a massive `.stack` property from a chat
agent hook.
This adds frame-based stack truncation (similar to Sentry's approach)
plus message length limits, applied consistently across all error
serialization paths.
### What changed
**`packages/core/src/v3/errors.ts`**
- `truncateStack()` — parses `error.stack` into message lines + frame
lines, caps at 50 frames (keep top 5 closest to throw + bottom 45 entry
points, with "... N frames omitted ..." in between). Individual lines
capped at 1024 chars.
- `truncateMessage()` — caps error messages at 1000 chars
- Applied in `parseError()` and `sanitizeError()`
**`packages/core/src/v3/otel/utils.ts`**
- `sanitizeSpanError()` now uses `truncateStack` and `truncateMessage`
from `errors.ts` instead of duplicating truncation logic
- Non-Error values (strings, JSON) capped at 5000 chars
**`packages/core/src/v3/tracer.ts`**
- `startActiveSpan` catch block now delegates to `recordSpanException()`
instead of calling `span.recordException()` directly
### Limits
| What | Limit | Rationale |
|------|-------|-----------|
| Stack frames | 50 | Matches Sentry's `STACKTRACE_FRAME_LIMIT` |
| Top frames kept | 5 | Closest to throw site |
| Bottom frames kept | 45 | Entry points / framework frames |
| Per-line length | 1024 | Matches Sentry, prevents regex DoS |
| Message length | 1000 | Bounded but generous |
| Generic string (non-Error) | 5000 | Fallback for JSON/string errors in
spans |
## Test plan
- [x] 17 unit tests in `packages/core/test/errors.test.ts`
- [x] E2E: threw a 300-frame / 5000-char-message error in the ai-chat
reference app, verified truncated stack and message in span via
`get_span_details`
- [x] Verified the run survived the error (no OOM, continued waiting for
next message)1 parent 45ba398 commit 69acdc2
File tree
6 files changed
+444
-21
lines changed- .changeset
- packages/core
- src/v3
- otel
- test
6 files changed
+444
-21
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
154 | 154 | | |
155 | 155 | | |
156 | 156 | | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 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 | + | |
157 | 209 | | |
158 | 210 | | |
159 | 211 | | |
160 | 212 | | |
161 | 213 | | |
162 | | - | |
163 | | - | |
| 214 | + | |
| 215 | + | |
164 | 216 | | |
165 | 217 | | |
166 | 218 | | |
167 | 219 | | |
168 | 220 | | |
169 | 221 | | |
170 | 222 | | |
171 | | - | |
172 | | - | |
| 223 | + | |
| 224 | + | |
173 | 225 | | |
174 | 226 | | |
175 | 227 | | |
| |||
248 | 300 | | |
249 | 301 | | |
250 | 302 | | |
251 | | - | |
| 303 | + | |
252 | 304 | | |
253 | 305 | | |
254 | 306 | | |
255 | 307 | | |
256 | 308 | | |
257 | | - | |
| 309 | + | |
258 | 310 | | |
259 | | - | |
| 311 | + | |
260 | 312 | | |
261 | 313 | | |
262 | 314 | | |
263 | 315 | | |
264 | 316 | | |
265 | | - | |
| 317 | + | |
266 | 318 | | |
267 | 319 | | |
268 | 320 | | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
269 | 329 | | |
270 | 330 | | |
271 | | - | |
| 331 | + | |
272 | 332 | | |
273 | 333 | | |
274 | 334 | | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
275 | 338 | | |
276 | 339 | | |
277 | 340 | | |
278 | | - | |
279 | | - | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
280 | 349 | | |
281 | 350 | | |
282 | 351 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 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 | + | |
2 | 28 | | |
3 | 29 | | |
4 | 30 | | |
5 | 31 | | |
6 | 32 | | |
7 | | - | |
| 33 | + | |
8 | 34 | | |
9 | | - | |
| 35 | + | |
10 | 36 | | |
11 | 37 | | |
12 | 38 | | |
13 | 39 | | |
14 | 40 | | |
15 | 41 | | |
16 | | - | |
17 | | - | |
| 42 | + | |
18 | 43 | | |
19 | | - | |
| 44 | + | |
20 | 45 | | |
21 | 46 | | |
22 | 47 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
145 | 145 | | |
146 | 146 | | |
147 | 147 | | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
| 148 | + | |
153 | 149 | | |
154 | 150 | | |
155 | 151 | | |
| |||
0 commit comments