Skip to content

Tried to use newrelic with Bun #22630

Closed
Closed

Description

@pal

How can we reproduce the crash?

I added newrelic to the project, added import 'newrelic'; to my test file and ran the following (in fish);

env NEW_RELIC_APP_NAME=myprojg NEW_RELIC_LICENSE_KEY=redacted bun test myfile.test.ts

Also tested in bash (without env first) with same result

Got this in console:

bun test v1.2.21 (7c45ed97)

myfile.test.ts:
============================================================
Bun v1.2.21 (7c45ed97) macOS Silicon
macOS v15.6.1
CPU: fp aes crc32 atomics
Args: "bun" "test" "myfile.test.ts"
Features: Bun.stderr(2) Bun.stdout(2) bunfig dotenv jsc transpiler_cache(4) tsconfig(3) tsconfig_paths
Builtins: "node:assert" "node:async_hooks" "node:buffer" "node:child_process" "node:crypto" "node:dns" "node:events" "node:fs" "node:fs/promises" "node:http" "node:https" "node:module" "node:net" "node:os" "node:path" "node:stream" "node:timers" "node:tls" "node:tty" "node:url" "node:util" "node:zlib" "node:worker_threads" "node:inspector" "node:diagnostics_channel"
Elapsed: 160ms | User: 155ms | Sys: 54ms
RSS: 103.25MB | Peak: 103.25MB | Commit: 1.07GB | Faults: 86 | Machine: 34.36GB

panic(main thread): Segmentation fault at address 0x0
Crashed while loading native module: /Users/pal/dev/redacted/node_modules/.bun/@newrelic+native-metrics@12.0.0/node_modules/@newrelic/native-metrics/prebuilds/darwin-arm64/@newrelic+native-metrics.abi137.uv1.armv8.glibc.node
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.2.21/Mt17c45ed94Bq0gQ___u22unB______A2AA

fish: Job 1, 'env NEW_RELIC_APP_NAME=redacted…' terminated by signal SIGTRAP (Trace or breakpoint trap)

Relevant log output

Stack Trace (bun.report)

Bun v1.2.21 (7c45ed9) on macos aarch64 [RunCommand]

Segmentation fault at address 0x00000000

Features: tsconfig, Bun.stderr, dotenv, jsc

Sentry Issue: BUN-DCM

Activity

  1. added
    crash
    on Sep 13, 2025
  2. linear commented on Sep 13, 2025

    @linear
  3. added
    macOS
    on Sep 13, 2025
  4. added
    napi
    and removed on Sep 13, 2025
  5. robobun commented on Mar 18, 2026

    @robobun
    Collaborator

    This was fixed in #24403.

    Issue Summary

    Segfault (at address 0x0) when loading @newrelic/native-metrics native .node module via process.dlopen. The crash occurred in node_module_register at src/bun.js/bindings/v8/node.cpp:95 (old line numbering).

    Root Cause

    In the original code, strongExportsObject was conditionally initialized: if module.exports was null, undefined, or a primitive value, the if (exportsObject && exportsObject.isObject()) guard would skip initialization. Later at line 97 (now line 97), *strongExportsObject was dereferenced unconditionally, causing a null pointer dereference (segfault at address 0x0).

    Fix Applied

    Two commits fixed this and related issues:

    1. c7b9e0dc92 (PR fix(node): prevent crash with null/undefined exports in process.dlopen #24403): "fix(node): prevent crash with null/undefined exports in process.dlopen"

      • Changed getIfPropertyExists() to get()
      • Uses toObject() to guarantee strongExportsObject is always initialized
      • Properly throws TypeError for null/undefined (matching Node.js behavior)
    2. 2dd997c4b5 (PR fix(node): support duplicate dlopen calls with DLHandleMap #24404): "fix(node): support duplicate dlopen calls with DLHandleMap"

      • Fixes duplicate loading of the same native module (related failure mode)

    Current Code (Fixed)

    In src/bun.js/bindings/v8/node.cpp lines 78-89:

    } else {
        JSValue exportsObject = object->get(globalObject, WebCore::builtinNames(vm).exportsPublicName());
        RETURN_IF_EXCEPTION(scope, void());
    
        // Convert exports to object, matching Node.js behavior.
        // This throws for null/undefined and creates wrapper objects for primitives.
        JSObject* exports = exportsObject.toObject(globalObject);
        RETURN_IF_EXCEPTION(scope, void());
    
        ASSERT(exports);
        strongExportsObject = { vm, exports };
    }

    The toObject() call guarantees that either:

    • exports is a valid JSObject pointer (and strongExportsObject gets initialized), OR
    • An exception is thrown and the function returns early via RETURN_IF_EXCEPTION

    This prevents the null pointer dereference at line 97 where *strongExportsObject is used.

    Test Coverage

    Tests were added to verify the fixes:

    • test/js/node/process/dlopen-non-object-exports.test.ts - Tests null/undefined/primitive exports
    • test/js/node/process/dlopen-duplicate-load.test.ts - Tests duplicate module loading

    This is an automated comment.

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

    crashmacOSnapiruntime

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions