@@ -56,21 +56,24 @@ def __init__(self, thread_cache):
5656 thread .name = f"Trio worker thread { next (name_counter )} "
5757 thread .start ()
5858
59+ def _handle_job (self ):
60+ # Handle job in a separate method to ensure user-created
61+ # objects are cleaned up in a consistent manner.
62+ fn , deliver = self ._job
63+ self ._job = None
64+ result = outcome .capture (fn )
65+ # Tell the cache that we're available to be assigned a new
66+ # job. We do this *before* calling 'deliver', so that if
67+ # 'deliver' triggers a new job, it can be assigned to us
68+ # instead of spawning a new thread.
69+ self ._thread_cache ._idle_workers [self ] = None
70+ deliver (result )
71+
5972 def _work (self ):
6073 while True :
6174 if self ._worker_lock .acquire (timeout = IDLE_TIMEOUT ):
6275 # We got a job
63- fn , deliver = self ._job
64- self ._job = None
65- result = outcome .capture (fn )
66- # Tell the cache that we're available to be assigned a new
67- # job. We do this *before* calling 'deliver', so that if
68- # 'deliver' triggers a new job, it can be assigned to us
69- # instead of spawning a new thread.
70- self ._thread_cache ._idle_workers [self ] = None
71- deliver (result )
72- del fn
73- del deliver
76+ self ._handle_job ()
7477 else :
7578 # Timeout acquiring lock, so we can probably exit. But,
7679 # there's a race condition: we might be assigned a job *just*
0 commit comments