Skip to content

Releasing napi_threadsafe_function during napi_add_async_cleanup_hook causes segfault #37201

Closed

Description

@laverdet

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

  1. added
    bug
    on Aug 8, 2026
  2. robobun commented on Aug 8, 2026

    @robobun
    Collaborator

    ✅ 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_hook read freed memory (segfault at 0x88).
    Fix ready for review → #37204 (CI green apart from a known-flaky unrelated test, see PR)

  3. added a commit that references this issue on Aug 9, 2026
    0c35ccc
  4. added a commit that references this issue on Aug 9, 2026
    6fd5e90
  5. added a commit that references this issue on Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugneeds triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions