Skip to content

Commit 967eae6

Browse files
committed
wip: prevent dispatch from non-main thread to run immediately
1 parent f958deb commit 967eae6

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

pulpcore/tasking/tasks.py

+12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sys
88
import traceback
99
import tempfile
10+
import threading
1011
from asgiref.sync import sync_to_async
1112
from datetime import timedelta
1213
from gettext import gettext as _
@@ -125,6 +126,12 @@ def _execute_task(task):
125126
send_task_notification(task)
126127

127128

129+
def running_from_thread_pool() -> bool:
130+
thread_name = threading.current_thread().name
131+
# ThreadPoolExecutor names threads like: "ThreadPoolExecutor-0_0"
132+
return "ThreadPoolExecutor" in thread_name
133+
134+
128135
def dispatch(
129136
func,
130137
args=None,
@@ -174,6 +181,11 @@ def dispatch(
174181

175182
assert deferred or immediate, "A task must be at least `deferred` or `immediate`."
176183

184+
# Can't run short tasks immediately if running from thread pool
185+
force_defer = True if running_from_thread_pool() else False
186+
short = immediate
187+
immediate = short and not force_defer
188+
177189
if callable(func):
178190
function_name = f"{func.__module__}.{func.__name__}"
179191
else:

0 commit comments

Comments
 (0)