Skip to content

fix: terminate worker threads after block evaluation in policy-service#6075

Merged
Pyatakov merged 6 commits into
hashgraph:developfrom
Climission:fix/policy-memory-leaks
May 28, 2026
Merged

fix: terminate worker threads after block evaluation in policy-service#6075
Pyatakov merged 6 commits into
hashgraph:developfrom
Climission:fix/policy-memory-leaks

Conversation

@vshvets-bc

Copy link
Copy Markdown
Collaborator

Three policy blocks spawn worker_threads Workers via new Worker(...) but never call worker.terminate().
Node Worker threads do not auto-exit when the script body returns — they keep the V8 isolate alive (~30 MB each) waiting for more messages.
Every formula / custom-logic / data-transformation invocation leaked one worker thread. A heavy policy could accumulate multi-GB of leaked workers.

Adds a small cleanup() helper that calls worker.terminate() (with a swallowed .catch() for already-exited workers, e.g. via OOM-kill). Called on the terminating message, on error, and on every reject path.

  • math-block.ts: terminate on the single 'done' message + on error
  • custom-logic-block.ts (JS branch): terminate after the final 'done' (data.final === true) so debug messages are still forwarded for multi-emit custom-logic runs.
  • data-transformation-addon.ts: terminate after final result + on error

Behavior unchanged for callers; resources are now released after each block evaluation.

Three policy blocks spawn worker_threads Workers via `new Worker(...)`
but never call worker.terminate(). Node Worker threads do not auto-exit
when the script body returns — they keep the V8 isolate alive (~30 MB
each) waiting for more messages on the parentPort. Every formula /
custom-logic / data-transformation invocation leaked one worker thread.
A heavy policy could accumulate multi-GB of leaked workers over the
course of an MRV submission.

Adds a small cleanup() helper that calls worker.terminate() (with a
swallowed .catch() for already-exited workers, e.g. via OOM-kill).
Called on the terminating message, on error, and on every reject path.

- math-block.ts: terminate on the single 'done' message + on error
- custom-logic-block.ts (JS branch): terminate after the final 'done'
  (data.final === true) so debug messages are still forwarded for
  multi-emit custom-logic runs. Python branch already has timeout-based
  termination + an 'exit' handler and is left untouched.
- data-transformation-addon.ts: terminate after final result + on error

No public API change. Behavior unchanged for callers; resources are now
released after each block evaluation.
@vshvets-bc vshvets-bc requested review from a team as code owners May 14, 2026 11:00
@vshvets-bc vshvets-bc changed the title Fix policy-service: terminate worker threads after block evaluation Fix: terminate worker threads after block evaluation in policy-service. May 14, 2026
@Pyatakov Pyatakov changed the title Fix: terminate worker threads after block evaluation in policy-service. fix: terminate worker threads after block evaluation in policy-service May 17, 2026
@Pyatakov Pyatakov self-assigned this May 17, 2026
Comment thread policy-service/src/policy-engine/blocks/data-transformation-addon.ts Outdated
Comment thread policy-service/src/policy-engine/blocks/data-transformation-addon.ts Outdated
Comment thread policy-service/src/policy-engine/blocks/data-transformation-addon.ts Outdated
Comment thread policy-service/src/policy-engine/blocks/math-block.ts Outdated
Comment thread policy-service/src/policy-engine/blocks/custom-logic-block.ts Outdated
vshvets-bc and others added 5 commits May 28, 2026 11:42
Co-authored-by: Alex Piatakov <alexander.piatakov@gmail.com>
Signed-off-by: Volodymyr Shvets <54721244+vshvets-bc@users.noreply.github.com>
…don.ts

Co-authored-by: Alex Piatakov <alexander.piatakov@gmail.com>
Signed-off-by: Volodymyr Shvets <54721244+vshvets-bc@users.noreply.github.com>
Co-authored-by: Alex Piatakov <alexander.piatakov@gmail.com>
Signed-off-by: Volodymyr Shvets <54721244+vshvets-bc@users.noreply.github.com>
@Pyatakov Pyatakov merged commit d616dd7 into hashgraph:develop May 28, 2026
13 checks passed
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.

2 participants