Skip to content

Commit 299beb9

Browse files
committed
fix: polishing supervisor nits
1 parent 9b2d794 commit 299beb9

File tree

2 files changed

+27
-32
lines changed

2 files changed

+27
-32
lines changed

crates/base/src/rt_worker/supervisor/strategy_per_request.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ pub async fn supervise(args: Arguments, oneshot: bool) -> ShutdownReason {
5555

5656
notify.notify_one();
5757

58-
if let Some(x) = cpu_timer.as_ref() {
59-
if let Err(ex) = x.reset() {
58+
if let Some(cpu_timer) = cpu_timer.as_ref() {
59+
if let Err(ex) = cpu_timer.reset() {
6060
error!("cannot reset the cpu timer: {}", ex);
6161
}
6262
}
@@ -100,7 +100,7 @@ pub async fn supervise(args: Arguments, oneshot: bool) -> ShutdownReason {
100100
continue;
101101
}
102102

103-
Some(x) => {
103+
Some(reason) => {
104104
thread_safe_handle.request_interrupt(
105105
handle_interrupt,
106106
Box::into_raw(Box::new(IsolateInterruptData {
@@ -109,7 +109,7 @@ pub async fn supervise(args: Arguments, oneshot: bool) -> ShutdownReason {
109109
})) as *mut std::ffi::c_void,
110110
);
111111

112-
return x;
112+
return reason;
113113
}
114114

115115
None => continue,

crates/base/src/rt_worker/supervisor/strategy_per_worker.rs

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ pub async fn supervise(args: Arguments) -> ShutdownReason {
4343
.unwrap_or(Duration::from_millis(0)),
4444
);
4545

46+
let interrupt_fn = {
47+
let thread_safe_handle = thread_safe_handle.clone();
48+
move |should_terminate: bool| {
49+
let interrupt_data = IsolateInterruptData {
50+
should_terminate,
51+
isolate_memory_usage_tx,
52+
};
53+
54+
thread_safe_handle.request_interrupt(
55+
handle_interrupt,
56+
Box::into_raw(Box::new(interrupt_data)) as *mut std::ffi::c_void,
57+
);
58+
}
59+
};
60+
4661
tokio::pin!(wall_clock_duration_alert);
4762

4863
loop {
@@ -55,22 +70,13 @@ pub async fn supervise(args: Arguments) -> ShutdownReason {
5570
cpu_time_soft_limit_reached = true;
5671

5772
if req_ack_count == demand.load(Ordering::Acquire) {
58-
let interrupt_data = IsolateInterruptData {
59-
should_terminate: true,
60-
isolate_memory_usage_tx
61-
};
62-
63-
thread_safe_handle.request_interrupt(handle_interrupt, Box::into_raw(Box::new(interrupt_data)) as *mut std::ffi::c_void);
73+
interrupt_fn(true);
6474
error!("early termination due to the last request being completed. isolate: {:?}", key);
6575
return ShutdownReason::EarlyDrop;
6676
}
6777
} else {
6878
// shutdown worker
69-
let interrupt_data = IsolateInterruptData {
70-
should_terminate: true,
71-
isolate_memory_usage_tx
72-
};
73-
thread_safe_handle.request_interrupt(handle_interrupt, Box::into_raw(Box::new(interrupt_data)) as *mut std::ffi::c_void);
79+
interrupt_fn(true);
7480
error!("CPU time hard limit reached. isolate: {:?}", key);
7581
return ShutdownReason::CPUTime;
7682
}
@@ -91,12 +97,7 @@ pub async fn supervise(args: Arguments) -> ShutdownReason {
9197
continue;
9298
}
9399

94-
let interrupt_data = IsolateInterruptData {
95-
should_terminate: true,
96-
isolate_memory_usage_tx
97-
};
98-
99-
thread_safe_handle.request_interrupt(handle_interrupt, Box::into_raw(Box::new(interrupt_data)) as *mut std::ffi::c_void);
100+
interrupt_fn(true);
100101
error!("early termination due to the last request being completed. isolate: {:?}", key);
101102
return ShutdownReason::EarlyDrop;
102103
}
@@ -115,27 +116,21 @@ pub async fn supervise(args: Arguments) -> ShutdownReason {
115116
// wall-clock limit reached
116117
// Don't terminate isolate from supervisor when wall-clock
117118
// duration reached. It's dropped in deno_runtime.rs
118-
let interrupt_data = IsolateInterruptData {
119+
interrupt_fn(
119120
// NOTE: Wall clock is also triggered when no more
120121
// pending requests, so we must compare the request
121122
// count here to judge whether we need to terminate the
122123
// isolate.
123-
should_terminate: req_ack_count == demand.load(Ordering::Acquire),
124-
isolate_memory_usage_tx
125-
};
126-
thread_safe_handle.request_interrupt(handle_interrupt, Box::into_raw(Box::new(interrupt_data)) as *mut std::ffi::c_void);
127-
error!("wall clock duration reached. isolate: {:?}", key);
124+
req_ack_count == demand.load(Ordering::Acquire),
125+
);
126+
128127
return ShutdownReason::WallClockTime;
129128
}
130129
}
131130

132131
// memory usage
133132
Some(_) = memory_limit_rx.recv() => {
134-
let interrupt_data = IsolateInterruptData {
135-
should_terminate: true,
136-
isolate_memory_usage_tx
137-
};
138-
thread_safe_handle.request_interrupt(handle_interrupt, Box::into_raw(Box::new(interrupt_data)) as *mut std::ffi::c_void);
133+
interrupt_fn(true);
139134
error!("memory limit reached for the worker. isolate: {:?}", key);
140135
return ShutdownReason::Memory;
141136
}

0 commit comments

Comments
 (0)