await bypasses catch when PromiseResolve throws

Open
#563 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 5 days

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c, javascript
Domain
compilers

Research direction

Start in quickjs.c at js_async_function_resume() and trace how PromiseResolve exceptions are handled when resuming an async function. Run the supplied qjs command at commit d73189d; done means the reproducer prints “caught: boom” instead of an unhandled rejection, while preserving normal async-function behavior.

Written by the indexing model from the issue text.

Description

Reproducer with the standalone qjs CLI (reproduced at d73189d, no embedding code):

./qjs -e 'const p = Promise.resolve(1);
Object.defineProperty(p, "constructor", { get() { throw Error("boom"); } });
(async () => {
    try { await p; } catch (e) { print("caught: " + e.message); }
})();'

Instead of entering catch, qjs reports Possibly unhandled promise rejection: Error: boom.

Expected behavior

Output: caught: boom. PromiseResolve propagates an exception thrown by the promise's constructor getter; Await propagates that abrupt completion at the await expression, where the surrounding try/catch handles it. QuickJS already resumes an async generator with throw_flag when preparing its await fails (js_async_generator_resume_next()); Node.js 25 also prints caught: boom for the equivalent example.

Possible patch

In js_async_function_resume(), throw the PromiseResolve exception into the suspended async function instead of rejecting its outer promise before its catch can run:

diff --git a/quickjs.c b/quickjs.c
--- a/quickjs.c
+++ b/quickjs.c
@@ -21056,1 +21056,2 @@ static void js_async_function_resume(JSContext *ctx, JSAsyncFunctionState *s)
+ resume:
     func_ret = async_func_resume(ctx, s);
@@ -21082,8 +21083,10 @@ static void js_async_function_resume(JSContext *ctx, JSAsyncFunctionState *s)
         promise = js_promise_resolve(ctx, ctx->promise_ctor,
                                      1, (JSValueConst *)&value, 0);
         JS_FreeValue(ctx, value);
-        if (JS_IsException(promise))
-            goto fail;
+        if (JS_IsException(promise)) {
+            s->throw_flag = TRUE;
+            goto resume;
+        }
Dominant language
C
Stars
11.1k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from bellard/quickjs

All issues in bellard/quickjs

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.