Title: serialization possible freezes the capturing thread · Issue #43 · getsentry/sentry-javascript-node-native-stacktrace · GitHub
Open Graph Title: serialization possible freezes the capturing thread · Issue #43 · getsentry/sentry-javascript-node-native-stacktrace
X Title: serialization possible freezes the capturing thread · Issue #43 · getsentry/sentry-javascript-node-native-stacktrace
Description: Environment Sentry SaaS. @sentry/node: 11.5.0 @sentry/profiling-node: 11.5.0 @sentry/node-native: 11.5.0 @sentry/node-native-stacktrace: 0.5.2 (latest) Node.js 24, Linux x64 (Google Cloud Run), reproduced on macOS arm64 eventLoopBlockInt...
Open Graph Description: Environment Sentry SaaS. @sentry/node: 11.5.0 @sentry/profiling-node: 11.5.0 @sentry/node-native: 11.5.0 @sentry/node-native-stacktrace: 0.5.2 (latest) Node.js 24, Linux x64 (Google Cloud Run), rep...
X Description: Environment Sentry SaaS. @sentry/node: 11.5.0 @sentry/profiling-node: 11.5.0 @sentry/node-native: 11.5.0 @sentry/node-native-stacktrace: 0.5.2 (latest) Node.js 24, Linux x64 (Google Cloud Run), rep...
Opengraph URL: https://github.com/getsentry/sentry-javascript-node-native-stacktrace/issues/43
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"serialization possible freezes the capturing thread","articleBody":"### Environment\n\n- Sentry SaaS.\n- @sentry/node: 11.5.0\n- @sentry/profiling-node: 11.5.0\n- @sentry/node-native: 11.5.0\n- @sentry/node-native-stacktrace: 0.5.2 (latest)\n- Node.js 24, Linux x64 (Google Cloud Run), reproduced on macOS arm64\n- `eventLoopBlockIntegration({ threshold: 500 })`\n\n### Steps to Reproduce\n\n`captureStackTrace()` serializes the thread's AsyncLocalStorage state without any limits. A small object graph with shared references is enough to freeze the captured thread and allocate GBs:\n\n```js\nimport { AsyncLocalStorage } from 'node:async_hooks';\nimport { Worker } from 'node:worker_threads';\nimport { registerThread } from '@sentry/node-native-stacktrace';\n\nconst DEPTH = Number(process.argv[2] ?? 22);\n// Each level references the next one twice: a few hundred bytes, 2^DEPTH paths\nlet state = { leaf: 'x' };\nfor (let i = 0; i \u003c DEPTH; i++) state = { a: state, b: state };\n\nconst als = new AsyncLocalStorage();\nregisterThread({ asyncLocalStorage: als });\n\nconst flags = new Int32Array(new SharedArrayBuffer(8));\nnew Worker(`\n const { workerData: { flags } } = require('node:worker_threads');\n const { captureStackTrace } = require('@sentry/node-native-stacktrace');\n Atomics.wait(flags, 0, 0);\n const t = Date.now();\n const trace = captureStackTrace();\n console.log('capture took', Date.now() - t, 'ms, asyncState JSON', JSON.stringify(trace['0']?.asyncState ?? null).length, 'chars');\n Atomics.store(flags, 1, 1);\n`, { eval: true, workerData: { flags } });\n\nals.run(state, () => {\n Atomics.store(flags, 0, 1);\n Atomics.notify(flags, 0);\n while (Atomics.load(flags, 1) === 0) {} // blocked event loop\n});\nconsole.log('maxRSS', Math.round(process.resourceUsage().maxRSS / 1024), 'MB');\n```\n\n| DEPTH | capture | peak RSS |\n|---|---|---|\n| 16 | 65 ms | 130 MB |\n| 20 | 1.1 s | 799 MB |\n| 22 | 5.0 s, then times out, `asyncState` is `null` | 2.3 GB |\n\n\n1. We have investigated several memory usage spikes and OOM Errors (a few per day) on one of our services.\n2. We found Sentry EventLoopBlocked Events for every OOM kill, which didn't have any stacktrace, so with the help of AI we found that this case happens, if Sentrys Module is not able to create a Stacktrace in time (5s)\n3. We let AI make testcases for complex data in AsyncLocalStorage and analyze the behaviour of SanitizeForJSON compared to libraries which handle the cases well.\n4. We created a fix living [here](https://github.com/johannespinger/sentry-javascript-node-native-stacktrace/tree/fix/bounded-async-state-serialization ), with which we patched the current sentry module for our service\n5. Since we deployed this fixed version (4 days ago) our OOMs are fixed.\n6. We didn't want to create a PR based on this branch, cause its AI-written, but I am open to contribute if you want the PR.\n\n### Expected Result\nSerializing the state takes bounded time and memory regardless of its shape (depth, width, shared references, large strings). Getters, proxies and `toJSON` do not throw into the interrupted application code.\n\n### Actual Result\n`SanitizeForJSON` (module.cc) has no limits on depth, breadth, total values or string size. Its cycle check only compares against the current ancestor path, so shared references are copied once per path (exponential), and it runs inside the interrupted thread. Effects:\n\n- the captured thread freezes for seconds and memory grows by GBs (see table)\n- deep chains such as a Node timer list crash V8 (`Check failed: isolate_->IsOnCentralStack()`)\n- exceptions from getters, revoked proxies or `toJSON` are thrown into the application\n\nIn production this hit us through `eventLoopBlockIntegration`: its rate limit allows one event per clock hour, so the first block after hh:00 triggered a capture of the request scope. Instances froze and were OOM-killed by Cloud Run at the full hour, 2–3 times a day. Every kill had a matching `EventLoopBlocked` event without a stack trace, because the 5 s wait ran out.\n\n### Fix\n\nWe patched the addon to bound the serialization (depth 10, 1,000 entries per object, 20,000 values, 1 MiB of strings, with markers where it cuts), read only own properties, and keep exceptions out of the app thread: https://github.com/johannespinger/sentry-javascript-node-native-stacktrace/tree/fix/bounded-async-state-serialization\n\nWe've run it in production since 2026-10-08, with zero OOM kills since (before: 13 in 6 days). It was written with AI. Happy to open a PR if the approach works for you.","author":{"url":"https://github.com/johannespinger","@type":"Person","name":"johannespinger"},"datePublished":"2026-10-12T10:57:47Z","url":"https://github.com/getsentry/sentry-javascript-node-native-stacktrace/issues/43"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:158abb35-23b8-914a-9ac8-1cd20a62acab |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C41A:29409B:2869A0:3885C2:6B04212A |
| html-safe-nonce | 175b4c5224b4f38dbbdba4fbbb16dc9357eab1d20452829502473a3e0bf0be9a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNDFBOjI5NDA5QjoyODY5QTA6Mzg4NUMyOjZCMDQyMTJBIiwidmlzaXRvcl9pZCI6IjIwMjAyMTY0MjQ0MjA4NzY1ODYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 8acbe183f237931cadf4461353c0c4618dca1cb8fe14f9529d762177d7770f31 |
| hovercard-subject-tag | issue:5812550172 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/getsentry/sentry-javascript-node-native-stacktrace/43/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2e16574d4e52932553fdd2523cbc9fe5b8b6c55f0df03ba7c1cb5ba9250430f4/getsentry/sentry-javascript-node-native-stacktrace/issues/43 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2e16574d4e52932553fdd2523cbc9fe5b8b6c55f0df03ba7c1cb5ba9250430f4/getsentry/sentry-javascript-node-native-stacktrace/issues/43 |
| og:image:alt | Environment Sentry SaaS. @sentry/node: 11.5.0 @sentry/profiling-node: 11.5.0 @sentry/node-native: 11.5.0 @sentry/node-native-stacktrace: 0.5.2 (latest) Node.js 24, Linux x64 (Google Cloud Run), rep... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | johannespinger |
| hostname | github.com |
| expected-hostname | github.com |
| None | f8d9f404447ab5df341c8a92cf5fc927730ad1321e64ac57656444d2438097f1 |
| turbo-cache-control | no-preview |
| go-import | github.com/getsentry/sentry-javascript-node-native-stacktrace git https://github.com/getsentry/sentry-javascript-node-native-stacktrace.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 990851629 |
| octolytics-dimension-repository_nwo | getsentry/sentry-javascript-node-native-stacktrace |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 990851629 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-javascript-node-native-stacktrace |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f7edccce04851591ee529948701779ce6819bc53 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width