Skip to content

Fix crash when reloading the addon in worker threads - #119

Open
CheckPickerUpper wants to merge 2 commits into
baudehlo:masterfrom
CheckPickerUpper:worker-reload-static-handles
Open

CheckPickerUpper wants to merge 2 commits into
baudehlo:masterfrom
CheckPickerUpper:worker-reload-static-handles

Conversation

@CheckPickerUpper

Copy link
Copy Markdown

Follow-up to #109.

Loading fs-ext in a worker after an earlier worker has exited can crash Node. The static Nan::Persistent handles for statVFS property names belong to the old worker's V8 isolate, and the next load tries to reset them after that isolate is gone.

This creates the property names when building each statVFS result. The regression loads the addon in the main thread, then in 20 workers one after another. The existing worker test also leaves log cleanup to the main thread, since a late-starting worker could delete its output.

The CI steps pass on Linux with Node 18.20.8, 20.20.2 and 22.23.5, including all 75 tests. The new regression fails with the old fs-ext.cc. Lint reports the same 17 warnings as master, with no errors.

worker-test.js removed worker-test.log at the top level, so the worker
thread removed it too when it started. If the worker took longer than the
main thread's 100 ms lock hold to start, it deleted the "releasing main
lock" line the main thread had already written, and the assertion failed
with only "worker lock acquired" in the log. Any other test that starts
workers in the same process slows worker startup enough to hit this.
init() stored the nine statVFS result keys in static Nan::Persistent
handles and reset them on every load. Static handles are shared by every
thread, but each handle belongs to the isolate that created it. When the
addon loads in a second worker after the first has exited, Reset() frees
a handle owned by an isolate that no longer exists, and V8 aborts with
"Check failed: node->IsInUse()".

The keys are now created with Nan::New() where the result object is
built, so no handle outlives the isolate that made it.

tests/worker-reload-test.js loads the addon in the main thread and then
in 20 workers one after another, collecting garbage after each exits.
Before this change it aborts with the check above; after it, it passes.
npm test passes (75 tests).
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