Skip to content

Commit df73fa2

Browse files
runtime: panic when event_interval is set to 0 (#7838)
1 parent 09ad536 commit df73fa2

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

tokio/src/runtime/builder.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,10 @@ impl Builder {
11231123
/// will minimize that overhead while still keeping the scheduler responsive to
11241124
/// events.
11251125
///
1126+
/// # Panics
1127+
///
1128+
/// This function will panic if 0 is passed as an argument.
1129+
///
11261130
/// # Examples
11271131
///
11281132
/// ```
@@ -1136,7 +1140,9 @@ impl Builder {
11361140
/// # }
11371141
/// # }
11381142
/// ```
1143+
#[track_caller]
11391144
pub fn event_interval(&mut self, val: u32) -> &mut Self {
1145+
assert!(val > 0, "event_interval must be greater than 0");
11401146
self.event_interval = val;
11411147
self
11421148
}

tokio/tests/rt_panic.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ fn builder_global_queue_interval_panic_caller() -> Result<(), Box<dyn Error>> {
8282
Ok(())
8383
}
8484

85+
#[test]
86+
fn builder_event_interval_interval_panic_caller() -> Result<(), Box<dyn Error>> {
87+
let panic_location_file = test_panic(|| {
88+
let _ = Builder::new_multi_thread().event_interval(0).build();
89+
});
90+
91+
// The panic location should be in this file
92+
assert_eq!(&panic_location_file.unwrap(), file!());
93+
94+
Ok(())
95+
}
96+
8597
fn current_thread() -> Runtime {
8698
tokio::runtime::Builder::new_current_thread()
8799
.enable_all()

0 commit comments

Comments
 (0)