Skip to content

Commit

Permalink
Report panics in web worker threads.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ms2ger committed May 18, 2016
1 parent aa8c835 commit 71dc1f3
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 11 deletions.
10 changes: 9 additions & 1 deletion components/script/dom/bindings/global.rs
Expand Up @@ -18,7 +18,7 @@ use ipc_channel::ipc::IpcSender;
use js::jsapi::{CurrentGlobalOrNull, GetGlobalForObjectCrossCompartment};
use js::jsapi::{JSContext, JSObject, JS_GetClass, MutableHandleValue};
use js::{JSCLASS_IS_DOMJSCLASS, JSCLASS_IS_GLOBAL};
use msg::constellation_msg::{ConstellationChan, PipelineId};
use msg::constellation_msg::{ConstellationChan, PanicMsg, PipelineId};
use net_traits::ResourceThread;
use profile_traits::mem;
use script_runtime::{CommonScriptMsg, ScriptChan, ScriptPort};
Expand Down Expand Up @@ -263,6 +263,14 @@ impl<'a> GlobalRef<'a> {
GlobalRef::Worker(ref worker) => worker.reflector(),
}
}

/// Returns an `IpcSender` to report panics on.
pub fn panic_chan(&self) -> &IpcSender<PanicMsg> {
match *self {
GlobalRef::Window(ref window) => window.panic_chan(),
GlobalRef::Worker(ref worker) => worker.panic_chan(),
}
}
}

impl GlobalRoot {
Expand Down
11 changes: 5 additions & 6 deletions components/script/dom/dedicatedworkerglobalscope.rs
Expand Up @@ -36,8 +36,7 @@ use std::sync::mpsc::{Receiver, RecvError, Select, Sender, channel};
use std::sync::{Arc, Mutex};
use url::Url;
use util::str::DOMString;
use util::thread::spawn_named;
use util::thread_state;
use util::thread::spawn_named_with_send_on_panic;
use util::thread_state::{IN_WORKER, SCRIPT};

/// Messages used to control the worker event loops
Expand Down Expand Up @@ -218,9 +217,9 @@ impl DedicatedWorkerGlobalScope {
own_sender: Sender<(TrustedWorkerAddress, WorkerScriptMsg)>,
receiver: Receiver<(TrustedWorkerAddress, WorkerScriptMsg)>) {
let serialized_worker_url = worker_url.to_string();
spawn_named(format!("WebWorker for {}", serialized_worker_url), move || {
thread_state::initialize(SCRIPT | IN_WORKER);

let name = format!("WebWorker for {}", serialized_worker_url);
let panic_chan = init.panic_chan.clone();
spawn_named_with_send_on_panic(name, SCRIPT | IN_WORKER, move || {
let roots = RootCollection::new();
let _stack_roots_tls = StackRootTLS::new(&roots);

Expand Down Expand Up @@ -284,7 +283,7 @@ impl DedicatedWorkerGlobalScope {
global.handle_event(event);
}
}, reporter_name, parent_sender, CommonScriptMsg::CollectReports);
});
}, Some(id.clone()), panic_chan);
}

