Auto merge of #18514 - asajeffrey:layout-dont-panic-if-no-iframe-bc, r=emilio

Remove sources of panic when laying out an iframe without a nested browsing context

<!-- Please describe your changes on the following line: -->

At the moment, layout panics if it discovers an iframe without a nested browsing context. Under normal circumstances, this is reasonable, but it requires very tight synchronization between script, layout, the constellation and the compositor. In particular, if a layout is in progress when an iframe's browsing context is discarded, this can trigger panic.

This PR fixes this in two ways:

1. Making the pipeline and browsing context ids optional in layout's representation of an iframe.
2. Shutting down layout before discarding a browsing context, rather than after.

This is belt and braces.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #17482 and #18477
- [X] These changes do not require tests because the PR is fixing a panic caused by a race condition

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/18514)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2017-09-22 09:34:12 -05:00 committed by GitHub
commit c48ef50b72
6 changed files with 58 additions and 30 deletions

View file

@ -1024,8 +1024,8 @@ pub trait LayoutNodeHelpers {
fn image_url(&self) -> Option<ServoUrl>;
fn canvas_data(&self) -> Option<HTMLCanvasData>;
fn svg_data(&self) -> Option<SVGSVGData>;
fn iframe_browsing_context_id(&self) -> BrowsingContextId;
fn iframe_pipeline_id(&self) -> PipelineId;
fn iframe_browsing_context_id(&self) -> Option<BrowsingContextId>;
fn iframe_pipeline_id(&self) -> Option<PipelineId>;
fn opaque(&self) -> OpaqueNode;
}
@ -1175,16 +1175,16 @@ impl LayoutNodeHelpers for LayoutJS<Node> {
.map(|svg| svg.data())
}
fn iframe_browsing_context_id(&self) -> BrowsingContextId {
fn iframe_browsing_context_id(&self) -> Option<BrowsingContextId> {
let iframe_element = self.downcast::<HTMLIFrameElement>()
.expect("not an iframe element!");
iframe_element.browsing_context_id().unwrap()
iframe_element.browsing_context_id()
}
fn iframe_pipeline_id(&self) -> PipelineId {
fn iframe_pipeline_id(&self) -> Option<PipelineId> {
let iframe_element = self.downcast::<HTMLIFrameElement>()
.expect("not an iframe element!");
iframe_element.pipeline_id().unwrap()
iframe_element.pipeline_id()
}
#[allow(unsafe_code)]

View file

@ -1750,10 +1750,35 @@ impl ScriptThread {
load.pipeline_id == id
});
let document = self.documents.borrow_mut().remove(id);
// We should never have a pipeline that's still an incomplete load,
// but also has a Document.
debug_assert!(idx.is_none() || document.is_none());
// Remove any incomplete load.
let chan = if let Some(idx) = idx {
let load = self.incomplete_loads.borrow_mut().remove(idx);
load.layout_chan.clone()
} else if let Some(document) = self.documents.borrow_mut().remove(id) {
} else if let Some(ref document) = document {
document.window().layout_chan().clone()
} else {
return warn!("Exiting nonexistant pipeline {}.", id);
};
// We shut down layout before removing the document,
// since layout might still be in the middle of laying it out.
debug!("preparing to shut down layout for page {}", id);
let (response_chan, response_port) = channel();
chan.send(message::Msg::PrepareToExit(response_chan)).ok();
let _ = response_port.recv();
debug!("shutting down layout for page {}", id);
chan.send(message::Msg::ExitNow).ok();
self.script_sender.send((id, ScriptMsg::PipelineExited)).ok();
// Now that layout is shut down, it's OK to remove the document.
if let Some(document) = document {
// We don't want to dispatch `mouseout` event pointing to non-existing element
if let Some(target) = self.topmost_mouse_over_target.get() {
if target.upcast::<Node>().owner_doc() == document {
@ -1761,22 +1786,14 @@ impl ScriptThread {
}
}
// We discard the browsing context after requesting layout shut down,
// to avoid running layout on detached iframes.
let window = document.window();
if discard_bc == DiscardBrowsingContext::Yes {
window.window_proxy().discard_browsing_context();
}
window.clear_js_runtime();
window.layout_chan().clone()
} else {
return warn!("Exiting nonexistant pipeline {}.", id);
};
let (response_chan, response_port) = channel();
chan.send(message::Msg::PrepareToExit(response_chan)).ok();
debug!("shutting down layout for page {}", id);
let _ = response_port.recv();
chan.send(message::Msg::ExitNow).ok();
self.script_sender.send((id, ScriptMsg::PipelineExited)).ok();
}
debug!("Exited pipeline {}.", id);
}