Skip to content

fix(test): randomBytes async callback test asserted pre-#6430 inline timing#6838

Open
TheHypnoo wants to merge 1 commit into
mainfrom
fix/random-bytes-async-test-macrotask
Open

fix(test): randomBytes async callback test asserted pre-#6430 inline timing#6838
TheHypnoo wants to merge 1 commit into
mainfrom
fix/random-bytes-async-test-macrotask

Conversation

@TheHypnoo

@TheHypnoo TheHypnoo commented Jul 25, 2026

Copy link
Copy Markdown
Member

What

crypto::random::tests::native_dispatch_random_bytes_value_form_fires_callback fails on main (verified at 8b2c736ed, macOS arm64):

panicked at crates/perry-stdlib/src/crypto/random.rs:906:9:
randomBytes callback must fire

The other 96 tests in perry-stdlib pass. A permanently red test in the per-crate CI scope teaches everyone to ignore that crate's results, so this closes it.

Why it fails

The test's assumption broke — the dispatch path is fine.

#6430 moved the async randomBytes callback onto a macrotask:

-    call_node_style_callback2(callback_bits, null, value);   // inline
+    perry_runtime::timer::js_set_immediate_callback_args(cb_ptr, args.as_ptr(), 2);

That is correct and deliberate. Node runs randomBytes on the libuv threadpool, so the (err, buffer) callback fires a loop iteration later, never inline. The test predates #6430, asserted synchronous firing, and was never updated.

The value-form dispatch itself still works: the "randomBytes" if args_len >= 2 arm routes to js_crypto_random_bytes_async, the callback lands with err = null and a Buffer pointer — just one macrotask later.

Change

Test only. Assert the callback does not fire inline, drain the callback-timer queue, then check the delivered arguments:

assert!(!CB_FIRED.with(|f| f.get()), "randomBytes callback must not fire synchronously");
perry_runtime::timer::js_callback_timer_tick();
assert!(CB_FIRED.with(|f| f.get()), "randomBytes callback must fire");

This locks in #6430's macrotask timing rather than the behavior it replaced.

Validation

cargo test --lib -p perry-stdlib
test result: ok. 97 passed; 0 failed; 0 ignored

./scripts/check_file_size.sh OK. cargo fmt --all leaves this diff unchanged (it does reformat crates/perry-hir/src/lower/builder_fold.rs, pre-existing drift on main, left alone here).

One thing worth knowing

CALLBACK_TIMERS is a process-global mutex, but agent::CURRENT_AGENT is thread-local and defaults to PRIMARY_AGENT, so every cargo test thread passes agent::owns(). A test that calls js_callback_timer_tick() can therefore drain and run another test thread's immediates. That is safe today only because nothing else in the perry-stdlib lib-test binary schedules callback timers (webcrypto/digest.rs schedules immediates but has no tests). Worth checking before a second timer-using test lands in this crate.

No version bump, no CHANGELOG.md edit; changelog fragment added under changelog.d/.

Summary by CodeRabbit

  • Tests

    • Updated asynchronous crypto.randomBytes coverage to verify callbacks execute on a later task rather than synchronously.
    • Added checks confirming callback timing and expected (error, buffer) results.
  • Documentation

    • Updated the changelog to reflect the test-only correction; runtime behavior is unchanged.

…timing

#6430 moved the async `randomBytes` callback onto a macrotask
(`js_set_immediate_callback_args`) to match Node, which runs `randomBytes`
on the libuv threadpool and fires `(err, buffer)` a loop iteration later.
`native_dispatch_random_bytes_value_form_fires_callback` predates that
change and still asserted the callback fired inline, so it has been red on
`main` ever since.

Update the test to match: assert the callback does not fire synchronously,
pump the callback-timer queue, then check the delivered arguments. This
locks in the macrotask timing instead of the behavior it replaced. The
dispatch path itself is unchanged.
@TheHypnoo
TheHypnoo force-pushed the fix/random-bytes-async-test-macrotask branch from 76d6653 to b17e095 Compare July 25, 2026 16:08
@coderabbitai

coderabbitai Bot commented Jul 25, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 505c980a-14eb-4e6b-bab8-b9050e250a63

📥 Commits

Reviewing files that changed from the base of the PR and between 8b2c736 and b17e095.

📒 Files selected for processing (2)
  • changelog.d/6838-random-bytes-async-test-macrotask.md
  • crates/perry-stdlib/src/crypto/random.rs

📝 Walkthrough

Walkthrough

The random bytes native dispatch test now verifies callback execution on a macrotask, advances the callback timer queue, and validates the callback arguments. The changelog documents the test-only correction and confirms no runtime behavior changed.

Changes

Random bytes callback timing

Layer / File(s) Summary
Asynchronous callback assertions
crates/perry-stdlib/src/crypto/random.rs, changelog.d/6838-random-bytes-async-test-macrotask.md
The test asserts non-inline callback execution, processes the callback timer queue, validates (err, buffer), and documents the test-only change.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Suggested reviewers: proggeramlug

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately describes the test-only randomBytes async callback timing fix.
Description check ✅ Passed The description covers the summary, root cause, change, and validation, with only minor omissions versus the template.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/random-bytes-async-test-macrotask

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

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

@TheHypnoo TheHypnoo added the bug Confirmed defect or regression label Jul 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Confirmed defect or regression

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant