Skip to content

Commit 65ab371

Browse files
committed
Convert internal channel to use Result and do not panic in helper thread
1 parent 985dbae commit 65ab371

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/job_token.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ pub(crate) struct JobToken {
1414
token: Option<Acquired>,
1515
/// A pool to which `token` should be returned. `pool` is optional, as one might want to release a token straight away instead
1616
/// of storing it back in the pool - see [`Self::forget()`] function for that.
17-
pool: Option<Sender<Option<Acquired>>>,
17+
pool: Option<Sender<Option<Result<Acquired, crate::Error>>>>,
1818
}
1919

2020
impl Drop for JobToken {
2121
fn drop(&mut self) {
2222
if let Some(pool) = &self.pool {
23-
let _ = pool.send(self.token.take());
23+
// Always send back an Ok() variant as we know that the acquisition for this token has succeeded.
24+
let _ = pool.send(self.token.take().map(|token| Ok(token)));
2425
}
2526
}
2627
}
@@ -43,8 +44,8 @@ impl JobToken {
4344
/// for reuse if we know we're going to request another token after freeing the current one.
4445
pub(crate) struct JobTokenServer {
4546
helper: HelperThread,
46-
tx: Sender<Option<Acquired>>,
47-
rx: Receiver<Option<Acquired>>,
47+
tx: Sender<Option<Result<Acquired, crate::Error>>>,
48+
rx: Receiver<Option<Result<Acquired, crate::Error>>>,
4849
}
4950

5051
impl JobTokenServer {
@@ -58,12 +59,12 @@ impl JobTokenServer {
5859
tx.send(None).unwrap();
5960
let pool = tx.clone();
6061
let helper = client.into_helper_thread(move |acq| {
61-
let _ = pool.send(Some(acq.unwrap()));
62+
let _ = pool.send(Some(acq.map_err(|e| e.into())));
6263
})?;
6364
Ok(Self { helper, tx, rx })
6465
}
6566

66-
pub(crate) fn acquire(&self) -> JobToken {
67+
pub(crate) fn acquire(&self) -> Result<JobToken, crate::Error> {
6768
let token = if let Ok(token) = self.rx.try_recv() {
6869
// Opportunistically check if there's a token that can be reused.
6970
token
@@ -72,10 +73,15 @@ impl JobTokenServer {
7273
self.helper.request_token();
7374
self.rx.recv().unwrap()
7475
};
75-
JobToken {
76+
let token = if let Some(token) = token {
77+
Some(token?)
78+
} else {
79+
None
80+
};
81+
Ok(JobToken {
7682
token,
7783
pool: Some(self.tx.clone()),
78-
}
84+
})
7985
}
8086
}
8187

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1427,7 +1427,7 @@ impl Build {
14271427
})?;
14281428
for obj in objs {
14291429
let (mut cmd, program) = self.create_compile_object_cmd(obj)?;
1430-
let token = tokens.acquire();
1430+
let token = tokens.acquire()?;
14311431
let child = spawn(&mut cmd, &program, print.pipe_writer_cloned()?.unwrap())?;
14321432

14331433
tx.send((cmd, program, KillOnDrop(child), token))

0 commit comments

Comments
 (0)