Auto merge of #10587 - asajeffrey:add-failure-panic-message, r=Manishearth

Added panic message to failures.

Added the panic message to failures. This is a step towards #10334, since it gives us access to the panic error message when we fire a `mozbrowsererror` event. The remaining steps are also to record the backtrace, and to report the failure in the event.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/10587)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-04-14 19:55:17 +05:30
commit 2b910678db
9 changed files with 88 additions and 37 deletions

View file

@ -653,9 +653,9 @@ impl<LTF: LayoutThreadFactory, STF: ScriptThreadFactory> Constellation<LTF, STF>
// Messages from script
Request::Script(FromScriptMsg::Failure(Failure { pipeline_id, parent_info })) => {
debug!("handling script failure message from pipeline {:?}, {:?}", pipeline_id, parent_info);
self.handle_failure_msg(pipeline_id, parent_info);
Request::Script(FromScriptMsg::Failure(failure)) => {
debug!("handling script failure message from pipeline {:?}", failure);
self.handle_failure_msg(failure);
}
Request::Script(FromScriptMsg::ScriptLoadedURLInIFrame(load_info)) => {
debug!("constellation got iframe URL load message {:?} {:?} {:?}",
@ -786,9 +786,9 @@ impl<LTF: LayoutThreadFactory, STF: ScriptThreadFactory> Constellation<LTF, STF>
Request::Layout(FromLayoutMsg::ChangeRunningAnimationsState(pipeline_id, animation_state)) => {
self.handle_change_running_animations_state(pipeline_id, animation_state)
}
Request::Layout(FromLayoutMsg::Failure(Failure { pipeline_id, parent_info })) => {
debug!("handling paint failure message from pipeline {:?}, {:?}", pipeline_id, parent_info);
self.handle_failure_msg(pipeline_id, parent_info);
Request::Layout(FromLayoutMsg::Failure(failure)) => {
debug!("handling paint failure message from pipeline {:?}", failure);
self.handle_failure_msg(failure);
}
Request::Layout(FromLayoutMsg::SetCursor(cursor)) => {
self.handle_set_cursor_msg(cursor)
@ -803,9 +803,9 @@ impl<LTF: LayoutThreadFactory, STF: ScriptThreadFactory> Constellation<LTF, STF>
// Notification that painting has finished and is requesting permission to paint.
Request::Paint(FromPaintMsg::Failure(Failure { pipeline_id, parent_info })) => {
debug!("handling paint failure message from pipeline {:?}, {:?}", pipeline_id, parent_info);
self.handle_failure_msg(pipeline_id, parent_info);
Request::Paint(FromPaintMsg::Failure(failure)) => {
debug!("handling paint failure message from pipeline {:?}", failure);
self.handle_failure_msg(failure);
}
}
@ -835,12 +835,11 @@ impl<LTF: LayoutThreadFactory, STF: ScriptThreadFactory> Constellation<LTF, STF>
Some(pipeline) => pipeline.parent_info,
};
// Treat send error the same as receiving a failure message
self.handle_failure_msg(pipeline_id, parent_info);
let failure = Failure::new(pipeline_id, parent_info);
self.handle_failure_msg(failure);
}
fn handle_failure_msg(&mut self,
pipeline_id: PipelineId,
parent_info: Option<(PipelineId, SubpageId)>) {
fn handle_failure_msg(&mut self, failure: Failure) {
if opts::get().hard_fail {
// It's quite difficult to make Servo exit cleanly if some threads have failed.
// Hard fail exists for test runners so we crash and that's good enough.
@ -850,12 +849,12 @@ impl<LTF: LayoutThreadFactory, STF: ScriptThreadFactory> Constellation<LTF, STF>
process::exit(1);
}
let window_size = self.pipelines.get(&pipeline_id).and_then(|pipeline| pipeline.size);
let window_size = self.pipelines.get(&failure.pipeline_id).and_then(|pipeline| pipeline.size);
self.close_pipeline(pipeline_id, ExitPipelineMode::Force);
self.close_pipeline(failure.pipeline_id, ExitPipelineMode::Force);
while let Some(pending_pipeline_id) = self.pending_frames.iter().find(|pending| {
pending.old_pipeline_id == Some(pipeline_id)
pending.old_pipeline_id == Some(failure.pipeline_id)
}).map(|frame| frame.new_pipeline_id) {
warn!("removing pending frame change for failed pipeline");
self.close_pipeline(pending_pipeline_id, ExitPipelineMode::Force);
@ -865,12 +864,12 @@ impl<LTF: LayoutThreadFactory, STF: ScriptThreadFactory> Constellation<LTF, STF>
let new_pipeline_id = PipelineId::new();
self.new_pipeline(new_pipeline_id,
parent_info,
failure.parent_info,
window_size,
None,
LoadData::new(Url::parse("about:failure").unwrap()));
self.push_pending_frame(new_pipeline_id, Some(pipeline_id));
self.push_pending_frame(new_pipeline_id, Some(failure.pipeline_id));
}

View file

@ -140,10 +140,7 @@ impl Pipeline {
.expect("Pipeline script to compositor chan");
let mut pipeline_port = Some(pipeline_port);
let failure = Failure {
pipeline_id: state.id,
parent_info: state.parent_info,
};
let failure = Failure::new(state.id, state.parent_info);
let window_size = state.window_size.map(|size| {
WindowSizeData {
@ -182,7 +179,7 @@ impl Pipeline {
subpage_id: subpage_id,
load_data: state.load_data.clone(),
paint_chan: layout_to_paint_chan.clone().to_opaque(),
failure: failure,
failure: failure.clone(),
pipeline_port: mem::replace(&mut pipeline_port, None)
.expect("script_pipeline != None but pipeline_port == None"),
layout_shutdown_chan: layout_shutdown_chan.clone(),
@ -230,7 +227,7 @@ impl Pipeline {
layout_to_constellation_chan: state.layout_to_constellation_chan,
script_chan: script_chan,
load_data: state.load_data.clone(),
failure: failure,
failure: failure.clone(),
script_port: script_port,
opts: (*opts::get()).clone(),
prefs: prefs::get_cloned(),

View file

@ -441,7 +441,7 @@ impl<C> PaintThread<C> where C: PaintListener + Send + 'static {
debug!("paint_thread: shutdown_chan send");
shutdown_chan.send(()).unwrap();
}, ConstellationMsg::Failure(failure_msg), c);
}, failure_msg, c);
}
#[allow(unsafe_code)]

View file

@ -34,6 +34,12 @@ pub enum PaintMsg {
Failure(Failure),
}
impl From<Failure> for PaintMsg {
fn from(failure: Failure) -> PaintMsg {
PaintMsg::Failure(failure)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum LayerKind {
NoTransform,

View file

@ -286,7 +286,7 @@ impl LayoutThreadFactory for LayoutThread {
}
let _ = shutdown_chan.send(());
let _ = content_process_shutdown_chan.send(());
}, ConstellationMsg::Failure(failure_msg), con_chan);
}, failure_msg, con_chan);
}
}

View file

@ -16,6 +16,7 @@ use std::cell::Cell;
use std::fmt;
use url::Url;
use util::geometry::{PagePx, ViewportPx};
use util::thread::AddFailureDetails;
use webdriver_msg::{LoadStatus, WebDriverScriptCommand};
use webrender_traits;
@ -36,10 +37,27 @@ impl<T: Serialize + Deserialize> Clone for ConstellationChan<T> {
}
// We pass this info to various threads, so it lives in a separate, cloneable struct.
#[derive(Clone, Copy, Deserialize, Serialize)]
#[derive(Clone, Deserialize, Serialize, Debug)]
pub struct Failure {
pub pipeline_id: PipelineId,
pub parent_info: Option<(PipelineId, SubpageId)>,
pub panic_message: Option<String>,
}
impl Failure {
pub fn new(pipeline_id: PipelineId, parent_info: Option<(PipelineId, SubpageId)>) -> Failure {
Failure {
pipeline_id: pipeline_id,
parent_info: parent_info,
panic_message: None,
}
}
}
impl AddFailureDetails for Failure {
fn add_panic_message(&mut self, message: String) {
self.panic_message = Some(message);
}
}
#[derive(Copy, Clone, Deserialize, Serialize, HeapSizeOf)]

View file

@ -445,7 +445,7 @@ impl ScriptThreadFactory for ScriptThread {
let ConstellationChan(const_chan) = state.constellation_chan.clone();
let (script_chan, script_port) = channel();
let layout_chan = LayoutChan(layout_chan.sender());
let failure_info = state.failure_info;
let failure_info = state.failure_info.clone();
thread::spawn_named_with_send_on_failure(format!("ScriptThread {:?}", state.id),
thread_state::SCRIPT,
move || {
@ -481,7 +481,7 @@ impl ScriptThreadFactory for ScriptThread {
// This must always be the very last operation performed before the thread completes
failsafe.neuter();
}, ConstellationMsg::Failure(failure_info), const_chan);
}, failure_info, const_chan);
}
}

View file

@ -32,6 +32,12 @@ pub enum LayoutMsg {
ViewportConstrained(PipelineId, ViewportConstraints),
}
impl From<Failure> for LayoutMsg {
fn from(failure: Failure) -> LayoutMsg {
LayoutMsg::Failure(failure)
}
}
/// Messages from the script to the constellation.
#[derive(Deserialize, Serialize)]
pub enum ScriptMsg {
@ -86,3 +92,9 @@ pub enum ScriptMsg {
/// Update the pipeline Url, which can change after redirections.
SetFinalUrl(PipelineId, Url),
}
impl From<Failure> for ScriptMsg {
fn from(failure: Failure) -> ScriptMsg {
ScriptMsg::Failure(failure)
}
}

View file

@ -5,6 +5,7 @@
use ipc_channel::ipc::IpcSender;
use opts;
use serde::Serialize;
use std::any::Any;
use std::borrow::ToOwned;
use std::io::{Write, stderr};
use std::panic::{PanicInfo, take_hook, set_hook};
@ -13,6 +14,8 @@ use std::thread;
use std::thread::Builder;
use thread_state;
pub type PanicReason = Option<String>;
pub fn spawn_named<F>(name: String, f: F)
where F: FnOnce() + Send + 'static
{
@ -52,6 +55,17 @@ pub fn spawn_named<F>(name: String, f: F)
builder.spawn(f_with_hook).unwrap();
}
pub trait AddFailureDetails {
fn add_panic_message(&mut self, message: String);
fn add_panic_object(&mut self, object: Box<Any>) {
if let Some(message) = object.downcast_ref::<String>() {
self.add_panic_message(message.to_owned());
} else if let Some(&message) = object.downcast_ref::<&'static str>() {
self.add_panic_message(message.to_owned());
}
}
}
/// An abstraction over `Sender<T>` and `IpcSender<T>`, for use in
/// `spawn_named_with_send_on_failure`.
pub trait SendOnFailure {
@ -62,14 +76,16 @@ pub trait SendOnFailure {
impl<T> SendOnFailure for Sender<T> where T: Send + 'static {
type Value = T;
fn send_on_failure(&mut self, value: T) {
self.send(value).unwrap();
// Discard any errors to avoid double-panic
let _ = self.send(value);
}
}
impl<T> SendOnFailure for IpcSender<T> where T: Send + Serialize + 'static {
type Value = T;
fn send_on_failure(&mut self, value: T) {
self.send(value).unwrap();
// Discard any errors to avoid double-panic
let _ = self.send(value);
}
}
@ -77,11 +93,13 @@ impl<T> SendOnFailure for IpcSender<T> where T: Send + Serialize + 'static {
pub fn spawn_named_with_send_on_failure<F, T, S>(name: String,
state: thread_state::ThreadState,
f: F,
msg: T,
mut msg: T,
mut dest: S)
where F: FnOnce() + Send + 'static,
T: Send + 'static,
S: Send + SendOnFailure<Value=T> + 'static {
where F: FnOnce() + Send + 'static,
T: Send + AddFailureDetails + 'static,
S: Send + SendOnFailure + 'static,
S::Value: From<T>,
{
let future_handle = thread::Builder::new().name(name.to_owned()).spawn(move || {
thread_state::initialize(state);
f()
@ -91,9 +109,10 @@ pub fn spawn_named_with_send_on_failure<F, T, S>(name: String,
Builder::new().name(watcher_name).spawn(move || {
match future_handle.join() {
Ok(()) => (),
Err(..) => {
Err(err) => {
debug!("{} failed, notifying constellation", name);
dest.send_on_failure(msg);
msg.add_panic_object(err);
dest.send_on_failure(S::Value::from(msg));
}
}
}).unwrap();