Skip to content

Conversation

@robobun
Copy link
Collaborator

@robobun robobun commented Jan 29, 2026

Summary

Changes

The following callbacks now safely handle null handle.data:

PipeReader.zig:

  • onStreamAlloc - returns empty buffer if null
  • onStreamRead - returns early if null
  • onPipeClose - returns early if null
  • onTTYClose - returns early if null

PipeWriter.zig:

  • onPipeClose - returns early if null
  • onTTYClose - returns early if null

win_watcher.zig:

  • uvClosedCallback - returns early if null

Root Cause

When the pipe/handle is being closed, the code sets pipe.data = pipe (self-pointer) before calling close. Due to race conditions in Windows libuv event processing, callbacks can be invoked:

  1. After the handle's data pointer has been nullified
  2. After the handle has been deallocated
  3. During the transition period when data no longer points to the expected object

The bun.cast() function triggers Zig's runtime safety check when casting a null pointer, causing a panic.

Test plan

  • Build succeeds (bun bd)
  • Existing spawn tests pass on Linux
  • Manual testing on Windows with standalone executables (requires Windows CI)

Fixes #26574

🤖 Generated with Claude Code

Add null checks to Windows libuv callbacks to prevent "cast causes
pointer to be null" panics. This is a recurring Windows-specific bug
where libuv callbacks may be invoked with null handle.data due to race
conditions during handle cleanup or event loop processing.

The affected callbacks are:
- PipeReader: onStreamAlloc, onStreamRead, onPipeClose, onTTYClose
- PipeWriter: onPipeClose, onTTYClose
- PathWatcher: uvClosedCallback

When handle.data is null, the callbacks now safely return early instead
of panicking.

Fixes #26574

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@robobun
Copy link
Collaborator Author

robobun commented Jan 29, 2026

Updated 2:37 PM PT - Jan 29th, 2026

❌ Your commit 373f0cac has 3 failures in Build #36131 (All Failures):


🧪   To try this PR locally:

bunx bun-pr 26577

That installs a local version of the PR into your bun-26577 executable, so you can run:

bun-26577 --bun

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 29, 2026

Walkthrough

This PR adds defensive null-safety checks to Windows-specific libuv handle callbacks in file watcher and pipe I/O modules. The changes guard against null dereferences in close operations and race conditions by validating handle.data pointers before casting or dereferencing them.

Changes

Cohort / File(s) Summary
Windows File Watcher Callbacks
src/bun.js/node/win_watcher.zig
UV close callback now safely derives PathWatcher instance from event data with null-check fallback to prevent null dereferences during cleanup races.
Windows Pipe I/O Callbacks
src/io/PipeReader.zig, src/io/PipeWriter.zig
Added defensive null-handling for handle.data in stream read/alloc callbacks and pipe/TTY close handlers to prevent use-after-free scenarios during close races and handle state transitions.

Possibly related PRs

Suggested reviewers

  • Jarred-Sumner
  • pfgithub
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: adding null handle checks to Windows libuv callbacks to fix a critical crash issue.
Description check ✅ Passed The description provides context, lists affected callbacks, explains the root cause, and includes a test plan, aligning well with the template requirements.
Linked Issues check ✅ Passed All coding-related requirements from issue #26574 are met: null checks added to prevent panics, callbacks guard against null handle.data, and unsafe casts are guarded to prevent Zig runtime errors.
Out of Scope Changes check ✅ Passed All changes are focused and in-scope: adding null-safety guards to Windows libuv callbacks in three specific files (PipeReader.zig, PipeWriter.zig, win_watcher.zig) to address the identified issue.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash: cast causes pointer to be null when running standalone executable on Windows

2 participants