Skip to content

Fix unhandled promise rejection tracker, again #1049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,8 @@ static JSValue js_promise_resolve(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv, int magic);
static JSValue js_promise_then(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv);
static JSValue js_promise_resolve_thenable_job(JSContext *ctx,
int argc, JSValueConst *argv);
static bool js_string_eq(JSString *p1, JSString *p2);
static int js_string_compare(JSString *p1, JSString *p2);
static int JS_SetPropertyValue(JSContext *ctx, JSValueConst this_obj,
Expand Down Expand Up @@ -50221,6 +50223,9 @@ static JSValue promise_rejection_tracker_job(JSContext *ctx, int argc,
JSRuntime *rt;
JSPromiseData *s;
JSValueConst promise;
struct list_head *el, *el1;
JSJobEntry *job;
bool has_other_promise_jobs;

assert(argc == 1);

Expand All @@ -50233,6 +50238,22 @@ static JSValue promise_rejection_tracker_job(JSContext *ctx, int argc,

promise_trace(ctx, "promise_rejection_tracker_job\n");

// Push the rejection tracker jobs to the end of the queue if there are other jobs.
// This allows us to handle rejections that get added later and thus would handle the
// rejection _after_ we check for it.
has_other_promise_jobs = false;
list_for_each_safe(el, el1, &rt->job_list) {
job = list_entry(el, JSJobEntry, link);
if (job->job_func == promise_reaction_job || job->job_func == js_promise_resolve_thenable_job) {
has_other_promise_jobs = true;
break;
}
}
if (has_other_promise_jobs) {
JS_EnqueueJob(ctx, promise_rejection_tracker_job, 1, &promise);
return JS_UNDEFINED;
}

// Check again in case the hook was removed.
if (rt->host_promise_rejection_tracker)
rt->host_promise_rejection_tracker(
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions tests/bug39/2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*---
flags: [qjs:track-promise-rejections]
---*/

const error = await Promise.resolve().then(() => Promise.reject('reject')).catch(e => e)
print('Got this error:', error)
7 changes: 7 additions & 0 deletions tests/bug39/3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*---
flags: [qjs:track-promise-rejections]
---*/

const promise = Promise.reject('reject')
const error = await Promise.resolve().then(() => promise).catch(e => e)
print('Got this error:', error)