Skip to content

RedisClient: assigning null to onclose panics during close() #29145

Closed

Description

@Desttro

What version of Bun is running?

1.3.12+700fc117 (Linux x64, WSL kernel 6.6.87, glibc 2.39)

What platform is your computer?

Linux 6.6.87.2-microsoft-standard-WSL2 (WSL2 on Windows, x86_64)

What steps can reproduce the bug?

The published type definitions for Bun.RedisClient declare:

// packages/bun-types/redis.d.ts
onconnect: ((this: RedisClient) => void) | null;
onclose: ((this: RedisClient, error: Error) => void) | null;

So null is documented as a valid value. At runtime, however, assigning null to onclose panics during the subsequent close().

Minimal reproduction (requires a Redis/Valkey/Dragonfly server on localhost:6379):

// repro.ts
const c = new Bun.RedisClient("redis://localhost:6379")
await c.set("test:onclose-null-panic", "v", "EX", "10")
c.onclose = null  // <-- panics on the close() below
c.close()
console.log("closed ok")

Run: bun repro.ts

What is the expected behavior?

Clean exit, prints closed ok. The type def advertises null as an assignable value, so detaching a handler via null should be a supported way to suppress post-assignment callback invocations.

What do you see instead?

============================================================
Bun v1.3.12 (700fc117) Linux x64
WSL Kernel v6.6.87 | glibc v2.39
CPU: sse42 popcnt avx avx2
Args: "bun" "repro.ts"
Features: dotenv(2) jsc tsconfig(2) valkey
Builtins: "bun:main"
Elapsed: 21ms | User: 7ms | Sys: 15ms
RSS: 1.11GB | Peak: 35.39MB | Commit: 1.11GB | Faults: 0 | Machine: 8.26GB

panic(main thread): A JavaScript exception was thrown, but it was cleared before it could be read.
oh no: Bun has crashed. This indicates a bug in Bun, not your code.

Bun exits with status 132 (SIGILL). The panic reproduces 100% of the time on the minimal repro above.

Workaround

Assign an empty function instead of null:

c.onclose = () => {}  // works fine — graceful close, no panic
c.close()

This is load-bearing if you're trying to suppress "connection closed" callback logic during an intentional shutdown (e.g. to avoid logging graceful close as a connection failure).

Additional information

  • onconnect = null was not tested — it may or may not have the same issue. The reproduction is specific to onclose.
  • The panic message ("A JavaScript exception was thrown, but it was cleared before it could be read") suggests the runtime is attempting to invoke the cleared callback during socket teardown and hitting an NPE that's masked by the teardown path.
  • Discovered while migrating a ServiceNow ops copilot from ioredis to native Bun.RedisClient. The graceful-shutdown path originally used onclose = null to detach the failure handler before an intentional close(); switching to () => {} fixed it.

Crash report URL

https://bun.report/1.3.12/la1700fc11gigBgwggCwv2q2Cm1p8oD6pq8oD8z8ooEqg6u4D087sqE_spzykEm8yqkE+q74yF2kp12F_2yqykEm8yqkEi/j02Fo52z2F4g/p7F80ojmEu2xi+Col9yuDA0eNodyMENgCAMBdBV/gDGHbx6dYJSamxCKKlFHN/g8b0NOz10sGsLyMvSQq1i0I243EZdkHpA4y8uQi4ZSU5zmcvWyzRcKK8fXeUb/A

Activity

  1. robobun commented on Apr 10, 2026

    @robobun
    Collaborator

    ✅ Reproduced — c.onclose = null; c.close() hits A JavaScript exception was thrown, but it was cleared before it could be read.

    Fix in #29149:

    • setOnClose / setOnConnect now accept null/undefined to detach the handler (throw TypeError for other non-callable values).
    • Call sites guard the cached value with .isCallable().
    • packages/bun-types/redis.d.ts updated so onconnect/onclose accept null | undefined to match the new runtime contract.

    Supersedes #28454 (same bug, narrower scope).

  2. added a commit that references this issue on Apr 11, 2026
  3. added a commit that references this issue on May 5, 2026
  4. added a commit that references this issue on Oct 20, 2026
    9b4284f
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions