Skip to content
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
8b513f4
Service the regular loop's concurrent queue while a macro is being wa…
robobun Aug 21, 2026
f202cb0
[autofix.ci] apply automated fixes
autofix-ci[bot] Aug 21, 2026
b19e793
Tighten comments
robobun Aug 21, 2026
2386c5a
Use into_task for the batch move, cover the un-awaited digest exit
robobun Aug 21, 2026
e6933c1
Fold take_concurrent_tasks into take_concurrent_batch
robobun Aug 21, 2026
6a2a313
Share the ticket issue path between ticket and regular_ticket
robobun Aug 21, 2026
c6e5d93
Cover the other regular-loop producers a macro can await
dylan-conway Aug 22, 2026
5eb0d4d
Route completions by the loop current when the work was initiated
dylan-conway Aug 22, 2026
30dbe52
[autofix.ci] apply automated fixes
autofix-ci[bot] Aug 22, 2026
9e92026
Assert the un-awaited digest continuation runs before exit
robobun Aug 22, 2026
c0e7d52
Take LoopKind across the FFI as the enum, not u8
dylan-conway Aug 22, 2026
ea43333
Capture the loop for cross-thread MessagePort and BroadcastChannel de…
dylan-conway Aug 22, 2026
7da4c35
[autofix.ci] apply automated fixes
autofix-ci[bot] Aug 22, 2026
5e922f9
Name BunLoopKind values loopKind, not loop
dylan-conway Aug 22, 2026
a5ead42
Make loopKind postTaskTo's second parameter, with no default
dylan-conway Aug 22, 2026
6c974b9
[autofix.ci] apply automated fixes
autofix-ci[bot] Aug 22, 2026
a87fb37
Restore multi-line lambda layout at the postTaskTo call sites
dylan-conway Aug 22, 2026
add5509
Inline postTaskToWorkerObject at its call sites
dylan-conway Aug 22, 2026
69704ce
JSCTaskScheduler: keep the two pending containers, store the loop kin…
dylan-conway Aug 22, 2026
61822af
Use the currentLoopKind accessor at the JSWorker request-reply sites
robobun Aug 22, 2026
1e73951
postTaskTo: use the caller's loopKind as given
dylan-conway Aug 22, 2026
33cb02c
JSCTaskScheduler: minimal change — store the loop kind as the map val…
dylan-conway Aug 22, 2026
a622a44
WebCrypto: capture the loop kind at dispatch and pass it to postTaskT…
dylan-conway Aug 22, 2026
f094e2b
event_loop.rs: keep the original drain, add only the finished-macro a…
dylan-conway Aug 22, 2026
80078b9
Rename finished_macro_loop to macro_loop_if_not_running
dylan-conway Aug 22, 2026
8f1658d
Merge branch 'main' into farm/4186ec60/macro-webcrypto-hang
dylan-conway Aug 22, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/jsc/CppTask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bun_event_loop::{TaskTag, Taskable, task_tag};
use bun_threading::work_pool::{Task as WorkPoolTask, WorkPool};

unsafe extern "C" {
fn Bun__EventLoopTaskNoContext__performTask(task: *mut EventLoopTaskNoContext);
fn Bun__EventLoopTaskNoContext__performTask(task: *mut EventLoopTaskNoContext) -> *mut CppTask;
}

