diff --git a/Cargo.toml b/Cargo.toml index bd2fd46..a538ec5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,6 @@ tracing = { version = "0.1.41", features = ["log"] } tracing-subscriber = { version = "0.3.19", features = ["env-filter"] } [workspace.package] -version = "0.1.5" +version = "0.1.6" license = "MIT" edition = "2021" diff --git a/concurrency/src/tasks/gen_server.rs b/concurrency/src/tasks/gen_server.rs index 7eb6795..f04bb79 100644 --- a/concurrency/src/tasks/gen_server.rs +++ b/concurrency/src/tasks/gen_server.rs @@ -152,16 +152,21 @@ where state: Self::State, ) -> impl Future> + Send { async { - match self.init(handle, state).await { - Ok(new_state) => { - self.main_loop(handle, rx, new_state).await?; - Ok(()) - } - Err(err) => { - tracing::error!("Initialization failed: {err:?}"); - Err(GenServerError::Initialization) - } + let init_result = self + .init(handle, state.clone()) + .await + .inspect_err(|err| tracing::error!("Initialization failed: {err:?}")); + + let res = match init_result { + Ok(new_state) => self.main_loop(handle, rx, new_state).await, + Err(_) => Err(GenServerError::Initialization), + }; + + handle.cancellation_token().cancel(); + if let Err(err) = self.teardown(handle, state).await { + tracing::error!("Error during teardown: {err:?}"); } + res } } @@ -191,10 +196,6 @@ where } } tracing::trace!("Stopping GenServer"); - handle.cancellation_token().cancel(); - if let Err(err) = self.teardown(handle, state).await { - tracing::error!("Error during teardown: {err:?}"); - } Ok(()) } }