Skip to content

Commit 071ce01

Browse files
committed
[JSC] Async stack trace collection should stop at a cycle of awaits
https://bugs.webkit.org/show_bug.cgi?id=328147 Reviewed by Yusuke Suzuki. An async function can wait on its own promise through Promise.all, allSettled, any or race. getAsyncStackTrace then walked the same generators forever: it never returned when the cycle had only builtin functions such as Array.fromAsync, and otherwise repeated the frames up to Error.stackTraceLimit. Such a cycle always goes through a combinator, so remember the visited ones and stop at the second visit. Chains of plain awaits do no extra work. Test: JSTests/stress/async-stack-trace-cyclic-await.js * JSTests/stress/async-stack-trace-cyclic-await.js: Added. (shouldBe): (async leaf): (asyncFrames): (string_appeared_here.shouldBe.asyncFrames.async a): (string_appeared_here.shouldBe.asyncFrames.async b): (string_appeared_here.shouldBe.asyncFrames.async c): (string_appeared_here.shouldBe.asyncFrames): (shouldBe.asyncFrames.async a): * Source/JavaScriptCore/interpreter/Interpreter.cpp: (JSC::Interpreter::getAsyncStackTrace): Canonical link: https://commits.webkit.org/324511@main
1 parent f59345e commit 071ce01

2 files changed

Lines changed: 72 additions & 1 deletion

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//@ requireOptions("--useAsyncStackTrace=1")
2+
3+
function shouldBe(actual, expected) {
4+
if (actual !== expected)
5+
throw new Error(`expected "${expected}" but got "${actual}"`);
6+
}
7+
8+
let stack;
9+
async function leaf() {
10+
await 0;
11+
await 0;
12+
stack = new Error().stack;
13+
}
14+
15+
function asyncFrames(run) {
16+
stack = undefined;
17+
run();
18+
drainMicrotasks();
19+
return stack.split("\n").filter(line => line.startsWith("async ")).map(line => line.replace(/@.*/, "")).join();
20+
}
21+
22+
for (const combinator of ["all", "allSettled", "any", "race"]) {
23+
shouldBe(asyncFrames(() => {
24+
async function a() { await 0; await Promise[combinator]([leaf(), promise]); }
25+
const promise = a();
26+
}), "async a");
27+
28+
shouldBe(asyncFrames(() => {
29+
async function a() { await 0; await Promise[combinator]([promise, leaf()]); }
30+
async function b() { await a(); }
31+
const promise = b();
32+
}), "async a,async b");
33+
34+
shouldBe(asyncFrames(() => {
35+
async function a() { await 0; await Promise[combinator]([c(), promise]); }
36+
async function b() { await a(); }
37+
async function c() { await leaf(); }
38+
const promise = b();
39+
}), "async c,async a,async b");
40+
41+
shouldBe(asyncFrames(() => {
42+
async function a() { await 0; await Promise[combinator]([b(), promise]); }
43+
async function b() { await Promise[combinator]([leaf(), promise]); }
44+
const promise = a();
45+
}), "async b,async a");
46+
47+
shouldBe(asyncFrames(() => {
48+
const promise = Array.fromAsync({ length: 2, 0: 0, get 1() { return Promise[combinator]([leaf(), promise]); } });
49+
}), "");
50+
51+
shouldBe(asyncFrames(() => {
52+
async function a() { await Array.fromAsync({ length: 2, 0: 0, get 1() { return Promise[combinator]([leaf(), promise]); } }); }
53+
const promise = a();
54+
}), "async a");
55+
56+
shouldBe(asyncFrames(() => {
57+
async function a(depth) { await Promise[combinator]([depth ? a(depth - 1) : leaf()]); }
58+
a(3);
59+
}), "async a,async a,async a,async a");
60+
}
61+
62+
Error.stackTraceLimit = Infinity;
63+
shouldBe(asyncFrames(() => {
64+
async function a() { await 0; await Promise.all([leaf(), promise]); }
65+
const promise = a();
66+
}), "async a");

‎Source/JavaScriptCore/interpreter/Interpreter.cpp‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
#include <stdio.h>
9191
#include <wtf/NeverDestroyed.h>
9292
#include <wtf/Scope.h>
93+
#include <wtf/SmallSet.h>
9394
#include <wtf/StdLibExtras.h>
9495
#include <wtf/text/MakeString.h>
9596
#include <wtf/text/StringBuilder.h>
@@ -493,7 +494,8 @@ void Interpreter::getAsyncStackTrace(JSCell* owner, Vector<StackFrame>& results,
493494
return promise->asyncStackTraceContext();
494495
};
495496

496-
auto getParentGenerator = [&](JSAsyncFunctionGenerator* gen) -> JSAsyncFunctionGenerator* {
497+
SmallSet<JSCell*> visitedCombinators;
498+
auto getParentGenerator = [&](JSAsyncFunctionGenerator* gen) ALWAYS_INLINE_LAMBDA -> JSAsyncFunctionGenerator* {
497499
JSValue generatorContext = gen->internalField(static_cast<unsigned>(JSAsyncFunctionGenerator::Field::Context)).get();
498500
ASSERT(generatorContext);
499501
JSPromise* awaitedPromise = dynamicDowncast<JSPromise>(generatorContext);
@@ -506,6 +508,9 @@ void Interpreter::getAsyncStackTrace(JSCell* owner, Vector<StackFrame>& results,
506508
if (auto* generator = dynamicDowncast<JSAsyncFunctionGenerator>(promiseContext))
507509
return generator;
508510

511+
if (!promiseContext.isCell() || !visitedCombinators.add(promiseContext.asCell()).isNewEntry)
512+
return nullptr;
513+
509514
// handle `Promise.all`, `Promise.allSettled`, and `Promise.any`
510515
if (auto* globalContext = dynamicDowncast<JSPromiseCombinatorsGlobalContext>(promiseContext)) {
511516
JSValue promiseValue = globalContext->promise();

0 commit comments

Comments
 (0)