1
1
use crate :: deno_runtime:: DenoRuntime ;
2
+ use crate :: rt_worker:: supervisor;
2
3
use crate :: rt_worker:: utils:: { get_event_metadata, parse_worker_conf} ;
3
4
use crate :: rt_worker:: worker_ctx:: create_supervisor;
4
5
use crate :: utils:: send_event_if_event_worker_available;
@@ -7,7 +8,7 @@ use event_worker::events::{
7
8
EventMetadata , ShutdownEvent , ShutdownReason , UncaughtExceptionEvent , WorkerEventWithMetadata ,
8
9
WorkerEvents , WorkerMemoryUsed ,
9
10
} ;
10
- use futures_util:: { pin_mut , FutureExt } ;
11
+ use futures_util:: FutureExt ;
11
12
use log:: { debug, error} ;
12
13
use sb_core:: conn_sync:: ConnSync ;
13
14
use sb_workers:: context:: { UserWorkerMsgs , WorkerContextInitOpts } ;
@@ -22,10 +23,10 @@ use tokio::sync::{oneshot, watch, Notify};
22
23
use tokio:: time:: Instant ;
23
24
use uuid:: Uuid ;
24
25
26
+ use super :: rt;
25
27
use super :: supervisor:: CPUUsageMetrics ;
26
28
use super :: worker_ctx:: TerminationToken ;
27
29
use super :: worker_pool:: SupervisorPolicy ;
28
- use super :: { rt, supervisor} ;
29
30
30
31
#[ derive( Clone ) ]
31
32
pub struct Worker {
@@ -139,7 +140,11 @@ impl Worker {
139
140
140
141
_cpu_timer = maybe_timer;
141
142
pending ( ) . boxed ( )
142
- } else if let Some ( token) = termination_token. as_ref ( ) {
143
+ } else if let Some ( token) = termination_token. clone ( ) {
144
+ let is_terminated = new_runtime. is_terminated . clone ( ) ;
145
+ let is_termination_requested =
146
+ new_runtime. is_termination_requested . clone ( ) ;
147
+
143
148
let ( waker, thread_safe_handle) = {
144
149
let js_runtime = & mut new_runtime. js_runtime ;
145
150
(
@@ -148,32 +153,38 @@ impl Worker {
148
153
)
149
154
} ;
150
155
151
- async move {
152
- token. inbound . cancelled ( ) . await ;
153
- thread_safe_handle. request_interrupt (
154
- supervisor:: handle_interrupt,
155
- Box :: into_raw ( Box :: new ( supervisor:: IsolateInterruptData {
156
- should_terminate : true ,
157
- isolate_memory_usage_tx : None ,
158
- } ) )
159
- as * mut std:: ffi:: c_void ,
160
- ) ;
161
-
162
- waker. wake ( ) ;
163
-
164
- let _ = termination_event_tx. send ( WorkerEvents :: Shutdown (
165
- ShutdownEvent {
166
- reason : ShutdownReason :: TerminationRequested ,
167
- cpu_time_used : 0 ,
168
- memory_used : WorkerMemoryUsed {
169
- total : 0 ,
170
- heap : 0 ,
171
- external : 0 ,
156
+ rt:: SUPERVISOR_RT
157
+ . spawn ( async move {
158
+ token. inbound . cancelled ( ) . await ;
159
+
160
+ is_termination_requested. raise ( ) ;
161
+ thread_safe_handle. request_interrupt (
162
+ supervisor:: handle_interrupt,
163
+ Box :: into_raw ( Box :: new ( supervisor:: IsolateInterruptData {
164
+ should_terminate : true ,
165
+ isolate_memory_usage_tx : None ,
166
+ } ) )
167
+ as * mut std:: ffi:: c_void ,
168
+ ) ;
169
+
170
+ while !is_terminated. is_raised ( ) {
171
+ waker. wake ( ) ;
172
+ tokio:: task:: yield_now ( ) . await ;
173
+ }
174
+
175
+ let _ = termination_event_tx. send ( WorkerEvents :: Shutdown (
176
+ ShutdownEvent {
177
+ reason : ShutdownReason :: TerminationRequested ,
178
+ cpu_time_used : 0 ,
179
+ memory_used : WorkerMemoryUsed {
180
+ total : 0 ,
181
+ heap : 0 ,
182
+ external : 0 ,
183
+ } ,
172
184
} ,
173
- } ,
174
- ) ) ;
175
- }
176
- . boxed ( )
185
+ ) ) ;
186
+ } )
187
+ . boxed ( )
177
188
} else {
178
189
pending ( ) . boxed ( )
179
190
} ;
@@ -186,28 +197,17 @@ impl Worker {
186
197
Some ( worker_name) ,
187
198
) ;
188
199
189
- let mut termination_fut_resolved = false ;
190
-
191
- pin_mut ! ( termination_fut) ;
192
- pin_mut ! ( data) ;
200
+ let result = data. await ;
193
201
194
- loop {
195
- tokio:: select! {
196
- _ = & mut termination_fut, if !termination_fut_resolved => {
197
- termination_fut_resolved = true ;
198
- }
199
-
200
- result = & mut data => {
201
- if let Some ( token) = termination_token. as_ref( ) {
202
- if is_user_worker || termination_fut_resolved {
203
- token. outbound. cancel( ) ;
204
- }
205
- }
206
-
207
- break result
208
- }
202
+ if let Some ( token) = termination_token. as_ref ( ) {
203
+ if !is_user_worker {
204
+ let _ = termination_fut. await ;
209
205
}
206
+
207
+ token. outbound . cancel ( ) ;
210
208
}
209
+
210
+ result
211
211
}
212
212
213
213
Err ( err) => {
0 commit comments