Files
bun.sh/test/napi/napi-app/main.cpp
Claude Bot 3a625157b0 Fix NAPI crash when calling napi_create_object during GC
Previously, calling NAPI functions like napi_create_object from a finalizer
during garbage collection would trigger a NAPI_RELEASE_ASSERT that crashes
the process with SIGTRAP.

The fix changes the behavior to return napi_generic_failure instead of
crashing when GC-unsafe NAPI functions are called during GC. This allows
buggy NAPI modules to fail gracefully instead of taking down the entire
process.

Changes:
- Modified checkGC() to return bool instead of asserting
- Updated NAPI_CHECK_ENV_NOT_IN_GC macro to return error on GC detection
- Added test case to reproduce and verify the fix

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-23 08:04:23 +00:00

49 lines
1.4 KiB
C++

#include "napi_with_version.h"
#include "async_tests.h"
#include "class_test.h"
#include "conversion_tests.h"
#include "gc_crash_test.h"
#include "get_string_tests.h"
#include "js_test_helpers.h"
#include "standalone_tests.h"
#include "wrap_tests.h"
namespace napitests {
Napi::Value RunCallback(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
// this function is invoked without the GC callback
Napi::Function cb = info[0].As<Napi::Function>();
return cb.Call(env.Global(), {Napi::String::New(env, "hello world")});
}
Napi::Object Init2(Napi::Env env, Napi::Object exports) {
return Napi::Function::New(env, RunCallback);
}
Napi::Object InitAll(Napi::Env env, Napi::Object exports1) {
// check that these symbols are defined
auto *isolate = v8::Isolate::GetCurrent();
Napi::Object exports = Init2(env, exports1);
node::AddEnvironmentCleanupHook(isolate, [](void *) {}, isolate);
node::RemoveEnvironmentCleanupHook(isolate, [](void *) {}, isolate);
register_standalone_tests(env, exports);
register_async_tests(env, exports);
register_class_test(env, exports);
register_js_test_helpers(env, exports);
register_wrap_tests(env, exports);
register_conversion_tests(env, exports);
register_get_string_tests(env, exports);
InitGCCrashTest(env, exports);
return exports;
}
NODE_API_MODULE(napitests, InitAll)
} // namespace napitests