Commit Graph

6 Commits

Author SHA1 Message Date
Claude Bot
12dcd52dea Phase 2, Step 2.4: Synchronize HTTP Thread Callbacks
Improve thread safety and reduce lock contention in HTTP thread callbacks by
making the handoff from HTTP thread to main thread explicit and minimizing
work done under lock.

HTTP Thread Improvements:
- Add fast-path abort check before acquiring lock (atomic read only)
- Move duplicate scheduling check before lock (reduces contention)
- Keep critical section brief (just data copying)
- Handle OOM gracefully under lock
- Add ref() before enqueueTaskConcurrent for proper lifetime management

Main Thread Improvements:
- Dramatically reduce lock holding time (from ~150 lines to ~30 lines)
- Copy state out under lock, then release before JS work
- Perform all JS interactions without lock (certificate validation, promise resolution)
- Add unconditional deref() at start to balance HTTP thread ref()
- Re-acquire lock briefly only when needed

Reference Counting:
- HTTP thread: ref() before each enqueue to main thread
- Main thread: unconditional defer deref() at callback start
- Prevents use-after-free and reference leaks
- Matches existing onWriteRequestDataDrain/resumeRequestDataStream pattern

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-03 14:39:30 +00:00
Claude Bot
8a0681e0c8 Phase 2, Step 2.3: Fix Cross-Thread Deallocation
Fix potential memory leak and use-after-free in derefFromThread when VM is
shutting down. The function now checks VM shutdown status before enqueueing
cleanup tasks, intentionally leaking memory (detected by ASAN) rather than
risking use-after-free crashes.

Changes:
- Add VM shutdown pre-check before enqueueTaskConcurrent
- Add debug logging for intentional leaks during shutdown
- Add deinitFromMainThread helper with main thread assertion
- Early return on shutdown to prevent unsafe cleanup attempts

This is safer than attempting cleanup during VM shutdown and makes the
memory safety trade-off explicit: leak detection over undefined behavior.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-03 14:15:13 +00:00
Claude Bot
d78ab7ff05 Phase 2, Step 2.2: Fix Response Finalization Race
Fix critical race condition in Bun__FetchResponse_finalize where the function
accessed shared state without acquiring the mutex, racing with the HTTP
thread's callback() which does lock the mutex.

Changes:
- Add mutex acquisition at function entry with RAII defer unlock
- Inline ignoreRemainingResponseBody logic under lock protection
- Set abort flag atomically with .release ordering
- Clear buffers safely under lock (prevents concurrent modification)
- Invert promise check logic for clearer control flow

The dual ownership pattern (response_weak + native_response) is intentional
and remains unchanged for finalization tracking.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-03 13:55:49 +00:00
Claude Bot
ad58282b21 Phase 2, Step 2.1: Add MainThreadData and SharedData structs
Add thread safety architecture with clear separation between main-thread-only
data and shared data that requires mutex protection. This is a non-breaking
change that adds new types without modifying existing FetchTasklet logic.

Added:
- MainThreadData struct with 12 fields for main thread only data
- SharedData struct with 14 fields for cross-thread shared data
- LockedSharedData RAII wrapper for safe mutex handling
- Comprehensive documentation of ownership and thread safety patterns

All structs are at module level for architectural clarity.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-03 13:46:13 +00:00
Claude Bot
2b6b49c549 Phase 1, Steps 1.1-1.2: Add state machine enums and helpers
Add FetchLifecycle and RequestStreamState enums to replace boolean flag
soup with explicit multi-dimensional state tracking. This is a non-breaking
change that adds new types without modifying existing logic.

Added:
- FetchLifecycle enum with 10 states and 4 helper methods
- RequestStreamState enum with 4 states
- transitionLifecycle() helper with debug assertions
- shouldIgnoreBodyData() computed property
- Comprehensive documentation and state transition examples

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-03 13:24:42 +00:00
robobun
946470dcd7 Refactor: move FetchTasklet to separate file (#24330)
## Summary

Extract `FetchTasklet` struct from `src/bun.js/webcore/fetch.zig` into
its own file at `src/bun.js/webcore/fetch/FetchTasklet.zig` to improve
code organization and modularity.

## Changes

- Moved `FetchTasklet` struct definition (1336 lines) to new file
`src/bun.js/webcore/fetch/FetchTasklet.zig`
- Added all necessary imports to the new file
- Updated `fetch.zig` line 61 to import `FetchTasklet` from the new
location: `pub const FetchTasklet =
@import("./fetch/FetchTasklet.zig").FetchTasklet;`
- Verified compilation succeeds with `bun bd`

## Impact

- No functional changes - this is a pure refactoring
- Improves code organization by separating the large `FetchTasklet`
implementation
- Makes the codebase more maintainable and easier to navigate
- Reduces `fetch.zig` from 2768 lines to 1433 lines

## Test plan

- [x] Built successfully with `bun bd`
- [x] No changes to functionality - pure code organization refactor

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Bot <claude-bot@bun.sh>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2025-11-03 02:21:49 -08:00