Skip to content

Commit 1c892ad

Browse files
authored
fix(turbopack-node): incorrect idle_workers stats (#82)
1 parent d1a6954 commit 1c892ad

File tree

2 files changed

+13
-21
lines changed

2 files changed

+13
-21
lines changed

turbopack/crates/turbopack-node/src/pool_stats.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ impl NodeJsPoolStats {
4444
}
4545

4646
pub fn finished_booting_worker(&mut self) {
47-
self.booting_workers -= 1;
47+
self.booting_workers = self.booting_workers.saturating_sub(1);
4848
}
4949

5050
pub fn remove_worker(&mut self) {
51-
self.workers -= 1;
51+
self.workers = self.workers.saturating_sub(1);
5252
}
5353

5454
#[allow(unused)]
@@ -60,14 +60,14 @@ impl NodeJsPoolStats {
6060
pub fn add_cold_process_time(&mut self, time: Duration) {
6161
self.total_cold_process_time += time;
6262
self.cold_process_count += 1;
63-
self.queued_tasks -= 1;
63+
self.queued_tasks = self.queued_tasks.saturating_sub(1);
6464
}
6565

6666
#[allow(unused)]
6767
pub fn add_warm_process_time(&mut self, time: Duration) {
6868
self.total_warm_process_time += time;
6969
self.warm_process_count += 1;
70-
self.queued_tasks -= 1;
70+
self.queued_tasks = self.queued_tasks.saturating_sub(1);
7171
}
7272

7373
pub fn estimated_bootup_time(&self) -> Duration {

turbopack/crates/turbopack-node/src/worker_pool/worker_thread.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ static WORKER_TERMINATOR: OnceCell<
2222
ThreadsafeFunction<NapiWorkerTermination, ErrorStrategy::Fatal>,
2323
> = OnceCell::new();
2424

25-
static PENDING_CREATIONS: OnceCell<Mutex<VecDeque<(Arc<WorkerOptions>, oneshot::Sender<u32>)>>> =
26-
OnceCell::new();
25+
static PENDING_CREATIONS: OnceCell<Mutex<VecDeque<oneshot::Sender<u32>>>> = OnceCell::new();
2726

2827
#[napi]
2928
#[allow(unused)]
@@ -58,6 +57,8 @@ pub fn register_worker_scheduler(
5857
pub async fn create_worker(options: Arc<WorkerOptions>) -> anyhow::Result<u32> {
5958
let (tx, rx) = oneshot::channel();
6059

60+
let napi_options = (&options).into();
61+
6162
{
6263
let pending = PENDING_CREATIONS.get_or_init(|| Mutex::new(VecDeque::new()));
6364
// ensure pool entry exists for these options so scale ops can observe it
@@ -66,13 +67,13 @@ pub async fn create_worker(options: Arc<WorkerOptions>) -> anyhow::Result<u32> {
6667
.lock()
6768
.entry(options.clone())
6869
.or_default();
69-
pending.lock().push_back((options.clone(), tx));
70+
pending.lock().push_back(tx);
7071
}
7172

7273
if let Some(creator) = WORKER_CREATOR.get() {
7374
creator.call(
7475
NapiWorkerCreation {
75-
options: options.into(),
76+
options: napi_options,
7677
},
7778
ThreadsafeFunctionCallMode::NonBlocking,
7879
);
@@ -87,19 +88,10 @@ pub async fn create_worker(options: Arc<WorkerOptions>) -> anyhow::Result<u32> {
8788
#[napi]
8889
#[allow(unused)]
8990
pub fn worker_created(worker_id: u32) {
90-
if let Some(pending) = PENDING_CREATIONS.get() {
91-
if let Some((options, tx)) = pending.lock().pop_front() {
92-
// record into global pool
93-
WORKER_POOL_OPERATION
94-
.pools
95-
.lock()
96-
.entry(options.clone())
97-
.or_default()
98-
.idle_workers
99-
.lock()
100-
.push(worker_id);
101-
let _ = tx.send(worker_id);
102-
}
91+
if let Some(pending) = PENDING_CREATIONS.get()
92+
&& let Some(tx) = pending.lock().pop_front()
93+
{
94+
let _ = tx.send(worker_id);
10395
}
10496
}
10597

0 commit comments

Comments
 (0)