Skip to content

fix(storage): stop OOM-killing studio-worker on large thread-message parts - #4426

Merged
pedrofrxncx merged 2 commits into
mainfrom
fix/thread-message-parts-oom
Jul 10, 2026
Merged

pedrofrxncx merged 2 commits into
mainfrom
fix/thread-message-parts-oom

Conversation

@pedrofrxncx

@pedrofrxncx pedrofrxncx commented Jul 10, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Traced last night's deco-studio-worker restart storm (55 restarts/24h, one pod OOMKilled 40x over ~3h) to SqlThreadMessagePartStorage.serializeParts: it JSON.stringifys every thread-message part synchronously, with no yielding and no size bound, whenever a run flushes parts (e.g. a large tool result / file read comes through).
  • serializeParts now yields to the event loop every 25 parts during a batch instead of doing the whole flush in one synchronous pass.
  • Added a 50MB per-payload circuit breaker as a last resort for truly pathological sizes only — normal large tool output (big file reads, big stdout) is still stored in full, not dropped.
  • This gap has existed unguarded since the stream-of-record feature was dark-shipped (d54b94ca9, June 7) — not a recent regression, just not exercised hard enough to surface until now.

Testing

  • bun test apps/mesh/src/storage/thread-message-parts.test.ts
  • bun run --cwd=apps/mesh check
  • bun run fmt

Summary by cubic

Prevents OOM kills and event-loop stalls in the studio worker when flushing large thread-message parts by yielding during serialization and adding a per-payload size guard that estimates size without stringifying.

  • Bug Fixes
    • Yield to the event loop every 25 parts in serializeParts to avoid long synchronous JSON.stringify loops.
    • Add a 50MB per-payload circuit breaker that estimates size via a cheap traversal and skips JSON.stringify for oversized payloads.
    • Bound the size estimator to avoid deep-input stalls.
    • Use serializePayload for both payload and metadata fields.
    • Preserve normal large tool outputs in full; add tests for small, large, and pathological cases.

Written for commit 7fd361b. Summary will update on new commits.

Review in cubic

…parts

serializeParts synchronously JSON.stringify'd every part with no yielding
and no upper bound, which has been repeatedly OOM-killing deco-studio-worker
in production (55 container restarts / 24h, one pod OOMKilled 40 times in
~3h) whenever a large tool result (e.g. a big file read) flows through.

Yield to the event loop every 25 parts during a batch so a large flush
doesn't block request handling for the whole call, and add a 50MB
per-payload circuit breaker as a last resort for truly pathological sizes
only — normal large tool output is still stored in full, not dropped.
…yloads

The circuit breaker still ran JSON.stringify on the full payload before
checking its size, so it only avoided storing an oversized result — not
the expensive/blocking serialization that actually caused the stall and
memory spike. Estimate size via a cheap traversal (string .length is O(1),
no copy) instead, so an oversized payload is caught before ever being
stringified.
@pedrofrxncx
pedrofrxncx enabled auto-merge (squash) July 10, 2026 01:58
@pedrofrxncx
pedrofrxncx merged commit d573de2 into main Jul 10, 2026
15 checks passed
@pedrofrxncx
pedrofrxncx deleted the fix/thread-message-parts-oom branch July 10, 2026 01:59
decocms Bot pushed a commit that referenced this pull request Jul 10, 2026
PR: #4426 fix(storage): stop OOM-killing studio-worker on large thread-message parts
Bump type: patch

- decocms (apps/mesh/package.json): 4.0.4 -> 4.0.5

Deploy-Scope: server
@pedrofrxncx

Copy link
Copy Markdown
Collaborator Author

Trace that led to this fix

Symptom (Prometheus, kube_pod_container_status_restarts_total / kube_pod_container_status_last_terminated_reason, namespace deco-studio):

  • 55 studio-worker container restarts in 24h.
  • One pod (deco-studio-worker-7b896f4965-d5nh8) alone was OOMKilled 40 times over ~3h (2026-07-09 21:22 → 00:37 UTC), across several different rollouts (image versions from 3.115.1 through 4.0.4) — not a regression from one deploy, a standing gap.
  • Memory curve per crash cycle (container_memory_working_set_bytes, 60s resolution): ~4MB at pod start → ~300–340MB within the first minute → OOMKilled (container limit is 2Gi — the actual peak spikes past that between scrape samples).

Root cause (VictoriaLogs, pod logs right before each kill):

event-loop-profiler
Slow db pool acquire — event-loop lag (not pool): { waitMs: 283.47, idleAtStart: 13, total: 14, max: 25 }
event-loop-stall
event-loop-stall-profile
Slow query measurement — likely event-loop lag, not slow SQL: {
  durationMs: 1245.5,
  eventLoopLagMs: 869,
  sql: "select ... from \"org_fs_entry\" where \"organization_id\" = $1 and \"volume\" = $2 and \"path\" = $3 and \"deleted_at\" is null",
  pool: { total: 14, idle: 4, waiting: 0, max: 25 }
}

event-loop-stall-profile stack (from lagMs: 604–850 samples on the same pod):

stringify
serializeParts  file:///.../cli.js:83138
appendParts     file:///.../cli.js:83138
appendBuiltRows file:///.../cli.js:83287
emitStepParts   file:///.../cli.js:83287
onStepFinish    file:///.../cli.js:83287
...

That maps onto SqlThreadMessagePartStorage.serializeParts (thread-message-parts.ts) → PartEmitter.appendBuiltRows/emitStepParts (part-emitter.ts) → appendPartsStep (background-tool-workflow.ts). serializeParts was JSON.stringify-ing every thread-message part synchronously with no yielding and no size bound — the org_fs_entry query right alongside it suggests a large file read flowing through as a tool result and getting fully stringified in one blocking call, spiking RSS past the container limit.

This unguarded path has existed since the stream-of-record feature was dark-shipped (d54b94ca9, 2026-06-07) — not introduced by a recent change, just not exercised hard enough to surface until this traffic pattern hit it.

tlgimenes pushed a commit that referenced this pull request Jul 16, 2026
…TS.md (#4475)

Mined ~300 merged PRs for the delta between the first PR proposal and the
work added before merge. The dominant shape: a first draft ships the happy
path, then a hardening pass (often a different engineer who takes over the
branch and merges — the vibecoder→engineer handoff) adds the same categories
every time. Encoded those 9 categories as an actionable first-pass checklist,
each citing a real PR (#4008, #4230, #4365, #4409, #4426, #4445, #4357, #4355,
#4449, #4350, #4373, #4416, #4446, #4461, #4134, #4469).

Covers: variant/edge-case handling, tenant/permission scoping, concurrency &
silent-data-loss, test-per-fix + invert-bug-tests + real-PG, dead-code/knip,
lifecycle symmetry + state reset, kill-switch/GC/git-exclude for risky infra,
input validation vs over-engineering, compile-time type-safety.

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant