Skip to content

Commit f72abf2

Browse files
committed
Explicitly explain failure points in allow_threads implementation for better debuggability.
1 parent 508d4ee commit f72abf2

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

src/marker.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ impl<'py> Python<'py> {
318318
use std::mem::transmute;
319319
use std::panic::{catch_unwind, resume_unwind, AssertUnwindSafe};
320320
use std::sync::mpsc::{sync_channel, SendError, SyncSender};
321-
use std::thread::{spawn, Result};
321+
use std::thread::{Builder, Result};
322322
use std::time::Duration;
323323

324324
use parking_lot::{const_mutex, Mutex};
@@ -344,11 +344,15 @@ impl<'py> Python<'py> {
344344
let mut f = Some(f);
345345

346346
let mut task = || {
347-
let f = f.take().unwrap();
347+
let f = f
348+
.take()
349+
.expect("allow_threads closure called more than once");
348350

349351
let result = catch_unwind(AssertUnwindSafe(f));
350352

351-
result_sender.send(result).unwrap();
353+
result_sender
354+
.send(result)
355+
.expect("allow_threads runtime task was abandoned");
352356
};
353357

354358
// SAFETY: the current thread will block until the closure has returned
@@ -371,22 +375,27 @@ impl<'py> Python<'py> {
371375

372376
let (task_sender, task_receiver) = sync_channel::<Task>(0);
373377

374-
spawn(move || {
375-
let mut next_task = Ok(task);
378+
Builder::new()
379+
.name("pyo3 allow_threads runtime".to_owned())
380+
.spawn(move || {
381+
let mut next_task = Ok(task);
376382

377-
while let Ok(task) = next_task {
378-
// SAFETY: all data accessed by `task` will stay alive until it completes
379-
unsafe { (*task.0)() };
383+
while let Ok(task) = next_task {
384+
// SAFETY: all data accessed by `task` will stay alive until it completes
385+
unsafe { (*task.0)() };
380386

381-
next_task = task_receiver.recv_timeout(Duration::from_secs(60));
382-
}
383-
});
387+
next_task = task_receiver.recv_timeout(Duration::from_secs(60));
388+
}
389+
})
390+
.expect("failed to create allow_threads runtime thread");
384391

385392
task_sender
386393
};
387394

388395
// 3. Wait for completion and check result
389-
let result = result_receiver.recv().unwrap();
396+
let result = result_receiver
397+
.recv()
398+
.expect("allow_threads runtime thread died unexpectedly");
390399

391400
trap.disarm();
392401

0 commit comments

Comments
 (0)