Skip to content

Implement unhandledrejection event - #20755

Merged
bors-servo merged 20 commits into
servo:masterfrom
CYBAI:unhandled-rejection
Oct 23, 2018
Merged

bors-servo merged 20 commits into
servo:masterfrom
CYBAI:unhandled-rejection

Conversation

@CYBAI

@CYBAI CYBAI commented May 6, 2018 •

Copy link
Copy Markdown
Member


This change is Reviewable

@highfive

highfive commented May 6, 2018

Copy link
Copy Markdown

Heads up! This PR modifies the following files:

  • @asajeffrey: components/script/dom/bindings/codegen/CodegenRust.py, components/script/dom/promiserejectionevent.rs, components/script/dom/webidls/PromiseRejectionEvent.webidl, components/script/dom/globalscope.rs, components/script/dom/promisedebugging.rs and 2 more
  • @fitzgen: components/script/dom/bindings/codegen/CodegenRust.py, components/script/dom/promiserejectionevent.rs, components/script/dom/webidls/PromiseRejectionEvent.webidl, components/script/dom/globalscope.rs, components/script/dom/promisedebugging.rs and 2 more
  • @KiChjang: components/script/dom/bindings/codegen/CodegenRust.py, components/script/dom/promiserejectionevent.rs, components/script/dom/webidls/PromiseRejectionEvent.webidl, components/script/dom/globalscope.rs, components/script/dom/promisedebugging.rs and 2 more

@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label May 6, 2018
@highfive

highfive commented May 6, 2018

Copy link
Copy Markdown

warning Warning warning

  • These commits modify unsafe code. Please review it carefully!
  • These commits modify script code, but no tests are modified. Please consider adding a test!

EventCancelable::Cancelable,
// FIXME(CYBAI): Use real rejected `promise` and `reason`
Promise::new(&global.upcast::<GlobalScope>()),
HandleValue::null()

@CYBAI CYBAI May 6, 2018 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@jdm , @nox :

I'm stuck by how to convert a HandleObject into a Rc<Promise> and Value into HandleValue here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We can use Promise::new_with_js_promise here if we make the method public.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, new_with_js_promise is what I should use and just make it as a public method.

Previously, I also tried this method but the assert inside new_with_js_promise complained about that the passed HandleObject is not Promise. But now it works fine! I think I used wrong HandleObject before 😓

let result = GetPromiseResult(promise);
rooted!(in(cx) let object = result.to_object());
let error_report = JS_ErrorFromException(cx, object.handle());
println!("{:?}, {:?}", result, error_report);

ghost May 7, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would expect this to look like:

rooted!(in(cx) let result = GetPromiseResult(promise));

We can then use result.handle() to object a HandleValue. I don't think there's any reason to expect an exception object here.

ghost May 7, 2018 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@jdm Yeah! With using rooted macro, it works that I can cast the type of result to be HandleValue! Thanks a lot!

About the exception object, we don't need it for unhandlerejection so just removed it!

EventCancelable::Cancelable,
// FIXME(CYBAI): Use real rejected `promise` and `reason`
Promise::new(&global.upcast::<GlobalScope>()),
HandleValue::null()

ghost May 7, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We can use Promise::new_with_js_promise here if we make the method public.

@jdm jdm added S-needs-code-changes Changes have not yet been made that were requested by a reviewer. and removed S-awaiting-review There is new code that needs to be reviewed. labels May 7, 2018
@CYBAI
CYBAI force-pushed the unhandled-rejection branch from 344823d to 583beaa Compare May 7, 2018 14:30
@highfive highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-needs-code-changes Changes have not yet been made that were requested by a reviewer. labels May 7, 2018
@CYBAI
CYBAI force-pushed the unhandled-rejection branch from 583beaa to 3071ff5 Compare May 12, 2018 16:54
global.set_uncaught_rejections(promise);

