Skip to content

Keep park, wait and sleep with huge timeouts from waking after 1 ms - #66

Merged
Project516 merged 1 commit into
masterfrom
p516/long-timeouts
Nov 6, 2026
Merged

Project516 merged 1 commit into
masterfrom
p516/long-timeouts

Conversation

@Project516

Copy link
Copy Markdown
Owner

What this changes

Host timers hold at most 2^31-1 ms (about 24.8 days). A longer delay fires after 1 ms instead. park, Object.wait and Thread.sleep with a huge timeout, such as LockSupport.parkNanos(Long.MAX_VALUE), therefore woke after 1 ms. park and wait returned early, and sleep looped, waking every millisecond.

interruptibleWait, which all three use, now re-arms the timer in steps of at most that limit.

Tests

test/java/LongTimeouts.java blocks four threads in parkNanos, parkUntil, wait and sleep with huge timeouts and interrupts them after 300 ms. java-web now prints the same as HotSpot. Before, park and wait reported an early wake. pnpm typecheck passes.

Several OpenJDK virtual-thread and java.util.concurrent tests that hung or timed out may have been hitting this. They are not added here; the next sweep will show which ones now pass.

…ait and sleep with huge timeouts no longer wake early or spin
@coderabbitai

coderabbitai Bot commented Nov 6, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 4 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: eeb1b826-aca8-4394-9dac-0cde6f10b3b6
Commits

Reviewing files that changed from the base of the PR and between 85702e3 and 5fa946d.

Files selected for processing: 2

  • src/natives/lang.ts
  • test/java/LongTimeouts.java
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@project516-review-bot project516-review-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR fixes a bug where host timers clamp delays > 2^31-1 ms to 1 ms, causing park/wait/sleep with huge timeouts to wake early. The fix re-arms the timer in MAX_DELAY steps. A race condition exists: if an interrupt occurs between a step timer firing and its re-arm callback executing, the wait resolves but the callback still re-arms a stray timer. The test covers the four wait paths and passes with the fix.


review-bot, model nvidia/nemotron-3-ultra-550b-a55b:free, verdict request_changes

Comment thread src/natives/lang.ts
// timers fire at once past MAX_DELAY, so a longer wait re-arms
const arm = (left: number) => {
const step = Math.min(Math.max(0, left), MAX_DELAY);
timer = setTimeout(step < left ? () => { t.vm.timers.delete(timer!); arm(left - step); } : done, step);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Race condition: if an interrupt wakes the thread (calling done, which sets t.wake = null) after a step timer fires but before its re-arm callback runs, the callback still executes and re-arms a new timer. The wait has already resolved, so this creates a stray timer that will fire later and call done again. Fix: check if (t.wake === null) return; at the start of the re-arm callback before deleting the timer and calling arm.

@Project516
Project516 merged commit 316cc11 into master Nov 6, 2026
16 checks passed
@Project516
Project516 deleted the p516/long-timeouts branch November 6, 2026 22:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant