Skip to content

ffi: do not abort when a Worker stops in a callback - #66389

Open
trivikr wants to merge 1 commit into
nodejs:mainfrom
trivikr:ffi-stopping-worker-in-callback
Open

trivikr wants to merge 1 commit into
nodejs:mainfrom
trivikr:ffi-stopping-worker-in-callback

Conversation

@trivikr

@trivikr trivikr commented Sep 29, 2026

Copy link
Copy Markdown
Member

Fixes: #66388

Stopping a Worker while it is running an FFI callback aborted the whole process with "Callbacks cannot throw an exception". This happened on worker.terminate(), on process.exit() inside the callback, and when the main thread exited while the Worker was in a callback, since exit terminates all Workers.

All three stop the Worker by terminating execution, and InvokeCallback treated the termination as a thrown exception. Check HasTerminated() first and return a zeroed result so the native caller can unwind. Callbacks that throw still abort.


Assisted-by: claude:opus-5.5

Stopping a Worker while it is running an FFI callback aborted the whole
process with "Callbacks cannot throw an exception". This happened on
worker.terminate(), on process.exit() inside the callback, and when the
main thread exited while the Worker was in a callback, since exit
terminates all Workers.

All three stop the Worker by terminating execution, and InvokeCallback
treated the termination as a thrown exception. Check HasTerminated()
first and return a zeroed result so the native caller can unwind.
Callbacks that throw still abort.

Signed-off-by: Trivikram Kamat <16024985+trivikr@users.noreply.github.com>
Assisted-by: claude:opus-5.5
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/ffi

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Sep 29, 2026
@trivikr trivikr added ffi Issues and PRs related to experimental Foreign Function Interface support. request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. labels Sep 29, 2026
@codecov

codecov Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.35%. Comparing base (296584b) to head (48e261d).
⚠️ Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
src/node_ffi.cc 80.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #66389      +/-   ##
==========================================
- Coverage   90.36%   90.35%   -0.02%     
==========================================
  Files         792      792              
  Lines      275490   275495       +5     
  Branches    52796    52792       -4     
==========================================
- Hits       248954   248925      -29     
- Misses      16936    16985      +49     
+ Partials     9600     9585      -15     
Files with missing lines Coverage Δ
src/node_ffi.cc 72.45% <80.00%> (+0.04%) ⬆️

... and 30 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread src/node_ffi.cc
Comment on lines +771 to +774
// Return a zeroed result and let the caller unwind.
if (try_catch.HasTerminated()) {
if (ret != nullptr && cb->return_type->size > 0) {
std::memset(ret, 0, GetFFIReturnValueStorageSize(cb->return_type));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, this could still cause a segfault if the caller expects a non-null pointer and dereferences it. If so, could we clarify this limitation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. ffi Issues and PRs related to experimental Foreign Function Interface support. needs-ci PRs that need a full CI run. request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ffi: stopping a Worker while it is in a callback aborts the process

3 participants