Repository navigation
Description
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 = nullwas not tested — it may or may not have the same issue. The reproduction is specific toonclose.- 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
ioredisto nativeBun.RedisClient. The graceful-shutdown path originally usedonclose = nullto detach the failure handler before an intentionalclose(); switching to() => {}fixed it.
Activity
robobun commentedon Apr 10, 2026
✅ Reproduced —
c.onclose = null; c.close()hitsA JavaScript exception was thrown, but it was cleared before it could be read.Fix in #29149:
setOnClose/setOnConnectnow acceptnull/undefinedto detach the handler (throwTypeErrorfor other non-callable values)..isCallable().packages/bun-types/redis.d.tsupdated soonconnect/oncloseacceptnull | undefinedto match the new runtime contract.Supersedes #28454 (same bug, narrower scope).
test: wrap #29145 regression tests in try/finally for cleanup
test: wrap #29145 regression tests in try/finally for cleanup
Add regression test for onclose = null on manual close (#29145)
Add regression test for onclose = null on manual close (#29145)
Add regression test for onclose = null on manual close (#29145)
Add regression test for onclose = null on manual close (#29145)
Add regression test for onclose = null on manual close (#29145)
Hardening: fix crashes across the runtime, bundler, dev server, shell…