Title: fix(memory): close app-service leak paths behind the per-task memory ramp by icecrasher321 · Pull Request #7056 · simstudioai/sim · GitHub
Open Graph Title: fix(memory): close app-service leak paths behind the per-task memory ramp by icecrasher321 · Pull Request #7056 · simstudioai/sim
X Title: fix(memory): close app-service leak paths behind the per-task memory ramp by icecrasher321 · Pull Request #7056 · simstudioai/sim
Description: Problem App-service memory climbs monotonically with task uptime and resets only on deploy, with the worst tasks approaching the task memory limit between deploys. MemoryTelemetry shows the growth lives in the main Next.js process (its RSS accounts for essentially the entire container ramp) — not the isolated-vm sandbox worker, which is a separate child process with a bounded pool (4 workers, retired at 200 executions) and finally-guaranteed isolate disposal on every path. Fixes Copilot stream teardown backstop (lib/copilot/request/lifecycle/start.ts) — the activeStreams registration, the 250 ms Redis abort poller, and the SSE keepalive were acquired outside the finally that releases them, so a throw that bypassed the ordered teardown (e.g. resetBuffer on a Redis blip at stream start) orphaned two immortal intervals and the registration for the life of the process. An idempotent, flag-guarded backstop in the orchestration's existing outer finally now releases them on every path. Client-visible error behavior is unchanged (the backstop deliberately does not close the stream), and the chat-stream lock release is ownership-guarded so a successor stream's lock can never be touched. Large-value cache sweep + idle-TTL LRU (lib/execution/payloads/cache.ts) — expiry was enforced only inside later cache calls, so a quieting instance held expired parsed object graphs indefinitely; a self-retiring, unref'd 60 s sweep now keeps the TTL honest. Lifetimes are also now idle-based (every authorized read refreshes expiry and moves the entry to the back of the eviction order), so a value a live run keeps referencing can never expire or be pressure-evicted mid-use — strictly fewer mid-execution misses. TTL values, the admission budget, and the sole-copy (recoverable: false) eviction protection are unchanged. BYOK rotation cursors (lib/api-key/byok.ts) — the tenant-keyed cursor Map had no delete/TTL/ceiling; now an LRUCache({ max: 10_000 }). Evicting an idle pool's cursor just restarts rotation at index 0, which the per-instance approximate-rotation contract already tolerates. Collab-doc jsdom singleton (lib/collab-doc/converter.ts) — the DOM guard read the bundled module's window binding while the install wrote globalThis.window (the same bundler mismatch next.config.ts documents for TipTap), so a runtime where the two disagree allocated a fresh multi-MB jsdom window on every conversion. Guard and install now both use globalThis, and the window is a module singleton either way. Telemetry (lib/monitoring/memory-telemetry.ts) — the Memory snapshot now includes largeValueCacheEntries/largeValueCacheTrackedMB (reads the JSON-vs-heap amplification directly against heapUsedMB) and detachedContexts. Verification 436 tests across 30 files (copilot session + lifecycle, payloads, collab-doc, byok, function-execute route, streaming), including new regression tests for the early-throw release and the retention policy. tsc and biome clean. A local before/after repro harness (env-gated vitest suites with synthetic load, run on pre-fix staging and on this branch; deliberately not committed) measured: Probe Pre-fix staging This branch 200 streams failing at session reset → registrations still alive 200 / 200 leaked 0 / 200 47 MB tracked (≈128 MB heap) cached, traffic stops, 16 min idle 0 MB reclaimed while idle 124 MB reclaimed during idle The harness also measured the JSON-bytes → retained-heap amplification of parsed graphs at 2.2–2.7×, i.e. the cache's 256 MB nominal budget pins a small multiple of that in real heap. Tightening that accounting changes admission semantics (more storeLargeValue failures when durable storage is down), so it is deliberately not part of this PR — the new telemetry sizes it first. 🤖 Generated with Claude Code
Open Graph Description: Problem App-service memory climbs monotonically with task uptime and resets only on deploy, with the worst tasks approaching the task memory limit between deploys. MemoryTelemetry shows the growth ...
X Description: Problem App-service memory climbs monotonically with task uptime and resets only on deploy, with the worst tasks approaching the task memory limit between deploys. MemoryTelemetry shows the growth ...
Opengraph URL: https://github.com/simstudioai/sim/pull/7056
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:74c288e6-d419-588b-a70e-94541495956c |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | E794:487EF:B3B8EB:F7B845:6AFB549E |
| html-safe-nonce | 4af0f56e8065060c1b558510e16f57d4d1583a59f00f7bfd1771d1005241c173 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNzk0OjQ4N0VGOkIzQjhFQjpGN0I4NDU6NkFGQjU0OUUiLCJ2aXNpdG9yX2lkIjoiODQxNTA2ODY5ODMwNTA1Nzk1MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ff4bc346a1c297ff1d4b291f83c8bc56e283cdcf2490058f9552ac8ab10b3959 |
| hovercard-subject-tag | pull_request:4353998566 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,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/simstudioai/sim/pull/7056/files |
| twitter:image | https://avatars.githubusercontent.com/u/17950982?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/17950982?s=400&v=4 |
| og:image:alt | Problem App-service memory climbs monotonically with task uptime and resets only on deploy, with the worst tasks approaching the task memory limit between deploys. MemoryTelemetry shows the growth ... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7122bb414b542dba14d71a3f0b9f6fd2abce715b9f5912d082800563e6c806d1 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/simstudioai/sim git https://github.com/simstudioai/sim.git |
| octolytics-dimension-user_id | 199344406 |
| octolytics-dimension-user_login | simstudioai |
| octolytics-dimension-repository_id | 912559512 |
| octolytics-dimension-repository_nwo | simstudioai/sim |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 912559512 |
| octolytics-dimension-repository_network_root_nwo | simstudioai/sim |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | cbf66c024bb6d10e6955baa7413c54cf13a201bb |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width