-
Notifications
You must be signed in to change notification settings - Fork 5k
node:vm: enforce timeout as a wall-clock deadline that interrupts Atomics.wait #33764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robobun
wants to merge
4
commits into
main
Choose a base branch
from
claude/farm/1771cc13/vm-timeout-atomics-wait
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
79dc203
node:vm: enforce timeout as a wall-clock deadline that interrupts Ato…
robobun 000b5e4
review: trim comments to 3 lines, guard drainMicrotasksForGlobalObjec…
robobun b0e199f
TimeoutWatchdog: re-assert termination in the nudge loop
robobun 7051bc3
ci: retrigger
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| #include "TimeoutWatchdog.h" | ||
|
|
||
| #include <JavaScriptCore/VM.h> | ||
| #include <JavaScriptCore/VMTraps.h> | ||
| #include <JavaScriptCore/WaiterListManager.h> | ||
| #include <JavaScriptCore/ExceptionScope.h> | ||
|
|
||
| namespace Bun { | ||
|
|
||
| TimeoutWatchdog::TimeoutWatchdog(JSC::VM& vm, std::optional<int64_t> timeoutMs) | ||
| : m_vm(vm) | ||
| { | ||
| if (!timeoutMs) | ||
| return; | ||
|
|
||
| // throwTerminationException() (reached via handleTraps / Atomics.wait | ||
| // Terminated) asserts this exists; allocate it on the mutator thread. | ||
| vm.ensureTerminationException(); | ||
|
|
||
| auto deadline = MonotonicTime::now() + Seconds::fromMilliseconds(static_cast<double>(*timeoutMs)); | ||
| m_thread = WTF::Thread::create("node:vm timeout"_s, [this, deadline] { | ||
| Locker locker { m_lock }; | ||
| while (!m_disarmed) { | ||
| if (m_cond.waitUntil(m_lock, deadline)) | ||
| continue; | ||
| if (m_disarmed) | ||
| return; | ||
| fire(); | ||
| // Re-assert until disarmed: a nested scope's clearTerminationState | ||
| // can wipe the request this watchdog installed, and a lost notify | ||
| // can land between the waiter's predicate check and parking. | ||
| while (!m_disarmed) { | ||
| if (m_cond.waitUntil(m_lock, MonotonicTime::now() + 1_ms)) | ||
| continue; | ||
| fire(); | ||
| } | ||
| return; | ||
| } | ||
| }); | ||
| } | ||
|
robobun marked this conversation as resolved.
|
||
|
|
||
| TimeoutWatchdog::~TimeoutWatchdog() | ||
| { | ||
| disarm(); | ||
| } | ||
|
|
||
| void TimeoutWatchdog::disarm() | ||
| { | ||
| if (!m_thread) | ||
| return; | ||
| { | ||
| Locker locker { m_lock }; | ||
| m_disarmed = true; | ||
| } | ||
| m_cond.notifyOne(); | ||
| m_thread->waitForCompletion(); | ||
| m_thread = nullptr; | ||
| } | ||
|
|
||
| void TimeoutWatchdog::fire() | ||
| { | ||
| m_fired.store(true, std::memory_order_release); | ||
| // waitForSync's loop predicate is !vm.hasTerminationRequest(); set it | ||
| // before the notify so a woken Atomics.wait returns Terminated. | ||
| m_vm.setHasTerminationRequest(); | ||
| // Raise NeedTermination so running JS exits at the next back-edge, and | ||
| // notify the sync waiter unconditionally (requestThreadStopIfNeeded skips | ||
| // its own notify when a thread-stop is already pending). | ||
| m_vm.notifyNeedTermination(); | ||
|
robobun marked this conversation as resolved.
|
||
| m_vm.syncWaiter()->condition().notifyOne(); | ||
| } | ||
|
|
||
| void TimeoutWatchdog::clearTerminationState(JSC::VM& vm) | ||
| { | ||
| if (vm.hasPendingTerminationException()) { | ||
| auto scope = DECLARE_TOP_EXCEPTION_SCOPE(vm); | ||
| scope.clearException(); | ||
| } | ||
| vm.clearHasTerminationRequest(); | ||
| vm.traps().clearTrap(JSC::VMTraps::NeedTermination); | ||
| vm.traps().clearTrap(JSC::VMTraps::NeedWatchdogCheck); | ||
| } | ||
|
|
||
| } // namespace Bun | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| #pragma once | ||
|
|
||
| #include "root.h" | ||
|
|
||
| #include <wtf/Condition.h> | ||
| #include <wtf/Lock.h> | ||
| #include <wtf/Threading.h> | ||
|
|
||
| namespace Bun { | ||
|
|
||
| // Wall-clock watchdog for node:vm's `timeout`. On fire it requests VM | ||
| // termination and wakes a blocked Atomics.wait; the destructor joins the | ||
| // worker, so instances are stack-allocated bracketing JSC::evaluate. | ||
| class TimeoutWatchdog { | ||
| WTF_MAKE_NONCOPYABLE(TimeoutWatchdog); | ||
|
|
||
| public: | ||
| TimeoutWatchdog(JSC::VM& vm, std::optional<int64_t> timeoutMs); | ||
| ~TimeoutWatchdog(); | ||
|
|
||
| void disarm(); | ||
| bool didFire() const { return m_fired.load(std::memory_order_acquire); } | ||
|
|
||
| // Clears request flag, trap bits, and pending termination exception. | ||
| // Call from the mutator thread after evaluate returns. | ||
| static void clearTerminationState(JSC::VM&); | ||
|
|
||
| private: | ||
| void fire(); | ||
|
|
||
| JSC::VM& m_vm; | ||
| WTF::Lock m_lock; | ||
| WTF::Condition m_cond; | ||
| std::atomic<bool> m_fired { false }; | ||
| bool m_disarmed WTF_GUARDED_BY_LOCK(m_lock) { false }; | ||
| RefPtr<WTF::Thread> m_thread; | ||
| }; | ||
|
|
||
| } // namespace Bun |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.