From b328c57caa4f402f6724b5f03db7f8e775f012cc Mon Sep 17 00:00:00 2001 From: Eduard Burtescu Date: Fri, 5 Dec 2014 02:57:32 +0200 Subject: [PATCH] devtools: handle script task panics gracefully in ConsoleActor. --- components/devtools/actor.rs | 10 ++++++--- components/devtools/actors/console.rs | 8 +++---- components/devtools/actors/inspector.rs | 30 ++++++++++++------------- components/devtools/actors/root.rs | 6 ++--- components/devtools/actors/tab.rs | 6 ++--- components/devtools/lib.rs | 15 ++++++++++--- 6 files changed, 44 insertions(+), 31 deletions(-) diff --git a/components/devtools/actor.rs b/components/devtools/actor.rs index 0643b567f52..54b59bc8d79 100644 --- a/components/devtools/actor.rs +++ b/components/devtools/actor.rs @@ -21,7 +21,7 @@ pub trait Actor : Any { registry: &ActorRegistry, msg_type: &String, msg: &json::JsonObject, - stream: &mut TcpStream) -> bool; + stream: &mut TcpStream) -> Result; fn name(&self) -> String; } @@ -148,13 +148,16 @@ impl ActorRegistry { /// Attempt to process a message as directed by its `to` property. If the actor is not /// found or does not indicate that it knew how to process the message, ignore the failure. - pub fn handle_message(&mut self, msg: &json::JsonObject, stream: &mut TcpStream) { + pub fn handle_message(&mut self, + msg: &json::JsonObject, + stream: &mut TcpStream) + -> Result<(), ()> { let to = msg.get(&"to".to_string()).unwrap().as_string().unwrap(); match self.actors.get(&to.to_string()) { None => println!("message received for unknown actor \"{:s}\"", to), Some(actor) => { let msg_type = msg.get(&"type".to_string()).unwrap().as_string().unwrap(); - if !actor.handle_message(self, &msg_type.to_string(), msg, stream) { + if !try!(actor.handle_message(self, &msg_type.to_string(), msg, stream)) { println!("unexpected message type \"{:s}\" found for actor \"{:s}\"", msg_type, to); } @@ -164,5 +167,6 @@ impl ActorRegistry { for actor in new_actors.into_iter() { self.actors.insert(actor.name().to_string(), actor); } + Ok(()) } } diff --git a/components/devtools/actors/console.rs b/components/devtools/actors/console.rs index 1e5ea5080cd..545fbb34e78 100644 --- a/components/devtools/actors/console.rs +++ b/components/devtools/actors/console.rs @@ -118,8 +118,8 @@ impl Actor for ConsoleActor { _registry: &ActorRegistry, msg_type: &String, msg: &json::JsonObject, - stream: &mut TcpStream) -> bool { - match msg_type.as_slice() { + stream: &mut TcpStream) -> Result { + Ok(match msg_type.as_slice() { "getCachedMessages" => { let types = msg.get(&"messageTypes".to_string()).unwrap().as_list().unwrap(); let /*mut*/ messages = vec!(); @@ -223,7 +223,7 @@ impl Actor for ConsoleActor { self.script_chan.send(EvaluateJS(self.pipeline, input.clone(), chan)); //TODO: extract conversion into protocol module or some other useful place - let result = match port.recv() { + let result = match try!(port.recv_opt()) { VoidValue => { let mut m = TreeMap::new(); m.insert("type".to_string(), "undefined".to_string().to_json()); @@ -285,6 +285,6 @@ impl Actor for ConsoleActor { } _ => false - } + }) } } diff --git a/components/devtools/actors/inspector.rs b/components/devtools/actors/inspector.rs index 01c6498ef1e..471e379e693 100644 --- a/components/devtools/actors/inspector.rs +++ b/components/devtools/actors/inspector.rs @@ -66,8 +66,8 @@ impl Actor for HighlighterActor { _registry: &ActorRegistry, msg_type: &String, _msg: &json::JsonObject, - stream: &mut TcpStream) -> bool { - match msg_type.as_slice() { + stream: &mut TcpStream) -> Result { + Ok(match msg_type.as_slice() { "showBoxModel" => { let msg = ShowBoxModelReply { from: self.name(), @@ -85,7 +85,7 @@ impl Actor for HighlighterActor { } _ => false, - } + }) } } @@ -103,8 +103,8 @@ impl Actor for NodeActor { registry: &ActorRegistry, msg_type: &String, msg: &json::JsonObject, - stream: &mut TcpStream) -> bool { - match msg_type.as_slice() { + stream: &mut TcpStream) -> Result { + Ok(match msg_type.as_slice() { "modifyAttributes" => { let target = msg.get(&"to".to_string()).unwrap().as_string().unwrap(); let mods = msg.get(&"modifications".to_string()).unwrap().as_list().unwrap(); @@ -123,7 +123,7 @@ impl Actor for NodeActor { } _ => false, - } + }) } } @@ -276,8 +276,8 @@ impl Actor for WalkerActor { registry: &ActorRegistry, msg_type: &String, msg: &json::JsonObject, - stream: &mut TcpStream) -> bool { - match msg_type.as_slice() { + stream: &mut TcpStream) -> Result { + Ok(match msg_type.as_slice() { "querySelector" => { let msg = QuerySelectorReply { from: self.name(), @@ -329,7 +329,7 @@ impl Actor for WalkerActor { } _ => false, - } + }) } } @@ -421,8 +421,8 @@ impl Actor for PageStyleActor { registry: &ActorRegistry, msg_type: &String, msg: &json::JsonObject, - stream: &mut TcpStream) -> bool { - match msg_type.as_slice() { + stream: &mut TcpStream) -> Result { + Ok(match msg_type.as_slice() { "getApplied" => { //TODO: query script for relevant applied styles to node (msg.node) let msg = GetAppliedReply { @@ -479,7 +479,7 @@ impl Actor for PageStyleActor { } _ => false, - } + }) } } @@ -492,8 +492,8 @@ impl Actor for InspectorActor { registry: &ActorRegistry, msg_type: &String, _msg: &json::JsonObject, - stream: &mut TcpStream) -> bool { - match msg_type.as_slice() { + stream: &mut TcpStream) -> Result { + Ok(match msg_type.as_slice() { "getWalker" => { if self.walker.borrow().is_none() { let walker = WalkerActor { @@ -569,6 +569,6 @@ impl Actor for InspectorActor { } _ => false, - } + }) } } diff --git a/components/devtools/actors/root.rs b/components/devtools/actors/root.rs index ac5c355da71..50d0e314bd2 100644 --- a/components/devtools/actors/root.rs +++ b/components/devtools/actors/root.rs @@ -54,8 +54,8 @@ impl Actor for RootActor { registry: &ActorRegistry, msg_type: &String, _msg: &json::JsonObject, - stream: &mut TcpStream) -> bool { - match msg_type.as_slice() { + stream: &mut TcpStream) -> Result { + Ok(match msg_type.as_slice() { "listAddons" => { let actor = ErrorReply { from: "root".to_string(), @@ -80,7 +80,7 @@ impl Actor for RootActor { } _ => false - } + }) } } diff --git a/components/devtools/actors/tab.rs b/components/devtools/actors/tab.rs index 48e773b0d83..f185f4ab5a9 100644 --- a/components/devtools/actors/tab.rs +++ b/components/devtools/actors/tab.rs @@ -78,8 +78,8 @@ impl Actor for TabActor { registry: &ActorRegistry, msg_type: &String, _msg: &json::JsonObject, - stream: &mut TcpStream) -> bool { - match msg_type.as_slice() { + stream: &mut TcpStream) -> Result { + Ok(match msg_type.as_slice() { "reconfigure" => { stream.write_json_packet(&ReconfigureReply { from: self.name() }); true @@ -125,7 +125,7 @@ impl Actor for TabActor { } _ => false - } + }) } } diff --git a/components/devtools/lib.rs b/components/devtools/lib.rs index 8233cd1da51..307c2336f48 100644 --- a/components/devtools/lib.rs +++ b/components/devtools/lib.rs @@ -101,9 +101,18 @@ fn run_server(receiver: Receiver, port: u16) { 'outer: loop { match stream.read_json_packet() { - Ok(json_packet) => - actors.lock().handle_message(json_packet.as_object().unwrap(), - &mut stream), + Ok(json_packet) => { + match actors.lock().handle_message(json_packet.as_object().unwrap(), + &mut stream) { + Ok(()) => {}, + Err(()) => { + println!("error: devtools actor stopped responding"); + stream.close_read(); + stream.close_write(); + break 'outer + } + } + } Err(e) => { println!("error: {}", e.desc); break 'outer