Skip to content

fix(js): let the anonymize worker exit after it replies - #271

Merged
karthiksathishjeemain merged 1 commit into
healthyinc:devfrom
Somya-jain0811:fix/js-anonymize-worker-exit
Nov 28, 2026
Merged

karthiksathishjeemain merged 1 commit into
healthyinc:devfrom
Somya-jain0811:fix/js-anonymize-worker-exit

Conversation

@Somya-jain0811

Copy link
Copy Markdown

what was wrong

every upload to /api/anonymize starts a new worker thread, and that worker never
exits. anonymizeWorker.js listens with parentPort.on("message", ...), which keeps
the thread alive, and the controller never terminates it. the worker only ever
handles one message, so each request leaves a thread and its heap behind.

repro on dev (start the server, then send 16 csv uploads one after another to
/api/anonymize, watching the node process):

idle: 84 MB, 13 threads
after 16 uploads: 598 MB, 33 threads, nothing is released

what i changed

parentPort.once instead of parentPort.on in the worker, so the listener goes away after
the one message and the worker exits by itself with code 0. i didn't call
worker.terminate() in the controller: it exits with code 1, and the existing exit handler
logs that as an error on every successful request.

added tests/controllers/anonymizeWorkerExit.test.js: it watches the worker the controller
starts and checks it exits with code 0, for a normal upload and for a file that can't be read
(the 400 path).

how i tested

  • new test without the fix (worker file reverted): 2 failing, "still running"
  • new test with the fix: 2 passing
  • full suite on node 18.20.8 (TZ=UTC) and node 24: 109 passing
  • same 16 uploads with the fix: 83 MB / 13 threads -> 111 MB / 17 threads, no error lines in the server log
  • prettier --check on the committed files: clean

note: the new test is in tests/controllers, so npm test won't pick it up until its
file list includes that folder. run it with
npx mocha tests/controllers/anonymizeWorkerExit.test.js --exit from javascript_backend

the worker listened with parentPort.on, so it never exited and every
upload left a thread behind. it only handles one message, so use once
and let it exit on its own.
@karthiksathishjeemain
karthiksathishjeemain merged commit d10a950 into healthyinc:dev Nov 28, 2026
2 of 4 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