Repository navigation
Closed
Labels
Description
What version of Bun is running?
1.4.0-canary.1+52bf09cb1
What platform is your computer?
Linux 6.12.76-linuxkit aarch64 unknown
What steps can reproduce the bug?
addon.c
#define NAPI_EXPERIMENTAL
#include <node_api.h>
#include <stdio.h>
#include <stdlib.h>
static napi_async_cleanup_hook_handle hook_handle;
static napi_threadsafe_function tsfn;
static void async_cleanup_hook(napi_async_cleanup_hook_handle handle, void* arg) {
fprintf(stderr, "[addon] async cleanup hook fired\n");
napi_release_threadsafe_function(tsfn, napi_tsfn_release);
}
static void tsfn_finalize(napi_env env, void* data, void* hint) {
fprintf(stderr, "[addon] tsfn finalize: removing async cleanup hook\n");
napi_remove_async_cleanup_hook(hook_handle);
fprintf(stderr, "[addon] async cleanup hook removed\n");
}
static void call_js_cb(napi_env env, napi_value js_cb, void* ctx, void* data) {}
static napi_value noop(napi_env env, napi_callback_info info) {
return NULL;
}
static napi_value start(napi_env env, napi_callback_info info) {
napi_value name;
napi_create_string_utf8(env, "repro", NAPI_AUTO_LENGTH, &name);
napi_value js_cb;
napi_create_function(env, "noop", NAPI_AUTO_LENGTH, noop, NULL, &js_cb);
napi_create_threadsafe_function(env, js_cb, NULL, name, 0, 1, NULL, tsfn_finalize, NULL, call_js_cb, &tsfn);
napi_unref_threadsafe_function(env, tsfn);
napi_add_async_cleanup_hook(env, async_cleanup_hook, NULL, &hook_handle);
return NULL;
}
NAPI_MODULE_INIT() {
napi_value fn;
napi_create_function(env, "start", NAPI_AUTO_LENGTH, start, NULL, &fn);
napi_set_named_property(env, exports, "start", fn);
return exports;
}repro.js
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
require("./addon.node").start();
console.log("main done");results
$ cc -Wall -shared -fPIC -O2 -I /opt/include/node -o addon.node addon.c
// [...]
$ node repro.js
main done
[addon] async cleanup hook fired
[addon] tsfn finalize: removing async cleanup hook
[addon] async cleanup hook removed
$ bun --version
1.3.14
$ bun repro.js
main done
[addon] async cleanup hook fired
$ bun upgrade --canary
// [...]
$ bun repro.js
main done
[addon] async cleanup hook fired
[addon] tsfn finalize: removing async cleanup hook
============================================================
Bun Canary v1.4.0-canary.1 (52bf09cb1) Linux arm64
Linux Kernel v6.12.76 | glibc v2.42
CPU: neon fp aes crc32 atomics
Args: "bun" "repro.js"
Features: jsc tsconfig(2) process_dlopen
Builtins: "bun:main" "node:module"
Elapsed: 22ms | User: 6ms | Sys: 17ms
RSS: 29.78 MB | Peak: 29.32 MB | Commit: 58.98 MB | Faults: 1 | Machine: 50.43 GB
panic(main thread): Segmentation fault at address 0x88
oh no: Bun has crashed. This indicates a bug in Bun, not your code.
To send a redacted crash report to Bun's team,
please file a GitHub issue using the link below:
https://bun.report/1.4.0/La252bf09cgiEggggC+hzwhCutFm2w7zCu6752Cu40whCmh5q9Cu5qmxC2/gi2C2mn8wBm3l0wCmw0zxCmw9k1Cm23l1C+uqi1C+yx0sB2xyI__A2AwI
Segmentation fault bun repro.js
What is the expected behavior?
I wouldn't expect it to segfault.
What do you see instead?
It segfaults.
Additional information
No response
Activity
robobun commentedon Aug 8, 2026
✅ Reproduced: env teardown freed the async cleanup hook handle as soon as the hook returned, so the tsfn finalizer's later
napi_remove_async_cleanup_hookread freed memory (segfault at 0x88).Fix ready for review → #37204 (CI green apart from a known-flaky unrelated test, see PR)
Pin bun & deno to working releases
ci: pin deno, remove bun
napi: keep async cleanup hook handles alive until the addon removes t…
napi: keep async cleanup hook handles alive until the addon removes t…
Reenable Bun tests
test: note what the #37201 fixture covers now
napi: wait for napi_remove_async_cleanup_hook before tearing down the…
napi: wait for napi_remove_async_cleanup_hook before tearing down the…
napi: wait for async cleanup hooks to finish before tearing down the env