Skip to content

Commit 0377429

Browse files
committed
Safe arithmetic ops
1 parent 6664f4b commit 0377429

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/task/blocking.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ fn create_blocking_thread() {
260260
let _ = thread::Builder::new()
261261
.name("async-blocking-driver-dynamic".to_string())
262262
.spawn(move || {
263-
let wait_limit = Duration::from_millis(1000 + rand_sleep_ms);
263+
let wait_limit = Duration::from_millis(1000_u64 + rand_sleep_ms);
264264

265265
// Adjust the pool size counter before and after spawn
266266
*POOL_SIZE.lock().unwrap() += 1;
@@ -276,7 +276,12 @@ fn create_blocking_thread() {
276276
// Also, some systems have it(like macOS), and some don't(Linux).
277277
// This case expected not to happen.
278278
// But when happened this shouldn't throw a panic.
279-
MAX_THREADS.store(*POOL_SIZE.lock().unwrap() - 1, Ordering::SeqCst);
279+
let guarded_count = POOL_SIZE
280+
.lock()
281+
.unwrap()
282+
.checked_sub(1)
283+
.expect("shouldn't underflow");
284+
MAX_THREADS.store(guarded_count, Ordering::SeqCst);
280285
}
281286
_ => eprintln!(
282287
"cannot start a dynamic thread driving blocking tasks: {}",

0 commit comments

Comments
 (0)