pub fn script_chan(&self) -> Box<ScriptChan + Send> {
Expand Down
13 changes: 11 additions & 2 deletions components/script/dom/window.rs
Expand Up @@ -42,7 +42,7 @@ use js::rust::Runtime;
use layout_interface::{ContentBoxResponse, ContentBoxesResponse, ResolvedStyleResponse, ScriptReflow};
use layout_interface::{LayoutChan, LayoutRPC, Msg, Reflow, ReflowQueryType, MarginStyleResponse};
use libc;
use msg::constellation_msg::{ConstellationChan, LoadData, PipelineId, SubpageId};
use msg::constellation_msg::{ConstellationChan, LoadData, PanicMsg, PipelineId, SubpageId};
use msg::constellation_msg::{WindowSizeData, WindowSizeType};
use msg::webdriver_msg::{WebDriverJSError, WebDriverJSResult};
use net_traits::ResourceThread;
Expand Down Expand Up @@ -253,6 +253,9 @@ pub struct Window {
ignore_further_async_events: Arc<AtomicBool>,

error_reporter: CSSErrorReporter,

#[ignore_heap_size_of = "Defined in ipc-channel"]
panic_chan: IpcSender<PanicMsg>,
}

impl Window {
Expand Down Expand Up @@ -1275,6 +1278,10 @@ impl Window {
&self.scheduler_chan
}

pub fn panic_chan(&self) -> &IpcSender<PanicMsg> {
&self.panic_chan
}

pub fn schedule_callback(&self, callback: OneshotTimerCallback, duration: MsDuration) -> OneshotTimerHandle {
self.timers.schedule_callback(callback,
duration,
Expand Down Expand Up @@ -1428,6 +1435,7 @@ impl Window {
constellation_chan: ConstellationChan<ConstellationMsg>,
control_chan: IpcSender<ConstellationControlMsg>,
scheduler_chan: IpcSender<TimerEventRequest>,
panic_chan: IpcSender<PanicMsg>,
timer_event_chan: IpcSender<TimerEvent>,
layout_chan: LayoutChan,
id: PipelineId,
Expand Down Expand Up @@ -1496,7 +1504,8 @@ impl Window {
devtools_wants_updates: Cell::new(false),
webdriver_script_chan: DOMRefCell::new(None),
ignore_further_async_events: Arc::new(AtomicBool::new(false)),
error_reporter: error_reporter
error_reporter: error_reporter,
panic_chan: panic_chan,
};

WindowBinding::Wrap(runtime.cx(), win)
Expand Down
1 change: 1 addition & 0 deletions components/script/dom/worker.rs
Expand Up @@ -106,6 +106,7 @@ impl Worker {
from_devtools_sender: optional_sender,
constellation_chan: constellation_chan,
scheduler_chan: scheduler_chan,
panic_chan: global.panic_chan().clone(),
worker_id: worker_id,
closing: closing,
};
Expand Down
11 changes: 10 additions & 1 deletion components/script/dom/workerglobalscope.rs
Expand Up @@ -21,7 +21,7 @@ use ipc_channel::ipc::IpcSender;
use js::jsapi::{HandleValue, JSContext, JSRuntime, RootedValue};
use js::jsval::UndefinedValue;
use js::rust::Runtime;
use msg::constellation_msg::{ConstellationChan, PipelineId};
use msg::constellation_msg::{ConstellationChan, PanicMsg, PipelineId};
use net_traits::{LoadContext, ResourceThread, load_whole_resource};
use profile_traits::mem;
use script_runtime::{CommonScriptMsg, ScriptChan, ScriptPort};
Expand Down Expand Up @@ -49,6 +49,7 @@ pub struct WorkerGlobalScopeInit {
pub from_devtools_sender: Option<IpcSender<DevtoolScriptControlMsg>>,
pub constellation_chan: ConstellationChan<ConstellationMsg>,
pub scheduler_chan: IpcSender<TimerEventRequest>,
pub panic_chan: IpcSender<PanicMsg>,
pub worker_id: WorkerId,
pub closing: Arc<AtomicBool>,
}
Expand Down Expand Up @@ -94,6 +95,9 @@ pub struct WorkerGlobalScope {

#[ignore_heap_size_of = "Defined in std"]
scheduler_chan: IpcSender<TimerEventRequest>,

#[ignore_heap_size_of = "Defined in ipc-channel"]
panic_chan: IpcSender<PanicMsg>,
}

impl WorkerGlobalScope {
Expand Down Expand Up @@ -124,6 +128,7 @@ impl WorkerGlobalScope {
devtools_wants_updates: Cell::new(false),
constellation_chan: init.constellation_chan,
scheduler_chan: init.scheduler_chan,
panic_chan: init.panic_chan,
}
}

Expand Down Expand Up @@ -191,6 +196,10 @@ impl WorkerGlobalScope {
self.next_worker_id.set(WorkerId(id_num + 1));
worker_id
}

pub fn panic_chan(&self) -> &IpcSender<PanicMsg> {
&self.panic_chan
}
}

impl WorkerGlobalScopeMethods for WorkerGlobalScope {
Expand Down
6 changes: 5 additions & 1 deletion components/script/script_thread.rs
Expand Up @@ -59,7 +59,7 @@ use js::rust::Runtime;
use layout_interface::{ReflowQueryType};
use layout_interface::{self, LayoutChan, NewLayoutThreadInfo, ScriptLayoutChan};
use mem::heap_size_of_self_and_children;
use msg::constellation_msg::{ConstellationChan, LoadData};
use msg::constellation_msg::{ConstellationChan, LoadData, PanicMsg};
use msg::constellation_msg::{PipelineId, PipelineNamespace};
use msg::constellation_msg::{SubpageId, WindowSizeData, WindowSizeType};
use msg::webdriver_msg::WebDriverScriptCommand;
Expand Down Expand Up @@ -383,6 +383,8 @@ pub struct ScriptThread {
timer_event_port: Receiver<TimerEvent>,

content_process_shutdown_chan: IpcSender<()>,

panic_chan: IpcSender<PanicMsg>,
}

/// In the event of thread panic, all data on the stack runs its destructor. However, there
Expand Down Expand Up @@ -575,6 +577,7 @@ impl ScriptThread {
compositor: DOMRefCell::new(state.compositor),
time_profiler_chan: state.time_profiler_chan,
mem_profiler_chan: state.mem_profiler_chan,
panic_chan: state.panic_chan.0,

devtools_chan: state.devtools_chan,
devtools_port: devtools_port,
Expand Down Expand Up @@ -1450,6 +1453,7 @@ impl ScriptThread {
self.constellation_chan.clone(),
self.control_chan.clone(),
self.scheduler_chan.clone(),
self.panic_chan.clone(),
ipc_timer_event_chan,
incomplete.layout_chan,
incomplete.pipeline_id,
Expand Down

0 comments on commit 71dc1f3

Please sign in to comment.