fix: terminate worker threads after block evaluation in policy-service#6075
Open
vshvets-bc wants to merge 1 commit into
Open
fix: terminate worker threads after block evaluation in policy-service#6075vshvets-bc wants to merge 1 commit into
vshvets-bc wants to merge 1 commit into
Conversation
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.
Pyatakov
requested changes
May 27, 2026
| // isolate is released. Without this the worker thread stays alive | ||
| // after the script body returns, leaking ~30 MB per transformation. | ||
| const cleanup = () => { worker.terminate().catch(() => { /* noop */ }); }; | ||
|
|
Contributor
There was a problem hiding this comment.
No 'exit' handler. Additional code block needs to be added in case if a worker exits without emitting 'done' and without emitting 'error' (e.g., process.exit(0) inside the worker, OOM-kill, or any unhandled internal throw that terminates the isolate)
Suggested change
| worker.on('exit', (code) => { | |
| cleanup(); | |
| if (code !== 0 && code !== null) { | |
| reject(new Error(`Data transformation worker exited with code ${code}`)); | |
| } | |
| }); |
| // Terminate the worker after the final result / on error so the V8 | ||
| // isolate is released. Without this the worker thread stays alive | ||
| // after the script body returns, leaking ~30 MB per transformation. | ||
| const cleanup = () => { worker.terminate().catch(() => { /* noop */ }); }; |
Contributor
There was a problem hiding this comment.
Please remove '/* noop */' here and in the two other places
Comment on lines
+53
to
+55
| // Terminate the worker after the final result / on error so the V8 | ||
| // isolate is released. Without this the worker thread stays alive | ||
| // after the script body returns, leaking ~30 MB per transformation. |
Contributor
There was a problem hiding this comment.
One-line comment is enough, please replace here and in the other two places with:
// Release the worker's V8 isolate; without this each invocation leaks ~30 MB.
| @@ -87,15 +84,21 @@ export class MathBlock { | |||
| return new Promise<IPolicyDocument>(async (resolve, reject) => { | |||
| const workerFile = path.join(path.dirname(filename), '..', 'helpers', 'workers', 'math-worker.js'); | |||
| const worker = new Worker(workerFile, { workerData }); | |||
| // Terminate the worker once it finishes so the V8 isolate is released. | |||
| // Otherwise every formula evaluation leaks a worker thread (~30 MB). | |||
| const cleanup = () => { worker.terminate().catch(() => { /* noop */ }); }; | |||
Contributor
There was a problem hiding this comment.
Suggested change
| const cleanup = () => { worker.terminate().catch(() => { /* noop */ }); }; | |
| const cleanup = () => { worker.terminate().catch(() => {}); }; | |
| worker.on('exit', (code) => { | |
| cleanup(); | |
| if (code !== 0 && code !== null) { | |
| reject(new Error(`Math worker exited with code ${code}`)); | |
| } | |
| }); |
| // the V8 isolate is released. Without this the worker thread | ||
| // stays alive after the script body returns, leaking ~30 MB per | ||
| // custom-logic invocation. | ||
| const cleanup = () => { worker.terminate().catch(() => { /* noop */ }); }; |
Contributor
There was a problem hiding this comment.
Suggested change
| const cleanup = () => { worker.terminate().catch(() => { /* noop */ }); }; | |
| const cleanup = () => { worker.terminate().catch(() => {}); }; | |
| worker.on('exit', (code) => { | |
| cleanup(); | |
| if (code !== 0 && code !== null) { | |
| reject(new Error(`Custom logic worker exited with code ${code}`)); | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
Behavior unchanged for callers; resources are now released after each block evaluation.