@@ -318,7 +318,7 @@ impl<'py> Python<'py> {
318
318
use std:: mem:: transmute;
319
319
use std:: panic:: { catch_unwind, resume_unwind, AssertUnwindSafe } ;
320
320
use std:: sync:: mpsc:: { sync_channel, SendError , SyncSender } ;
321
- use std:: thread:: { spawn , Result } ;
321
+ use std:: thread:: { Builder , Result } ;
322
322
use std:: time:: Duration ;
323
323
324
324
use parking_lot:: { const_mutex, Mutex } ;
@@ -344,11 +344,15 @@ impl<'py> Python<'py> {
344
344
let mut f = Some ( f) ;
345
345
346
346
let mut task = || {
347
- let f = f. take ( ) . unwrap ( ) ;
347
+ let f = f
348
+ . take ( )
349
+ . expect ( "allow_threads closure called more than once" ) ;
348
350
349
351
let result = catch_unwind ( AssertUnwindSafe ( f) ) ;
350
352
351
- result_sender. send ( result) . unwrap ( ) ;
353
+ result_sender
354
+ . send ( result)
355
+ . expect ( "allow_threads runtime task was abandoned" ) ;
352
356
} ;
353
357
354
358
// SAFETY: the current thread will block until the closure has returned
@@ -371,22 +375,27 @@ impl<'py> Python<'py> {
371
375
372
376
let ( task_sender, task_receiver) = sync_channel :: < Task > ( 0 ) ;
373
377
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) ;
376
382
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 ) ( ) } ;
380
386
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" ) ;
384
391
385
392
task_sender
386
393
} ;
387
394
388
395
// 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" ) ;
390
399
391
400
trap. disarm ( ) ;
392
401
0 commit comments