bun_opaque::opaque_ffi! {
Expand Down Expand Up @@ -44,16 +44,21 @@ impl CppTask {
bun_opaque::opaque_ffi! { pub struct EventLoopTaskNoContext; }

impl EventLoopTaskNoContext {
/// Deallocates `this`
pub unsafe fn run(this: *mut EventLoopTaskNoContext) {
// SAFETY: caller guarantees `this` is a valid C++ EventLoopTaskNoContext; performTask consumes/frees it.
/// Run and deallocate `this`; returns the `WebCore::EventLoopTask` the
/// work handed back for its JS thread, or null.
///
/// # Safety
/// `this` is the live C++ task handed to `ConcurrentCppTask__createAndRun`.
Comment thread
robobun marked this conversation as resolved.
Resolved
pub unsafe fn run(this: *mut EventLoopTaskNoContext) -> *mut CppTask {
// SAFETY: fn contract; performTask consumes/frees it.
unsafe { Bun__EventLoopTaskNoContext__performTask(this) }
}
}

/// A task created from C++ code that runs inside the workpool (WebCrypto's
/// `PhonyWorkQueue`). Holds the creating VM's ticket: the C++ closure captures
/// context-affine objects and posts its result back by context id.
/// context-affine objects, and the reply it returns is posted through the
/// ticket, to the loop that was current when the work was dispatched.
Comment thread
robobun marked this conversation as resolved.
Resolved
#[repr(C)]
pub struct ConcurrentCppTask {
pub(crate) cpp_task: *mut EventLoopTaskNoContext,
Expand All @@ -71,7 +76,12 @@ impl ConcurrentCppTask {
} = *self;
// SAFETY: `cpp_task` is the valid C++ handle stored by `ConcurrentCppTask__createAndRun`;
// `run` consumes it here.
unsafe { EventLoopTaskNoContext::run(cpp_task) };
let reply = unsafe { EventLoopTaskNoContext::run(cpp_task) };
if !reply.is_null() {
ticket.post(bun_event_loop::ConcurrentTask::ConcurrentTask::create(
bun_event_loop::Task::init(reply),
));
}
ticket.unref_keep_alive();
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/jsc/JSCScheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,21 @@ impl JSCDeferredWorkTask {
}

/// JSC helper threads (DeferredWorkTimer): deliver a deferred-work job to the
/// VM's loop, or run its release path here if the VM is gone.
/// VM's `kind` loop (captured by `JSCTaskScheduler::onAddPendingWork` on the JS
/// thread when the work was registered), or run its release path here if the
/// VM is gone.
Comment thread
robobun marked this conversation as resolved.
Resolved
#[unsafe(no_mangle)]
unsafe extern "C" fn Bun__queueJSCDeferredWorkTaskConcurrently(
r: *const crate::vm_handle::Shared,
task: *mut JSCDeferredWorkTask,
kind: crate::LoopKind,
) {
crate::mark_binding!();
// SAFETY: C++ passes the reference its JSVMClientData holds.
let handle = unsafe { crate::VmHandle::borrow_ref(r) };
// `create_from` heap-allocates with the auto-delete bit set.
let ct = ConcurrentTask::create_from(task);
if let crate::vm_handle::Posted::Refused(ct) = handle.post(crate::LoopKind::Regular, ct) {
if let crate::vm_handle::Posted::Refused(ct) = handle.post(kind, ct) {
// SAFETY: refused ⇒ we own the ConcurrentTask box; the C++ job's ticket
// was already cancelled by the VM teardown (DeferredWorkTimer is shut
// down before ~VM), so dropping the job pointer here loses nothing.
Expand Down
2 changes: 1 addition & 1 deletion src/jsc/VirtualMachine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,7 @@ impl VirtualMachine {
+ self.active_tasks
+ el.tasks.readable_length()
+ el.yield_tasks.len()
+ (!el.concurrent_tasks.is_empty() as usize)
+ (el.has_concurrent_tasks() as usize)
+ (el.has_pending_refs() as usize)
> 0)
}
Expand Down
51 changes: 35 additions & 16 deletions src/jsc/VmHandle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,21 @@ enum State {
Closed = 3,
}

/// Which of the VM's two embedded loops a completion belongs to, fixed when
/// the ticket is taken on the JS thread (work started while a macro runs
/// completes into the macro loop). `Bun.spawnSync`'s isolated loop is not one
/// of these: its producers post through that loop's own [`JsPoster`].
/// Which of the VM's two embedded loops a completion belongs to, decided on
/// the JS thread when the work is initiated: a [`Ticket`] records
/// [`VirtualMachine::current_loop_kind`] when it is taken, and a weak poster
/// carries the kind its JS-side initiator captured (C++ `BunLoopKind`). Work
/// started while a macro runs completes into the macro loop; once the macro
/// has returned, the regular loop services what is left there
/// (`EventLoop::finished_macro_loop`). `Bun.spawnSync`'s isolated loop is not
/// one of these: its producers post through that loop's own [`JsPoster`].
Comment thread
robobun marked this conversation as resolved.
Resolved
///
/// [`JsPoster`]: bun_event_loop::JsPoster
#[repr(u8)] // C++: `BunLoopKind` (BunLoopKind.h)
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LoopKind {
Regular,
Macro,
Regular = 0,
Macro = 1,
}

/// `state` (read by every native→JS entry on the JS thread) and `vm`, on
Expand Down Expand Up @@ -341,17 +346,18 @@ impl VmHandle {
})
}

/// Queue a C++ `EventLoopTask` from another thread (WebCore's
/// `postTaskConcurrently`), or delete it unrun if the VM is closed.
/// Queue a C++ `EventLoopTask` on the VM's `kind` loop from another
/// thread (WebCore's `postTaskTo` / `postTaskConcurrently`), or delete it
/// unrun if the VM is closed.
Comment thread
robobun marked this conversation as resolved.
Resolved
///
/// # Safety
/// `task` is a live heap `WebCore::EventLoopTask` the caller hands over.
pub unsafe fn post_cpp_task(&self, task: *mut crate::cpp_task::CppTask) {
pub unsafe fn post_cpp_task(&self, kind: LoopKind, task: *mut crate::cpp_task::CppTask) {
unsafe extern "C" {
fn Bun__deleteEventLoopTask(task: *mut crate::cpp_task::CppTask);
}
let ct = ConcurrentTaskItem::create(bun_event_loop::Task::init(task));
if let Posted::Refused(ct) = self.post(LoopKind::Regular, ct) {
if let Posted::Refused(ct) = self.post(kind, ct) {
// SAFETY: refused ⇒ we own both boxes.
unsafe {
drop(bun_core::heap::take(ct.as_ptr()));
Expand Down Expand Up @@ -518,6 +524,13 @@ impl VirtualMachine {
}
}

/// JS thread: [`VirtualMachine::current_loop_kind`] for C++ (`BunLoopKind`),
/// captured by the initiator of work whose completion is posted weakly.
Comment thread
robobun marked this conversation as resolved.
Resolved
#[unsafe(no_mangle)]
pub extern "C" fn Bun__VM__currentLoopKind(vm: &VirtualMachine) -> LoopKind {
vm.current_loop_kind()
}

// ── Test suite only: deterministic late completions ───────────────────────
//
// `BUN_DEBUG_TEST_WORKER_TEARDOWN_GATE` (first-level worker VMs; builds with
Expand Down Expand Up @@ -693,8 +706,8 @@ pub unsafe extern "C" fn Bun__VmHandle__release(r: *const Shared) {
drop(unsafe { VmHandle::from_ref(r) });
}

/// Any thread: post a C++ task through a reference and give the reference up
/// (queued, or deleted unrun if the VM is closed).
/// Any thread: post a C++ task to the VM's `kind` loop through a reference and
/// give the reference up (queued, or deleted unrun if the VM is closed).
Comment thread
robobun marked this conversation as resolved.
Resolved
///
/// # Safety
/// `r` came from `Bun__VmHandle__retain*` and is not used afterwards; `task` is
Expand All @@ -703,11 +716,12 @@ pub unsafe extern "C" fn Bun__VmHandle__release(r: *const Shared) {
pub unsafe extern "C" fn Bun__VmHandle__postAndRelease(
r: *const Shared,
task: *mut crate::cpp_task::CppTask,
kind: LoopKind,
) {
// SAFETY: fn contract.
let handle = unsafe { VmHandle::from_ref(r) };
// SAFETY: fn contract.
unsafe { handle.post_cpp_task(task) };
unsafe { handle.post_cpp_task(kind, task) };
}

/// JS thread: adjust this VM's keep-alive directly.
Expand All @@ -720,14 +734,19 @@ pub extern "C" fn Bun__eventLoop__refKeepAlive(vm: &VirtualMachine, delta: core:
}
}

/// Any thread: adjust the VM's keep-alive (no-op once the VM is closed).
/// Any thread: adjust the keep-alive on the VM's `kind` loop (no-op once the
/// VM is closed).
Comment thread
robobun marked this conversation as resolved.
Resolved
///
/// # Safety
/// `r` is a live reference its holder keeps for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn Bun__VmHandle__refKeepAlive(r: *const Shared, delta: core::ffi::c_int) {
pub unsafe extern "C" fn Bun__VmHandle__refKeepAlive(
r: *const Shared,
kind: LoopKind,
delta: core::ffi::c_int,
) {
// SAFETY: fn contract.
unsafe { VmHandle::borrow_ref(r) }.add_keep_alive(LoopKind::Regular, delta.signum());
unsafe { VmHandle::borrow_ref(r) }.add_keep_alive(kind, delta.signum());
}

/// Any thread: Node's `can_call_into_js()`.
Expand Down
8 changes: 5 additions & 3 deletions src/jsc/bindings/BunClientData.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// A counted reference to a VM's handle (bun_jsc::VmHandle): what any thread other than the
// VM's own uses to post work to it, keep its loop alive, or ask whether it may still run
// script. retain / retainRef take a count, release gives one up; valid however long it is held.
#include "BunLoopKind.h"
struct BunVmHandleRef;
extern "C" const BunVmHandleRef* Bun__VmHandle__retain(void* bunVM); // JS thread
extern "C" const BunVmHandleRef* Bun__VmHandle__retainRef(const BunVmHandleRef*); // any thread
Expand All @@ -23,9 +24,10 @@ namespace WebCore {
class WorkerMessagingProxy;
class EventLoopTask;
}
// Post through a reference and give it up in one step (a reference taken only to outlive a lock).
extern "C" void Bun__VmHandle__postAndRelease(const BunVmHandleRef*, WebCore::EventLoopTask*);
extern "C" void Bun__VmHandle__refKeepAlive(const BunVmHandleRef*, int delta);
// Post to the VM's `kind` loop through a reference and give it up in one step (a reference taken only
// to outlive a lock).
Comment thread
robobun marked this conversation as resolved.
Resolved
extern "C" void Bun__VmHandle__postAndRelease(const BunVmHandleRef*, WebCore::EventLoopTask*, BunLoopKind);
extern "C" void Bun__VmHandle__refKeepAlive(const BunVmHandleRef*, BunLoopKind, int delta);
// Node's can_call_into_js(): false once the VM's stop was requested (terminate()/exit/teardown). Any thread.
extern "C" bool Bun__VmHandle__scriptAllowed(const BunVmHandleRef*);
// The handle's state byte, so hot paths test it inline (BUN_VM_HANDLE_STATE_OPEN == bun_jsc::vm_handle::State::Open).
Expand Down
8 changes: 4 additions & 4 deletions src/jsc/bindings/BunDebugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class BunInspectorConnection : public ThreadSafeRefCounted<BunInspectorConnectio
this->status = ConnectionStatus::Connected;
auto* globalObject = context.jsGlobalObject();
if (this->unrefOnDisconnect) {
Bun__VmHandle__refKeepAlive(WebCore::clientData(JSC::getVM(globalObject))->vmHandle, 1);
Bun__VmHandle__refKeepAlive(WebCore::clientData(JSC::getVM(globalObject))->vmHandle, BunLoopKind::Regular, 1);
}
globalObject->setInspectable(true);
auto& inspector = globalObject->inspectorDebuggable();
Expand Down Expand Up @@ -213,7 +213,7 @@ class BunInspectorConnection : public ThreadSafeRefCounted<BunInspectorConnectio

if (connection->unrefOnDisconnect) {
connection->unrefOnDisconnect = false;
Bun__VmHandle__refKeepAlive(WebCore::clientData(context.vm())->vmHandle, -1);
Bun__VmHandle__refKeepAlive(WebCore::clientData(context.vm())->vmHandle, BunLoopKind::Regular, -1);
}

{
Expand Down Expand Up @@ -476,7 +476,7 @@ class BunInspectorConnection : public ThreadSafeRefCounted<BunInspectorConnectio
notifyPausedThread();

if (this->jsThreadMessageScheduledCount++ == 0) {
ScriptExecutionContext::postTaskTo(scriptExecutionContextIdentifier, [connection = Ref { *this }](ScriptExecutionContext& context) {
ScriptExecutionContext::postTaskTo(scriptExecutionContextIdentifier, BunLoopKind::Regular, [connection = Ref { *this }](ScriptExecutionContext& context) {
connection->receiveMessagesOnInspectorThread(context, static_cast<Zig::GlobalObject*>(context.jsGlobalObject()), true);
});
}
Expand All @@ -492,7 +492,7 @@ class BunInspectorConnection : public ThreadSafeRefCounted<BunInspectorConnectio
notifyPausedThread();

if (this->jsThreadMessageScheduledCount++ == 0) {
ScriptExecutionContext::postTaskTo(scriptExecutionContextIdentifier, [connection = Ref { *this }](ScriptExecutionContext& context) {
ScriptExecutionContext::postTaskTo(scriptExecutionContextIdentifier, BunLoopKind::Regular, [connection = Ref { *this }](ScriptExecutionContext& context) {
connection->receiveMessagesOnInspectorThread(context, static_cast<Zig::GlobalObject*>(context.jsGlobalObject()), true);
});
}
Expand Down
17 changes: 17 additions & 0 deletions src/jsc/bindings/BunLoopKind.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <stdint.h>

// Which of a VM's two embedded event loops a posted completion belongs to
// (bun_jsc::LoopKind). Decided on the VM's own thread when the work is
// initiated: `Regular`, or `Macro` while a macro is being run, so that a
// macro's wait services what the macro started and nothing else. Work that
// no script initiated (the debugger, signals, another thread's own doing)
// is `Regular`.
Comment thread
robobun marked this conversation as resolved.
Resolved
enum class BunLoopKind : uint8_t {
Regular = 0,
Macro = 1,
};

// JS thread only: the loop the VM is currently running (what a ticket taken now would record).
extern "C" BunLoopKind Bun__VM__currentLoopKind(void* bunVM);
2 changes: 2 additions & 0 deletions src/jsc/bindings/EventLoopTask.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

#include "root.h"
#include "ScriptExecutionContext.h"

Expand Down
4 changes: 2 additions & 2 deletions src/jsc/bindings/EventLoopTaskNoContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Bun {

extern "C" void Bun__EventLoopTaskNoContext__performTask(EventLoopTaskNoContext* task)
extern "C" WebCore::EventLoopTask* Bun__EventLoopTaskNoContext__performTask(EventLoopTaskNoContext* task)
{
task->performTask();
return task->performTask();
}

} // namespace Bun
23 changes: 14 additions & 9 deletions src/jsc/bindings/EventLoopTaskNoContext.h
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
#pragma once

#include "ZigGlobalObject.h"
#include "root.h"
#include "EventLoopTask.h"
#include "ZigGlobalObject.h"

namespace Bun {

// Just like WebCore::EventLoopTask but does not take a ScriptExecutionContext.
// The Rust `ConcurrentCppTask` that carries one to the work pool holds the
// creating VM's ticket, so that VM outlives the task.
// Work for the thread pool that hands back what to run on the JS thread it
// came from. The Rust `ConcurrentCppTask` that carries it holds the creating
// VM's ticket, so that VM outlives the task, and posts the reply through that
// ticket: to the loop that was current when the work was dispatched.
Comment thread
robobun marked this conversation as resolved.
Resolved
class EventLoopTaskNoContext {
WTF_MAKE_TZONE_ALLOCATED(EventLoopTaskNoContext);

public:
EventLoopTaskNoContext(Function<void()>&& task)
using Reply = Function<void(WebCore::ScriptExecutionContext&)>;

EventLoopTaskNoContext(Function<Reply()>&& task)
: m_task(WTF::move(task))
{
}

void performTask()
WebCore::EventLoopTask* performTask()
{
m_task();
Reply reply = m_task();
delete this;
return reply ? new WebCore::EventLoopTask(WTF::move(reply)) : nullptr;
}

private:
Function<void()> m_task;
Function<Reply()> m_task;
};

extern "C" void Bun__EventLoopTaskNoContext__performTask(EventLoopTaskNoContext* task);
extern "C" WebCore::EventLoopTask* Bun__EventLoopTaskNoContext__performTask(EventLoopTaskNoContext* task);

} // namespace Bun
2 changes: 1 addition & 1 deletion src/jsc/bindings/JSCFFIBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static void Bun__jscFFIThreadsafeDispatch(JSC::FFI::ThreadsafeInvocation& invoca
{
static_assert(sizeof(WebCore::ScriptExecutionContextIdentifier) <= sizeof(void*));
auto contextId = static_cast<WebCore::ScriptExecutionContextIdentifier>(reinterpret_cast<uintptr_t>(invocation.embedderContext()));
WebCore::ScriptExecutionContext::postTaskTo(contextId, [protectedInvocation = Ref { invocation }](WebCore::ScriptExecutionContext&) mutable { JSC::FFI::runThreadsafeInvocation(protectedInvocation.get()); });
WebCore::ScriptExecutionContext::postTaskTo(contextId, BunLoopKind::Regular, [protectedInvocation = Ref { invocation }](WebCore::ScriptExecutionContext&) mutable { JSC::FFI::runThreadsafeInvocation(protectedInvocation.get()); });
}

extern "C" JSC::EncodedJSValue Bun__CreateJSCFFICallback(
Expand Down
Loading
Loading