Repository navigation
fix(ext/node): prevent top-level await test(...) deadlock in node:test - #33947
Conversation
The `test()` and `suite()` functions in `node:test` returned a promise that only resolved when the underlying `Deno.test()` callback executed. Because `Deno.test()` defers execution until after the module finishes evaluating, top-level `await test(...)` would block module evaluation forever, producing "Top-level await promise never resolved". Return an already-resolved promise from the top-level entry points so the await unblocks. The test still runs and is reported by the Deno test runner. Fixes #32326
|
|
fibibot
left a comment
There was a problem hiding this comment.
Two issues — one mechanical CI fix, one substance question.
Mechanical: CI is red because of a missing trailing newline
All 14 test specs shards fail on the new node_test_top_level_await::suite_await spec test. The CI log on test specs (1/2) debug linux-x86_64 (job id 75243625358) shows the actual output matching the expected output character-for-character — <matched everything> per the matcher debug. The failure reason is:
!!!! PROBLEM: Missing final newline at end of expected output !!!!
The tests/specs/node/node_test_top_level_await/suite.out file is missing its trailing newline. (test.out apparently has it — test_await passes; suite_await fails the same matcher.) Adding a \n to the end of suite.out should clear this. The runner is strict about it because spec output files without trailing newlines have historically masked real assertion drift.
Substance: test() returning resolved-immediately diverges from Node
See inline. Not necessarily a blocker, but worth a maintainer's call.
| // completion: top-level `await test(...)` would deadlock the module. The | ||
| // test still runs and is reported by the runner; we return an already | ||
| // resolved promise so the await unblocks. | ||
| return PromiseResolve(); |
There was a problem hiding this comment.
The fix unblocks the deadlock — agreed that's a real bug worth fixing. But returning PromiseResolve() immediately changes the semantic contract of node:test's test() in a way that diverges from Node. Per Node's docs:
Returns: <Promise> Fulfilled with
undefinedonce the test completes...
With this PR, the following Node pattern produces different behavior:
import test from 'node:test';
await test('first', async (t) => { /* slow async work */ });
console.log('AFTER FIRST'); // Node: runs AFTER first completes. Deno (after PR): runs BEFORE first completes.
await test('second', async (t) => { /* depends on side-effects of first */ });In Node, the console.log line runs after first completes; user code can rely on side-effects of the awaited test. In Deno after this PR, both awaits unblock immediately and console.log and second race against first's actual execution (which the Deno runner schedules after module evaluation).
Correctness-wise the issue tracker (#32326) is a deadlock and a clear bug, so this fix is net-positive. But it introduces a different subtle bug: code that awaits test() for sequencing purposes will silently run out of order. Worth either:
- Documenting the divergence in the JSDoc on
prepareDenoTest, so future readers understand why this isn't matching Node. - Tracking the deeper fix (driving the test runner forward eagerly when
await test()is hit at top-level, so the awaited promise can actually resolve on completion). The comment at:579hints at this — "only executes registered tests after the module has finished evaluating" — and a follow-up issue would let users know there's a known semantic gap.
Neither is blocking; the deadlock fix is the right priority. Just want a maintainer to weigh in on whether the divergence needs an issue/docstring before merge.
- Add trailing newline to suite.out so the spec runner matches the
two trailing blank lines in `deno test` suite output.
- Make the inline comment in `prepareDenoTest` explicit about diverging
from Node's contract (Node resolves the returned promise on
completion; we resolve immediately to avoid deadlock) and call out
the ordering trade-off for `await test('a'); await test('b')`.
|
Thanks for the review.
|
The expected output was missing the trailing blank line that `deno test` emits after the summary, causing the test_await spec test to fail on CI.
|
Build infra flake — all 6 failed jobs hit |
…est (denoland#33947) Fixes denoland#32326. The `test()` and `suite()` entry points in `node:test` returned a promise that only resolved when the underlying `Deno.test()` callback executed. Because `Deno.test()` defers execution until after the module finishes evaluating, the following common Node.js pattern would deadlock: ```js import test from "node:test"; import assert from "node:assert"; await test("a test", () => { assert.strictEqual(1 + 1, 2); }); ``` The module could not finish evaluating because of the `await`, and the test could not start because the module had not finished evaluating, producing `error: Top-level await promise never resolved`. The fix returns an already-resolved promise from `prepareDenoTest` and `prepareDenoTestForSuite`. The test/suite is still registered with the Deno test runner and runs (and is reported) normally; only the awaited promise no longer waits for completion. Subtests and `await t.test(...)` inside a parent test are unaffected because they go through `denoContext.step()`, which executes synchronously inside the parent.
Summary
Fixes #32326.
The
test()andsuite()entry points innode:testreturned a promisethat only resolved when the underlying
Deno.test()callback executed.Because
Deno.test()defers execution until after the module finishesevaluating, the following common Node.js pattern would deadlock:
The module could not finish evaluating because of the
await, and thetest could not start because the module had not finished evaluating,
producing
error: Top-level await promise never resolved.The fix returns an already-resolved promise from
prepareDenoTestandprepareDenoTestForSuite. The test/suite is still registered with theDeno test runner and runs (and is reported) normally; only the awaited
promise no longer waits for completion. Subtests and
await t.test(...)inside a parent test are unaffected because they go through
denoContext.step(), which executes synchronously inside the parent.Test plan
await test(...)calls register and run, including failures.await suite(...)works the same way.await t.test(...)still work.tests/specs/node/node_test_*spec tests still produce the same output.tests/specs/node/node_test_top_level_await/.Generated by Claude Code