From 3d2a6049638aef9d3a066e18d73db7c2e56e34c0 Mon Sep 17 00:00:00 2001 From: Cameron LeCrone Date: Fri, 16 Apr 2021 17:03:13 -0400 Subject: [PATCH 1/3] Addresses #1958: Move code that handles job into its own method --- trio/_core/_thread_cache.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/trio/_core/_thread_cache.py b/trio/_core/_thread_cache.py index ae5e8450b9..ffea5a0ec8 100644 --- a/trio/_core/_thread_cache.py +++ b/trio/_core/_thread_cache.py @@ -56,21 +56,22 @@ def __init__(self, thread_cache): thread.name = f"Trio worker thread {next(name_counter)}" thread.start() + def _handle_job(self): + fn, deliver = self._job + self._job = None + result = outcome.capture(fn) + # Tell the cache that we're available to be assigned a new + # job. We do this *before* calling 'deliver', so that if + # 'deliver' triggers a new job, it can be assigned to us + # instead of spawning a new thread. + self._thread_cache._idle_workers[self] = None + deliver(result) + def _work(self): while True: if self._worker_lock.acquire(timeout=IDLE_TIMEOUT): # We got a job - fn, deliver = self._job - self._job = None - result = outcome.capture(fn) - # Tell the cache that we're available to be assigned a new - # job. We do this *before* calling 'deliver', so that if - # 'deliver' triggers a new job, it can be assigned to us - # instead of spawning a new thread. - self._thread_cache._idle_workers[self] = None - deliver(result) - del fn - del deliver + self._handle_job() else: # Timeout acquiring lock, so we can probably exit. But, # there's a race condition: we might be assigned a job *just* From ebea9283ac57272be2d6f9f05ac9e72d7e0cdb39 Mon Sep 17 00:00:00 2001 From: Cameron LeCrone Date: Sat, 17 Apr 2021 12:19:52 -0400 Subject: [PATCH 2/3] Add a comment explaining why _handle_job() is a separate method --- trio/_core/_thread_cache.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/trio/_core/_thread_cache.py b/trio/_core/_thread_cache.py index ffea5a0ec8..8ce37fff7e 100644 --- a/trio/_core/_thread_cache.py +++ b/trio/_core/_thread_cache.py @@ -57,6 +57,8 @@ def __init__(self, thread_cache): thread.start() def _handle_job(self): + # Handle job in a separate method to ensure user-created + # objects are cleaned up in a consistent manner. fn, deliver = self._job self._job = None result = outcome.capture(fn) From b1d558b805acbfbd9609eaf9b092e593a4e14eba Mon Sep 17 00:00:00 2001 From: Cameron LeCrone Date: Sat, 17 Apr 2021 12:33:54 -0400 Subject: [PATCH 3/3] Fix formatting via Black --- trio/_core/_thread_cache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trio/_core/_thread_cache.py b/trio/_core/_thread_cache.py index 8ce37fff7e..fc3b8a9508 100644 --- a/trio/_core/_thread_cache.py +++ b/trio/_core/_thread_cache.py @@ -58,7 +58,7 @@ def __init__(self, thread_cache): def _handle_job(self): # Handle job in a separate method to ensure user-created - # objects are cleaned up in a consistent manner. + # objects are cleaned up in a consistent manner. fn, deliver = self._job self._job = None result = outcome.capture(fn)