From 9b8e846caedb52bc0969fb66373484408f754b20 Mon Sep 17 00:00:00 2001 From: Bernhard Schuster Date: Fri, 11 Feb 2022 13:53:44 +0100 Subject: [PATCH 1/2] avoid panics --- src/backends/pprof.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/backends/pprof.rs b/src/backends/pprof.rs index bada331a..9170aeda 100644 --- a/src/backends/pprof.rs +++ b/src/backends/pprof.rs @@ -113,17 +113,15 @@ where } for (index, frame) in key.frames.iter().rev().enumerate() { - let last_frame = key.frames.len().saturating_sub(1); for (index, symbol) in frame.iter().rev().enumerate() { - let last_symbol = frame.len().saturating_sub(1); - if index == last_symbol { + if index + 1 == frame.len() { write!(writer, "{}", symbol)?; } else { write!(writer, "{};", symbol)?; } } - if index != last_frame { + if index + 1 != key.frames.len() { write!(writer, ";")?; } } From 3061de1dbd613957a93cc4262beea9fbdbd78a94 Mon Sep 17 00:00:00 2001 From: Bernhard Schuster Date: Fri, 11 Feb 2022 14:40:15 +0100 Subject: [PATCH 2/2] avoid all unwraps --- src/pyroscope.rs | 34 ++++++++++++++++++++++------------ src/session.rs | 6 +++--- src/timer/sleep.rs | 5 +---- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/pyroscope.rs b/src/pyroscope.rs index d95e7f57..3abcf364 100644 --- a/src/pyroscope.rs +++ b/src/pyroscope.rs @@ -344,7 +344,11 @@ impl PyroscopeAgent { fn _stop(&mut self) -> Result<()> { log::debug!("PyroscopeAgent - Stopping"); // get tx and send termination signal - self.tx.take().unwrap().send(0)?; + if let Some(sender) = self.tx.take() { + sender.send(0)?; + } else { + log::error!("PyroscopeAgent - Missing sender") + } // Wait for the Thread to finish let pair = Arc::clone(&self.running); @@ -362,11 +366,11 @@ impl PyroscopeAgent { /// Stop the agent. The agent will stop profiling and send a last report to the server. /// # Example /// ```ignore - /// let agent = PyroscopeAgent::builder("http://localhost:8080", "my-app").build().unwrap(); - /// agent.start(); + /// let agent = PyroscopeAgent::builder("http://localhost:8080", "my-app").build()?; + /// agent.start()?; /// // Expensive operation /// agent.stop(); - /// ``` + /// ``` pub fn stop(&mut self) { match self._stop() { Ok(_) => log::trace!("PyroscopeAgent - Agent stopped"), @@ -377,12 +381,12 @@ impl PyroscopeAgent { /// Add tags. This will restart the agent. /// # Example /// ```ignore - /// let agent = PyroscopeAgent::builder("http://localhost:8080", "my-app").build().unwrap(); - /// agent.start(); + /// let agent = PyroscopeAgent::builder("http://localhost:8080", "my-app").build()?; + /// agent.start()?; /// // Expensive operation - /// agent.add_tags(vec![("tag", "value")]).unwrap(); + /// agent.add_tags(vec!["tag", "value"])?; /// // Tagged operation - /// agent.stop(); + /// agent.stop()?; /// ``` pub fn add_tags(&mut self, tags: &[(&str, &str)]) -> Result<()> { log::debug!("PyroscopeAgent - Adding tags"); @@ -413,14 +417,20 @@ impl PyroscopeAgent { /// Remove tags. This will restart the agent. /// # Example /// ```ignore + /// # use pyroscope::*; + /// # use std::result; + /// # fn main() -> result::Result<(), error::PyroscopeError> { /// let agent = PyroscopeAgent::builder("http://localhost:8080", "my-app") /// .tags(vec![("tag", "value")]) - /// .build().unwrap(); - /// agent.start(); + /// .build()?; + /// agent.start()?; /// // Expensive operation - /// agent.remove_tags(vec!["tag"]).unwrap(); + /// agent.remove_tags(vec!["tag"])?; /// // Un-Tagged operation - /// agent.stop(); + /// agent.stop()?; + /// # Ok(()) + /// # } + /// ``` pub fn remove_tags(&mut self, tags: &[&str]) -> Result<()> { log::debug!("PyroscopeAgent - Removing tags"); diff --git a/src/session.rs b/src/session.rs index b2baeb86..9204c8f5 100644 --- a/src/session.rs +++ b/src/session.rs @@ -94,7 +94,7 @@ impl Session { /// let config = PyroscopeConfig::new("https://localhost:8080", "my-app"); /// let report = vec![1, 2, 3]; /// let until = 154065120; - /// let session = Session::new(until, config, report).unwrap(); + /// let session = Session::new(until, config, report)?; /// ``` pub fn new(until: u64, config: PyroscopeConfig, report: Vec) -> Result { log::info!("Session - Creating Session"); @@ -117,8 +117,8 @@ impl Session { /// let config = PyroscopeConfig::new("https://localhost:8080", "my-app"); /// let report = vec![1, 2, 3]; /// let until = 154065120; - /// let session = Session::new(until, config, report).unwrap(); - /// session.send().unwrap(); + /// let session = Session::new(until, config, report)?; + /// session.send()?; /// ``` pub fn send(self) -> Result<()> { log::info!("Session - Sending Session {} - {}", self.from, self.until); diff --git a/src/timer/sleep.rs b/src/timer/sleep.rs index f1f7e11c..91c01ab8 100644 --- a/src/timer/sleep.rs +++ b/src/timer/sleep.rs @@ -60,10 +60,7 @@ impl Timer { // Iterate through Senders txs.lock()?.iter().for_each(|tx| { // Send event to attached Sender - match tx.send(current) { - Ok(_) => {} - Err(_) => {} - } + let _res = tx.send(current); }); // Sleep for 10s