From 6223b38118a63e004cfd6ccfdfeb1867c2d971fc Mon Sep 17 00:00:00 2001 From: "M.Amin Rayej" Date: Sun, 17 Mar 2024 22:42:23 +0330 Subject: [PATCH 1/4] prevent using unhandled_panic option on multi-thread runtime --- tokio/src/runtime/builder.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index e20a3c4955b..47a1c055211 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -824,6 +824,9 @@ impl Builder { /// will immediately terminate and further calls to /// [`Runtime::block_on`] will panic. /// + /// # Panics + /// This method panics if called on a runtime other than [`Kind::CurrentThread`]. + /// /// # Unstable /// /// This option is currently unstable and its implementation is @@ -861,6 +864,10 @@ impl Builder { /// /// [`JoinHandle`]: struct@crate::task::JoinHandle pub fn unhandled_panic(&mut self, behavior: UnhandledPanic) -> &mut Self { + if !matches!(self.kind, Kind::CurrentThread) { + panic!("unhandled panic option is only supported in current thread runtime"); + } + self.unhandled_panic = behavior; self } From 0c373951bd2d28689e6ab54520f1e4f37e41ee27 Mon Sep 17 00:00:00 2001 From: "M.Amin Rayej" Date: Sun, 17 Mar 2024 22:48:50 +0330 Subject: [PATCH 2/4] fix docs --- tokio/src/runtime/builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index 47a1c055211..93ea1cadf9e 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -825,7 +825,7 @@ impl Builder { /// [`Runtime::block_on`] will panic. /// /// # Panics - /// This method panics if called on a runtime other than [`Kind::CurrentThread`]. + /// This method panics if called on a runtime other than the current thread runtime. /// /// # Unstable /// From ee9426114e1955a8107d6637152f372fb895ef12 Mon Sep 17 00:00:00 2001 From: "M.Amin Rayej" Date: Mon, 18 Mar 2024 19:33:04 +0330 Subject: [PATCH 3/4] only panic when ShutdownRuntime is passed --- tokio/src/runtime/builder.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index 93ea1cadf9e..26c4109f29c 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -825,7 +825,8 @@ impl Builder { /// [`Runtime::block_on`] will panic. /// /// # Panics - /// This method panics if called on a runtime other than the current thread runtime. + /// This method panics if called with [`UnhandledPanic::ShutdownRuntime`] + /// on a runtime other than the current thread runtime. /// /// # Unstable /// @@ -864,8 +865,8 @@ impl Builder { /// /// [`JoinHandle`]: struct@crate::task::JoinHandle pub fn unhandled_panic(&mut self, behavior: UnhandledPanic) -> &mut Self { - if !matches!(self.kind, Kind::CurrentThread) { - panic!("unhandled panic option is only supported in current thread runtime"); + if !matches!(self.kind, Kind::CurrentThread) && matches!(behavior, UnhandledPanic::ShutdownRuntime) { + panic!("UnhandledPanic::ShutdownRuntime is only supported in current thread runtime"); } self.unhandled_panic = behavior; From 60f7fd95b7a76b0c6679b871dfa34123138aed50 Mon Sep 17 00:00:00 2001 From: "M.Amin Rayej" Date: Mon, 18 Mar 2024 19:34:31 +0330 Subject: [PATCH 4/4] fix docs --- tokio/src/runtime/builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index 26c4109f29c..82d3596915e 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -825,7 +825,7 @@ impl Builder { /// [`Runtime::block_on`] will panic. /// /// # Panics - /// This method panics if called with [`UnhandledPanic::ShutdownRuntime`] + /// This method panics if called with [`UnhandledPanic::ShutdownRuntime`] /// on a runtime other than the current thread runtime. /// /// # Unstable