Skip to content
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

Handle promise reject handler added later issue #959

Merged
merged 6 commits into from
Oct 12, 2018

Conversation

kevinkassimo
Copy link
Contributor

Closes #936
Rationale see #936 comments (handler implementation moved to TS side).

v8 emits kPromiseHandlerAddedAfterReject when a catch block is found in async function, that cancels a previous kPromiseRejectWithNoHandler. Postpone error handling and exit to the end of each event loop cycle. tests/async_error.ts.out actually now matches that running under Node. ("world" is printed first: error handling only happens after script running to its end)

An alternative C++ only implementation could be found here

@kevinkassimo kevinkassimo changed the title Handle promise handler added later issue Handle promise reject handler added later issue Oct 11, 2018
Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! I'm not sure about the API... but glad these tricky bugs are being nailed down.

@piscisaureus Please review.

libdeno/deno.h Outdated
@@ -59,6 +59,8 @@ int deno_execute(Deno* d, void* user_data, const char* js_filename,
// libdeno.recv() callback. Check deno_last_exception() for exception text.
int deno_respond(Deno* d, void* user_data, int32_t req_id, deno_buf buf);

void deno_check_promise_reject_events(Deno* d);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tests to libdeno/libdeno_test.cc

}
d->pending_promise_events = 0; // reset
if (result->Uint32Value(context).FromJust() == 1) {
exit(1);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking returned value and exit here is not quite ideal... However, if I were to use os.exit(...) from TS side in promiseErrorExaminer it would trigger segfault.

Not quite sure what I did wrong

@@ -322,6 +396,43 @@ bool Execute(v8::Local<v8::Context> context, const char* js_filename,
return ExecuteV8StringSource(context, js_filename, source);
}

v8::Local<v8::Object> InitializeConstants(v8::Isolate* isolate,
v8::Local<v8::Context> context) {
auto constants_val = v8::Object::New(isolate);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put these directly into the libdeno namespace, not libdeno.constants


auto promise_reject_event_val = v8::Object::New(isolate);
CHECK(promise_reject_event_val
->Set(context, deno::v8_str("kPromiseRejectWithNoHandler"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put this directly into the libdeno namespace, not libdeno.constants.promiseRejectEvents

assertOrSend(count === 2);
}
})();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice work!

js/libdeno.ts Outdated
setPromiseRejectHandler: (
handler: (
error: Error,
event: number,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about about

event: "RejectWithNoHandler" | "HandlerAddedAfterReject" | "ResolveAfterResolved" | "RejectAfterResolved"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Event types from v8 here are integers. Not quite sure if we want to covert to string reprs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only concern would be about performance.. presumably numbers are faster.
Is this hot code?

Copy link
Contributor Author

@kevinkassimo kevinkassimo Oct 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The handler is invoked twice for each caught promise reject. Not quite sure if it is considered hot enough

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think mapping the V8 numbers to strings in C++ isn't so bad. This will simply the libdeno JS interface quite a bit.

Copy link
Member

@ry ry Oct 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const char* PromiseRejectStr(enum v8::PromiseRejectEvent e) {
  switch (e) {
    case kPromiseRejectWithNoHandler:
      return "RejectWithNoHandler";
    case kPromiseHandlerAddedAfterReject:
      return "HandlerAddedAfterReject";
    case kPromiseResolveAfterResolved:
      return "ResolveAfterResolved";
    case kPromiseRejectAfterResolved:
      return "RejectAfterResolved";
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Added the function.

const promiseRejectEvents = libdeno.constants.promiseRejectEvents;

/* tslint:disable-next-line:no-any */
const promiseRejectMap = new Map<Promise<any>, string>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:%s/promiseRejectMap/rejectMap/g

/* tslint:disable-next-line:no-any */
const promiseRejectMap = new Map<Promise<any>, string>();
/* tslint:disable-next-line:no-any */
const otherPromiseErrorMap = new Map<Promise<any>, string>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%s/otherPromiseErrorMap/otherErrorMap/g

promiseRejectMap.clear();
return 1;
}
return 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deno.exit() requires message passing - which probably doesn't work in this state of error.

maybe we should add libdeno.exit() as a way to quickly exit.. but i think your solution here is fine. maybe make the return value boolean.

@ry ry requested a review from piscisaureus October 11, 2018 21:53
Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I'll wait for @piscisaureus to review.

@piscisaureus piscisaureus self-assigned this Oct 12, 2018
piscisaureus
piscisaureus previously approved these changes Oct 12, 2018
@piscisaureus piscisaureus dismissed their stale review October 12, 2018 18:16

too rubberstamp happy today

@ry ry merged commit 45d3b89 into denoland:master Oct 12, 2018
@kevinkassimo kevinkassimo deleted the error/async_js branch December 27, 2019 07:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants