From d5f9cccf5e4c41f25bdaa1cb9bcdba1f8d28995d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Wed, 6 Apr 2016 10:58:51 +0200 Subject: [PATCH 1/8] Removing match on constant --- ethcore/src/account.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethcore/src/account.rs b/ethcore/src/account.rs index 6901996bc..87f2a05be 100644 --- a/ethcore/src/account.rs +++ b/ethcore/src/account.rs @@ -138,7 +138,7 @@ impl Account { /// get someone who knows to call `note_code`. pub fn code(&self) -> Option<&[u8]> { match self.code_hash { - Some(SHA3_EMPTY) | None if self.code_cache.is_empty() => Some(&self.code_cache), + Some(c) if c == SHA3_EMPTY && self.code_cache.is_empty() => Some(&self.code_cache), Some(_) if !self.code_cache.is_empty() => Some(&self.code_cache), None => Some(&self.code_cache), _ => None, From dc91e57c2ffb0b57d5f2c7e2bea6362485604607 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Wed, 6 Apr 2016 19:07:27 +0200 Subject: [PATCH 2/8] Additional logging and error messages --- Cargo.lock | 6 ++-- parity/main.rs | 81 ++++++++++++++++++++++++++++++++++---------------- rpc/Cargo.toml | 2 +- rpc/src/lib.rs | 23 +++++++++----- 4 files changed, 76 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b24bc9219..5c54ae66d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -245,7 +245,7 @@ dependencies = [ "ethminer 1.1.0", "ethsync 1.1.0", "jsonrpc-core 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", - "jsonrpc-http-server 3.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "jsonrpc-http-server 4.0.0 (git+https://github.com/tomusdrw/jsonrpc-http-server.git)", "log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -452,8 +452,8 @@ dependencies = [ [[package]] name = "jsonrpc-http-server" -version = "3.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" +version = "4.0.0" +source = "git+https://github.com/tomusdrw/jsonrpc-http-server.git#46bd4e7cf8352e0efc940cf76d3dff99f1a3da15" dependencies = [ "hyper 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "jsonrpc-core 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/parity/main.rs b/parity/main.rs index c5e0dce54..465a21fee 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -269,10 +269,8 @@ fn setup_rpc_server( sync: Arc, secret_store: Arc, miner: Arc, - url: &str, - cors_domain: &str, apis: Vec<&str> -) -> Option> { +) -> Option { use rpc::v1::*; let server = rpc::RpcServer::new(); @@ -290,7 +288,7 @@ fn setup_rpc_server( } } } - Some(server.start_http(url, cors_domain, ::num_cpus::get())) + Some(server) } #[cfg(not(feature = "rpc"))] @@ -299,8 +297,6 @@ fn setup_rpc_server( _sync: Arc, _secret_store: Arc, _miner: Arc, - _url: &str, - _cors_domain: &str, _apis: Vec<&str> ) -> Option> { None @@ -569,7 +565,10 @@ impl Configuration { let account_service = Arc::new(self.account_service()); // Build client - let mut service = ClientService::start(self.client_config(), spec, net_settings, &Path::new(&self.path())).unwrap(); + let mut service = ClientService::start( + self.client_config(), spec, net_settings, &Path::new(&self.path()) + ).unwrap_or_else(|e| die_with_error(e)); + panic_handler.forward_from(&service); let client = service.client(); @@ -584,7 +583,23 @@ impl Configuration { let sync = EthSync::register(service.network(), sync_config, client.clone(), miner.clone()); // Setup rpc - if self.args.flag_jsonrpc || self.args.flag_rpc { + let rpc_server = if self.args.flag_jsonrpc || self.args.flag_rpc { + // TODO: use this as the API list. + let apis = self.args.flag_rpcapi.as_ref().unwrap_or(&self.args.flag_jsonrpc_apis); + setup_rpc_server( + service.client(), + sync.clone(), + account_service.clone(), + miner.clone(), + apis.split(',').collect() + ) + } else { + None + }; + let rpc_handle = rpc_server.map(|server| { + panic_handler.forward_from(&server); + server + }).map(|server| { let url = format!("{}:{}", match self.args.flag_rpcaddr.as_ref().unwrap_or(&self.args.flag_jsonrpc_interface).as_str() { "all" => "0.0.0.0", @@ -594,22 +609,14 @@ impl Configuration { self.args.flag_rpcport.unwrap_or(self.args.flag_jsonrpc_port) ); SocketAddr::from_str(&url).unwrap_or_else(|_| die!("{}: Invalid JSONRPC listen host/port given.", url)); - let cors = self.args.flag_rpccorsdomain.as_ref().unwrap_or(&self.args.flag_jsonrpc_cors); - // TODO: use this as the API list. - let apis = self.args.flag_rpcapi.as_ref().unwrap_or(&self.args.flag_jsonrpc_apis); - let server_handler = setup_rpc_server( - service.client(), - sync.clone(), - account_service.clone(), - miner.clone(), - &url, - cors, - apis.split(',').collect() - ); - if let Some(handler) = server_handler { - panic_handler.forward_from(handler.deref()); + let cors_domain = self.args.flag_rpccorsdomain.as_ref().unwrap_or(&self.args.flag_jsonrpc_cors); + let start_result = server.start_http(&url, cors_domain, ::num_cpus::get()); + match start_result { + Ok(handle) => handle, + Err(rpc::RpcServerError::IoError(err)) => die_with_io_error(err), + Err(e) => die!("{:?}", e), } - } + }); // Register IO handler let io_handler = Arc::new(ClientIoHandler { @@ -621,11 +628,11 @@ impl Configuration { service.io().register_handler(io_handler).expect("Error registering IO handler"); // Handle exit - wait_for_exit(panic_handler); + wait_for_exit(panic_handler, rpc_handle); } } -fn wait_for_exit(panic_handler: Arc) { +fn wait_for_exit(panic_handler: Arc, _rpc_handle: Option) { let exit = Arc::new(Condvar::new()); // Handle possible exits @@ -639,6 +646,30 @@ fn wait_for_exit(panic_handler: Arc) { // Wait for signal let mutex = Mutex::new(()); let _ = exit.wait(mutex.lock().unwrap()).unwrap(); + info!("Closing...."); +} + +fn die_with_error(e: ethcore::error::Error) -> ! { + use ethcore::error::Error; + + match e { + Error::Util(UtilError::StdIo(e)) => die_with_io_error(e), + _ => die!("{:?}", e), + } +} +fn die_with_io_error(e: std::io::Error) -> ! { + match e.kind() { + std::io::ErrorKind::PermissionDenied => { + die!("We don't have permission to bind to this port.") + }, + std::io::ErrorKind::AddrInUse => { + die!("Specified address is already in use. Please make sure that nothing is listening on specified port or use different one") + }, + std::io::ErrorKind::AddrNotAvailable => { + die!("Couldn't use specified interface or given address is invalid.") + }, + _ => die!("{:?}", e), + } } fn main() { diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index c28f598fd..6fdf9a55e 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -13,7 +13,7 @@ log = "0.3" serde = "0.7.0" serde_json = "0.7.0" jsonrpc-core = "2.0" -jsonrpc-http-server = "3.0" +jsonrpc-http-server = { git = "https://github.com/tomusdrw/jsonrpc-http-server.git" } ethcore-util = { path = "../util" } ethcore = { path = "../ethcore" } ethash = { path = "../ethash" } diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index 3096a45c9..119d12a0f 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -34,14 +34,16 @@ extern crate transient_hashmap; use std::sync::Arc; use std::thread; -use util::panics::PanicHandler; +use util::panics::{MayPanic, PanicHandler, OnPanicListener}; use self::jsonrpc_core::{IoHandler, IoDelegate}; +pub use jsonrpc_http_server::{Listening, RpcServerError}; pub mod v1; /// Http server. pub struct RpcServer { handler: Arc, + panic_handler: Arc } impl RpcServer { @@ -49,6 +51,7 @@ impl RpcServer { pub fn new() -> RpcServer { RpcServer { handler: Arc::new(IoHandler::new()), + panic_handler: PanicHandler::new_in_arc(), } } @@ -58,17 +61,23 @@ impl RpcServer { } /// Start server asynchronously in new thread and returns panic handler. - pub fn start_http(&self, addr: &str, cors_domain: &str, threads: usize) -> Arc { + pub fn start_http(&self, addr: &str, cors_domain: &str, threads: usize) -> Result { let addr = addr.to_owned(); let cors_domain = cors_domain.to_owned(); - let panic_handler = PanicHandler::new_in_arc(); - let ph = panic_handler.clone(); + let ph = self.panic_handler.clone(); let server = jsonrpc_http_server::Server::new(self.handler.clone()); + thread::Builder::new().name("jsonrpc_http".to_string()).spawn(move || { ph.catch_panic(move || { - server.start(addr.as_ref(), jsonrpc_http_server::AccessControlAllowOrigin::Value(cors_domain), threads); + server.start(addr.as_ref(), jsonrpc_http_server::AccessControlAllowOrigin::Value(cors_domain), threads) }).unwrap() - }).expect("Error while creating jsonrpc http thread"); - panic_handler + }).expect("Error while creating jsonrpc http thread").join().unwrap() } } + +impl MayPanic for RpcServer { + fn on_panic(&self, closure: F) where F: OnPanicListener { + self.panic_handler.on_panic(closure); + } +} + From a52043d5b3ab37c29abfdd03505846aa9003b848 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Wed, 6 Apr 2016 19:14:05 +0200 Subject: [PATCH 3/8] Removing additional thread from JSON-RPC --- parity/main.rs | 4 +--- rpc/src/lib.rs | 18 +----------------- 2 files changed, 2 insertions(+), 20 deletions(-) diff --git a/parity/main.rs b/parity/main.rs index 1b1ca27e6..6fffa4b53 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -594,10 +594,8 @@ impl Configuration { } else { None }; + let rpc_handle = rpc_server.map(|server| { - panic_handler.forward_from(&server); - server - }).map(|server| { let url = format!("{}:{}", match self.args.flag_rpcaddr.as_ref().unwrap_or(&self.args.flag_jsonrpc_interface).as_str() { "all" => "0.0.0.0", diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index 119d12a0f..9787db9cd 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -33,8 +33,6 @@ extern crate ethminer; extern crate transient_hashmap; use std::sync::Arc; -use std::thread; -use util::panics::{MayPanic, PanicHandler, OnPanicListener}; use self::jsonrpc_core::{IoHandler, IoDelegate}; pub use jsonrpc_http_server::{Listening, RpcServerError}; @@ -43,7 +41,6 @@ pub mod v1; /// Http server. pub struct RpcServer { handler: Arc, - panic_handler: Arc } impl RpcServer { @@ -51,7 +48,6 @@ impl RpcServer { pub fn new() -> RpcServer { RpcServer { handler: Arc::new(IoHandler::new()), - panic_handler: PanicHandler::new_in_arc(), } } @@ -64,20 +60,8 @@ impl RpcServer { pub fn start_http(&self, addr: &str, cors_domain: &str, threads: usize) -> Result { let addr = addr.to_owned(); let cors_domain = cors_domain.to_owned(); - let ph = self.panic_handler.clone(); let server = jsonrpc_http_server::Server::new(self.handler.clone()); - thread::Builder::new().name("jsonrpc_http".to_string()).spawn(move || { - ph.catch_panic(move || { - server.start(addr.as_ref(), jsonrpc_http_server::AccessControlAllowOrigin::Value(cors_domain), threads) - }).unwrap() - }).expect("Error while creating jsonrpc http thread").join().unwrap() + server.start(addr.as_ref(), jsonrpc_http_server::AccessControlAllowOrigin::Value(cors_domain), threads) } } - -impl MayPanic for RpcServer { - fn on_panic(&self, closure: F) where F: OnPanicListener { - self.panic_handler.on_panic(closure); - } -} - From fd03f58eaebc7fd9b3fd795904c813d45184df32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Wed, 6 Apr 2016 19:22:10 +0200 Subject: [PATCH 4/8] Rewriting messages --- parity/main.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/parity/main.rs b/parity/main.rs index 6fffa4b53..1f06298be 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -656,13 +656,13 @@ fn die_with_error(e: ethcore::error::Error) -> ! { fn die_with_io_error(e: std::io::Error) -> ! { match e.kind() { std::io::ErrorKind::PermissionDenied => { - die!("We don't have permission to bind to this port.") + die!("No permissions to bind to specified port.") }, std::io::ErrorKind::AddrInUse => { - die!("Specified address is already in use. Please make sure that nothing is listening on specified port or use different one") + die!("Specified address is already in use. Please make sure that nothing is listening on the same port or try using a different one.") }, std::io::ErrorKind::AddrNotAvailable => { - die!("Couldn't use specified interface or given address is invalid.") + die!("Could not use specified interface or given address is invalid.") }, _ => die!("{:?}", e), } From d4f0902968a338345024cd75fabd969149c71a76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Wed, 6 Apr 2016 23:45:19 +0200 Subject: [PATCH 5/8] Tracing shutdown and changed order of IoManager shutdown process --- ethcore/src/block_queue.rs | 2 ++ util/src/io/service.rs | 4 +++- util/src/io/worker.rs | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/ethcore/src/block_queue.rs b/ethcore/src/block_queue.rs index 4a52d6a6b..433e8b40b 100644 --- a/ethcore/src/block_queue.rs +++ b/ethcore/src/block_queue.rs @@ -431,12 +431,14 @@ impl MayPanic for BlockQueue { impl Drop for BlockQueue { fn drop(&mut self) { + trace!(target: "shutdown", "[BlockQueue] Closing..."); self.clear(); self.deleting.store(true, AtomicOrdering::Release); self.more_to_verify.notify_all(); for t in self.verifiers.drain(..) { t.join().unwrap(); } + trace!(target: "shutdown", "[BlockQueue] Closed."); } } diff --git a/util/src/io/service.rs b/util/src/io/service.rs index 8a34ee80a..24cc1181a 100644 --- a/util/src/io/service.rs +++ b/util/src/io/service.rs @@ -231,8 +231,8 @@ impl Handler for IoManager where Message: Send + Clone + Sync fn notify(&mut self, event_loop: &mut EventLoop, msg: Self::Message) { match msg { IoMessage::Shutdown => { - self.workers.clear(); event_loop.shutdown(); + self.workers.clear(); }, IoMessage::AddHandler { handler } => { let handler_id = { @@ -376,8 +376,10 @@ impl IoService where Message: Send + Sync + Clone + 'static { impl Drop for IoService where Message: Send + Sync + Clone { fn drop(&mut self) { + trace!(target: "shutdown", "[IoService] Closing..."); self.host_channel.send(IoMessage::Shutdown).unwrap(); self.thread.take().unwrap().join().ok(); + trace!(target: "shutdown", "[IoService] Closed."); } } diff --git a/util/src/io/worker.rs b/util/src/io/worker.rs index b874ea0a4..917b1ad79 100644 --- a/util/src/io/worker.rs +++ b/util/src/io/worker.rs @@ -120,10 +120,12 @@ impl Worker { impl Drop for Worker { fn drop(&mut self) { + trace!(target: "shutdown", "[IoWorker] Closing..."); let _ = self.wait_mutex.lock(); self.deleting.store(true, AtomicOrdering::Release); self.wait.notify_all(); let thread = mem::replace(&mut self.thread, None).unwrap(); thread.join().ok(); + trace!(target: "shutdown", "[IoWorker] Closed"); } } From f27d88f6aba8b662645586530e551f51b2fdc9fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Wed, 6 Apr 2016 23:58:23 +0200 Subject: [PATCH 6/8] More descriptive message when closing --- parity/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parity/main.rs b/parity/main.rs index 1f06298be..06710a3a4 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -642,7 +642,7 @@ fn wait_for_exit(panic_handler: Arc, _rpc_handle: Option ! { From 12d1dcddeb17b7ffb5c48851620543b7017411f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Thu, 7 Apr 2016 10:31:42 +0200 Subject: [PATCH 7/8] Updating rpc comments --- rpc/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index 9787db9cd..4de405211 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -44,7 +44,7 @@ pub struct RpcServer { } impl RpcServer { - /// Construct new http server object with given number of threads. + /// Construct new http server object. pub fn new() -> RpcServer { RpcServer { handler: Arc::new(IoHandler::new()), @@ -56,7 +56,7 @@ impl RpcServer { self.handler.add_delegate(delegate); } - /// Start server asynchronously in new thread and returns panic handler. + /// Start server asynchronously and returns result with `Listening` handle on success or an error. pub fn start_http(&self, addr: &str, cors_domain: &str, threads: usize) -> Result { let addr = addr.to_owned(); let cors_domain = cors_domain.to_owned(); From ccd417f713b793399d8325216642fb0ccc7d2840 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Thu, 7 Apr 2016 12:27:54 +0200 Subject: [PATCH 8/8] Reverting order of shutdown event --- util/src/io/service.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/src/io/service.rs b/util/src/io/service.rs index 24cc1181a..95aa19e47 100644 --- a/util/src/io/service.rs +++ b/util/src/io/service.rs @@ -231,8 +231,8 @@ impl Handler for IoManager where Message: Send + Clone + Sync fn notify(&mut self, event_loop: &mut EventLoop, msg: Self::Message) { match msg { IoMessage::Shutdown => { - event_loop.shutdown(); self.workers.clear(); + event_loop.shutdown(); }, IoMessage::AddHandler { handler } => { let handler_id = {