unsafe {
PromiseDebugging::flush_uncaught_rejections_internal(global);

ghost May 12, 2018

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@jdm cc @nox

In Gecko, flush uncaught rejections internal will only be triggered in FlushSync.

So, I'd like to confirm if I'm correct about doing the similar implementation in Servo like following:

  • Make the received uncaught rejection be queued into script thread with task which should be the step 4 in notify-about-rejected-promises spec instead of running the flush_uncaught_rejections_internal directly when AddUncaughtRejection executed

ghost May 19, 2018

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@jdm ping for asking question, thanks!

ghost May 22, 2018

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I will try to use task macro to handle Queue a task for unhandled rejection.

ghost May 28, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry for not responding. Yes, queuing a task using the task macro is the right way to implement step 4. However, that task should only be queued once per run of the event loop, and I believe AddUncaughtRejection can run an arbitrary number of times per run of the event loop. You'll want to invoke the notify about rejected promises algorithm as part of the perform a microtask checkpoint algorithm.

@CYBAI
CYBAI force-pushed the unhandled-rejection branch from 3071ff5 to aa0254d Compare May 20, 2018 06:49
}
}

pub fn new_uninitialized(global: &GlobalScope) -> DomRoot<Self> {

ghost May 29, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's remove this method and add the initial values as arguments to new_inherited.

pub struct PromiseRejectionEvent {
event: Event,
#[ignore_malloc_size_of = "Rc"]
promise: DomRefCell<Rc<Promise>>,

ghost May 29, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We shouldn't need the DomRefCell if we pass the initial promise value to new_inherited.

ghost May 30, 2018

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I will try to not use the DomRefCell here!

};

dictionary PromiseRejectionEventInit : EventInit {
Promise<any> promise;

ghost May 29, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's add /* required */ here, since it's in the spec but we don't support it yet.

Comment thread components/script/dom/globalscope.rs Outdated
GlobalScope::from_object(obj)
}

pub fn set_uncaught_rejections(&self, rejection: HandleObject) {

ghost May 29, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's call this add_uncaught_rejection.

ghost May 30, 2018

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Indeed, using add as prefix for this function makes more sense! Thanks!

Comment thread components/script/dom/globalscope.rs Outdated
&self.uncaught_rejections
}

pub fn set_consumed_rejections(&self, rejection: HandleObject) {

ghost May 29, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's call this add_consumed_rejection.

}

#[allow(unsafe_code)]
pub unsafe fn flush_uncaught_rejections_internal(global: DomRoot<GlobalScope>) {

ghost May 29, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's move this function into script_runtime.rs; there's no need for the promisedebugging.rs file to exist. We should add a comment pointing at https://html.spec.whatwg.org/multipage/webappapis.html#notify-about-rejected-promises and call this function notify_about_rejected_promises. We will need to call it from MicrotaskQueue::checkpoint in the TODO in step 8.

ghost Jun 3, 2018

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@jdm cc @KiChjang

For this comment, I still doesn't implement it yet due to below question:

I tried to figure out how to call notify_about_rejected_promises inside MicrotaskQueue::checkpoint.
Due to GlobalScope as an argument in notify_about_rejected_promises, we'll need to pass a GlobalScope from MicrotaskQueue::checkpoint but it seems that we don't have a way to get GlobalScope in MicrotaskQueue. So, maybe we can only pass a GlobalScope to it.

Ex.

pub fn checkpoint<F>(&self, global: GlobalScope, target_provider: F)
    where F: Fn(PipelineId) -> Option<DomRoot<GlobalScope>>
{

However, it will have an issue that we'll call MicrotaskQueue::checkpoint in two places:

One is in ScriptThread:

fn perform_a_microtask_checkpoint(&self) {
self.microtask_queue.checkpoint(|id| self.documents.borrow().find_global(id))
}

and the other is in GlobalScope:

/// Perform a microtask checkpoint.
pub fn perform_a_microtask_checkpoint(&self) {
self.microtask_queue.checkpoint(|_| Some(DomRoot::from_ref(self)));
}

We must be able to get the globalscope inside GlobalScope module but we don't have globalscope under every ScriptThread.

How can we have GlobalScope for both caller places and pass it to notify_about_rejected_promises?
Do you have any advises?

ghost Jun 3, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

While investigating this question, I discovered and filed #20908. Since step 3 says we need to notify about rejected promises for each global associated with the script thread, I propose that the checkpoint method should accept a Vec<DomRoot<GlobalScope>> argument and iterate over it to notify all of the contained globals. For ScriptThread, this can be created by iterating over all known documents and getting their global. For GlobalScope, it can be a vector consisting of the self object.

ghost Jun 3, 2018

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Passing an vector of GlobalScope sounds reasonable to me and I will try it.

But, my only concern to the solution is that, in script thread, if there's no any global in it, then it won't trigger notify_about_rejected_promises method from checkpoint method, is this reasonable?
Or, that said, after having a solution for it like #20908 mentioned; then, we will have corresponding global in each checkpoint.

ghost Jun 3, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I can't think of a way to have a script thread with no globals while still having rejected promises that require notification.

ghost Jun 4, 2018

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Indeed. While having rejected promises, it should have its own globals.
I will try to use the vector way to implement it, thanks!

Comment thread components/script/script_runtime.rs Resolved
Comment thread components/script/script_runtime.rs Outdated
match state {
PromiseRejectionHandlingState::Unhandled => {
let global = GlobalScope::from_context(cx);
PromiseDebugging::AddUncaughtRejection(global, promise);

ghost May 29, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We can call global.add_uncaught_rejection directly instead of using PromiseDebugging.

pub unsafe fn flush_uncaught_rejections_internal(global: DomRoot<GlobalScope>) {
let cx = global.get_cx();

global.get_uncaught_rejections().borrow().iter().for_each(|promise: &Box<Heap<*mut JSObject>>| {

ghost May 29, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For the task from step 4, we need to create a RootedVec to hold the list of promises before we clear the original list. We can then use that vector in the task, rather than global.get_uncaught_rejections().

ghost Jun 3, 2018

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I will try to address this comment when I call notify_about_rejected_promises inside MicrotaskQueue::checkpoint successfully.

@jdm jdm added S-needs-squash Some (or all) of the commits in the PR should be combined. S-needs-code-changes Changes have not yet been made that were requested by a reviewer. and removed S-awaiting-review There is new code that needs to be reviewed. S-needs-squash Some (or all) of the commits in the PR should be combined. labels May 29, 2018
@CYBAI
CYBAI force-pushed the unhandled-rejection branch from aa0254d to 0c9042a Compare June 3, 2018 16:56
@highfive highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-needs-code-changes Changes have not yet been made that were requested by a reviewer. labels Jun 3, 2018
@jdm jdm added S-needs-code-changes Changes have not yet been made that were requested by a reviewer. and removed S-awaiting-review There is new code that needs to be reviewed. labels Jun 4, 2018
@highfive highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-needs-code-changes Changes have not yet been made that were requested by a reviewer. labels Jun 5, 2018
@CYBAI
CYBAI force-pushed the unhandled-rejection branch 2 times, most recently from 5a46ecf to eb7d954 Compare June 5, 2018 16:41

ghost left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks really close!

reason: HandleValue
) -> DomRoot<Self> {
let ev = reflect_dom_object(
Box::new(PromiseRejectionEvent::new_inherited(promise.clone())),

ghost Jun 6, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: no need for the clone.

Comment thread components/script/script_runtime.rs Outdated

#[allow(unsafe_code, unrooted_must_root)]
/// https://html.spec.whatwg.org/multipage/#notify-about-rejected-promises
pub unsafe fn notify_about_rejected_promises(global: DomRoot<GlobalScope>) {

ghost Jun 6, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's take &GlobalScope as an argument instead. And instead of making this function unsafe, we should use unsafe blocks in the implementation.

Comment thread components/script/script_runtime.rs Outdated
atom!("unhandledrejection"),
EventBubbles::DoesNotBubble,
EventCancelable::Cancelable,
promise.duplicate(),

ghost Jun 6, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we just pass promise instead?

Comment thread components/script/script_thread.rs Outdated
|id| self.documents.borrow().find_global(id),
self.documents.borrow()
.iter()
.filter_map(|(id, _doc)| self.documents.borrow().find_global(id))

ghost Jun 6, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What if we use map and return document.global() instead?

ghost Jun 7, 2018

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oh! Sounds good idea! I will try it! Thanks!

@servo-wpt-sync

ghost commented Oct 18, 2018

Copy link
Copy Markdown
Collaborator

Transplanted upstreamable changes to existing PR.

Completed upstream sync of web-platform-test changes at web-platform-tests/wpt#13506.

@CYBAI

ghost commented Oct 18, 2018

Copy link
Copy Markdown
Member Author

@bors-servo try

  • Both wpt tests and the PromiseIsHandled updated. I think this PR is ready for review!

@bors-servo

ghost commented Oct 18, 2018

Copy link
Copy Markdown
Contributor

⌛ Trying commit 24f1afd with merge bed2649...

@bors-servo

ghost commented Oct 18, 2018

Copy link
Copy Markdown
Contributor

💔 Test failed - linux-rel-wpt

@CYBAI

ghost commented Oct 18, 2018

Copy link
Copy Markdown
Member Author

@bors-servo retry

  • linux-rel-wpt failed with unrelated test (/_mozilla/css/incremental_inline_layout_a.html) and some other mac-rel tests failed with network issue when filter-intermittents

@bors-servo

ghost commented Oct 18, 2018

Copy link
Copy Markdown
Contributor

⌛ Trying commit 24f1afd with merge 2cd5dc8...

@bors-servo

ghost commented Oct 18, 2018

Copy link
Copy Markdown
Contributor

💔 Test failed - linux-rel-css

@ferjm

ghost commented Oct 18, 2018

Copy link
Copy Markdown
Contributor

@bors-servo retry

@bors-servo

ghost commented Oct 18, 2018

Copy link
Copy Markdown
Contributor

⌛ Trying commit 24f1afd with merge 18d7ad8...

@bors-servo

ghost commented Oct 18, 2018

Copy link
Copy Markdown
Contributor

💔 Test failed - linux-rel-wpt

@jdm

ghost commented Oct 18, 2018

Copy link
Copy Markdown
Member

@bors-servo retry

  • oom

@bors-servo

ghost commented Oct 18, 2018

Copy link
Copy Markdown
Contributor

⌛ Trying commit 24f1afd with merge a371f16...

@bors-servo

ghost commented Oct 18, 2018

Copy link
Copy Markdown
Contributor

☀️ Test successful - android, android-mac, android-x86, arm32, arm64, linux-dev, linux-rel-css, linux-rel-wpt, mac-dev-unit, mac-rel-css1, mac-rel-css2, mac-rel-wpt1, mac-rel-wpt2, mac-rel-wpt3, mac-rel-wpt4, status-taskcluster, windows-msvc-dev
State: approved= try=True

@jdm

ghost commented Oct 22, 2018

Copy link
Copy Markdown
Member

@bors-servo r+

@bors-servo

ghost commented Oct 22, 2018

Copy link
Copy Markdown
Contributor

📌 Commit 24f1afd has been approved by jdm

@CYBAI

ghost commented Oct 23, 2018

Copy link
Copy Markdown
Member Author

@bors-servo ping

@bors-servo

ghost commented Oct 23, 2018

Copy link
Copy Markdown
Contributor

😪 I'm awake I'm awake

@bors-servo

ghost commented Oct 23, 2018

Copy link
Copy Markdown
Contributor

⌛ Testing commit 24f1afd with merge 759fee8...

@SimonSapin

ghost commented Oct 23, 2018

Copy link
Copy Markdown
Member

This is still shown as "pending" in Homu’s queue although Buildbot doesn’t show any running jobs. Let’s try to get things unstuck.

@servo-wpt-sync

ghost commented Oct 23, 2018

Copy link
Copy Markdown
Collaborator

Opened new PR for upstreamable changes.

Completed upstream sync of web-platform-test changes at web-platform-tests/wpt#13681.

@CYBAI

ghost commented Oct 23, 2018 •

Copy link
Copy Markdown
Member Author

Hmm.. looks like homu still pend this PR 🤔

@jdm

ghost commented Oct 23, 2018

Copy link
Copy Markdown
Member

@bors-servo retry

@bors-servo

ghost commented Oct 23, 2018

Copy link
Copy Markdown
Contributor

⌛ Testing commit 24f1afd with merge 30d9962...

@bors-servo

ghost commented Oct 23, 2018

Copy link
Copy Markdown
Contributor

☀️ Test successful - android, android-mac, android-x86, arm32, arm64, linux-dev, linux-rel-css, linux-rel-wpt, mac-dev-unit, mac-rel-css1, mac-rel-css2, mac-rel-wpt1, mac-rel-wpt2, mac-rel-wpt3, mac-rel-wpt4, status-taskcluster, windows-msvc-dev
Approved by: jdm
Pushing 30d9962 to master...

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.

Notify global object about rejected promises