Repository navigation
Conversation
dave-365
commented
Nov 3, 2026
Problem BackgroundFixes: #1109 During application hot reload, pending This is particularly relevant to developers using Related discussion: [Issue #1109](#1109) Proposed SolutionThis PR introduces environment cleanup handling for The intention is to prevent event-loop hangs and improve application stability during hot reload. ImpactThis fix may benefit developers using Review RequestCould the maintainers please review the proposed cleanup approach and provide feedback on any lifecycle or thread-safety concerns? |
|
Hi @dave-365 , Is there any way to include a test in this PR? We have some tests already that check for various behavior in terminating environments, e.g. https://github.com/nodejs/node-addon-api/blob/main/test/error_terminating_environment.js . Maybe you can follow this pattern? |
@KevinEady Okay I will include test in this PR. |
Hi @KevinEady I have addressed the review feedback and added automated tests following the pattern in
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1759 +/- ##
==========================================
- Coverage 63.64% 63.20% -0.44%
==========================================
Files 3 3
Lines 2071 2090 +19
Branches 738 753 +15
==========================================
+ Hits 1318 1321 +3
- Misses 161 166 +5
- Partials 592 603 +11 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
"Hi @KevinEady , I have pushed a fix for the Windows CI failure in commit f4d293c (properly awaiting worker termination and removing script evaluation from the finalizer). Could you please approve the workflow run when you have a moment? Thank you!" |
|
Not sure if the failure was due to the code, since it looks like it was failing while building, but re-ran anyway |
|
|
@KevinEady |
…clean hool after free and no use, test afainst cpp bingidn noe and nodeexcept and onenvi tear down duplicate remvoal and while terminative napi cancelled no call onOk and OnError function
8b08f6b to
86d9a07
Compare
@KevinEady I was rebased this branch and latest code pushed. Can you try now. |
|
@KevinEady can you proceed workflow run. |
|
@KevinEady All checks passed now. If you any comments I fix that either can you proceed for merge. |
…and ensure safe destruction
|
@KevinEady all the comments were fixed. |
|
Hi @KevinEady Can you run workflow i was fixed everything and sorry for the delay i was sick. |
@KevinEady Can you proceed. friendly reminder. |
|
@KevinEady all the questions resolved and I fix the git check linux and unix double check failure can you approve workflow run. |
|
@KevinEady I have fixed the git CI check issue Linux and UNIX platform so kindly approve the workflow run. |
|
@dave-365 why did you need to go back to the |
@KevinEady Yes I we need this due to linus and unix has double-release so premature destruction so it completely fails there. So apply and npm test local that's completely works fine without any error. That's why my git ci fails so many times each time you apprve after that's why I apply this. |
@KevinEady Earlier we use that time git CI all check's passed. |
|
@dave-365 but why is it necessary? When is it the case that, within |
@KevinEady This happens when worker.terminate() is called and the worker finishes Execute() right as teardown begins: worker.terminate() immediately puts the Environment / V8 Isolate into a terminating state where JS can no longer run (napi_cannot_run_js / napi_pending_exception), before Environment::RunCleanup() runs OnEnvironmentTeardown. If OnWorkComplete -> _tsfn.Release() -> OnThreadSafeFunctionFinalize runs on the event loop before OnEnvironmentTeardown executes (or before our cleanup hook runs), _teardown_called is still false and _complete_status is napi_ok, so status != napi_cancelled. Without probing via napi_call_function to downgrade status to napi_cancelled, AsyncWorker::OnWorkComplete attempts to run OnOK()/OnError() and call into JS on a terminating environment. (On a healthy environment, calling undefined returns napi_function_expected, leaving status untouched.) |
@KevinEady Can we proceed further ? |
|
@dave-365 I will need to discuss with the team, because technically the async worker did complete its work, so passing |
@KevinEady Thanks, I am waiting for your response. |
|
Hi @dave-365 , We discussed this in the Node-API meeting on 27 Nov 2026. Our consensus is that node-addon-api should not hide the fact that an add-on cannot call into JavaScript by passing Our idea is that:
Then, the Markdown documentation should be updated, stating that it is possible that Does this make sense to you? |
…ync-worker-hot-reload
…ancelled handles napi cannnot run js and eoor()
@KevinEady Yes It's worked right now the double release checks pass without compromising onOk and onError. Can you proceed further. |
|
@KevinEady Can you proceed for further merging process. |
@KevinEady gentle reminder. |
|
Hi @dave-365 , There is a crash when compiling against Node-API version 9: I guess it's because you can't do this check for |
|
Hi @dave-365 , Another serious crash is if the worker calls I added this test case in cd3731a |
|
Hi @dave-365 , Another finding: the env teardown hook is not really what solves the problem you mention. The fix really comes from handling the Check this diff, which incorporates your changes to OnOK / OnError handling diff --git a/napi-inl.h b/napi-inl.h
index 9424fd0..fc7b99a 100644
--- a/napi-inl.h
+++ b/napi-inl.h
@@ -5772,16 +5772,50 @@ inline void AsyncWorker::SuppressDestruct() {
_suppress_destruct = true;
}
+namespace details {
+inline void CallAsyncWorkerCallback(napi_env env,
+ napi_value recv,
+ napi_value func,
+ size_t argc,
+ const napi_value* argv) {
+ napi_value result;
+ napi_status status = napi_call_function(env, recv, func, argc, argv, &result);
+#if (NAPI_VERSION >= 10)
+ if (status == napi_cannot_run_js) {
+ return;
+ }
+#else
+ // Before Node-API 10, a terminating environment reports
+ // napi_pending_exception even though no exception is pending.
+ if (status == napi_pending_exception) {
+ bool is_exception_pending = false;
+ if (napi_is_exception_pending(env, &is_exception_pending) == napi_ok &&
+ !is_exception_pending) {
+ return;
+ }
+ }
+#endif
+ NAPI_THROW_IF_FAILED_VOID(env, status);
+}
+} // namespace details
+
inline void AsyncWorker::OnOK() {
if (!_callback.IsEmpty()) {
- _callback.Call(_receiver.Value(), GetResult(_callback.Env()));
+ napi_env env = _callback.Env();
+ HandleScope scope(env);
+ std::vector<napi_value> args = GetResult(env);
+ details::CallAsyncWorkerCallback(
+ env, _receiver.Value(), _callback.Value(), args.size(), args.data());
}
}
inline void AsyncWorker::OnError(const Error& e) {
- if (!_callback.IsEmpty()) {
- _callback.Call(_receiver.Value(),
- std::initializer_list<napi_value>{e.Value()});
+ if (!_callback.IsEmpty() && !e.IsEmpty()) {
+ napi_env env = _callback.Env();
+ HandleScope scope(env);
+ napi_value arg = e.Value();
+ details::CallAsyncWorkerCallback(
+ env, _receiver.Value(), _callback.Value(), 1, &arg);
}
}
@@ -5829,7 +5863,16 @@ inline void AsyncWorker::OnWorkComplete(Napi::Env env, napi_status status) {
if (_error.size() == 0) {
OnOK();
} else {
- OnError(Error::New(_env, _error));
+ Error err;
+ napi_value str;
+ if (napi_create_string_utf8(_env, _error.data(), _error.size(), &str) ==
+ napi_ok) {
+ napi_value error;
+ if (napi_create_error(_env, nullptr, str, &error) == napi_ok) {
+ err = Error(_env, error);
+ }
+ }
+ OnError(err);
}
return nullptr;
});This minimal diff correctly passes all the test cases in your |
|
@KevinEady I covered the all issue ( node < 10, SuppressDestruct(), git diff) can you please check and proceed further. |
