fix(worker): record worker state at startup, for every pool

in_worker() read task_join_will_block, which eventlet and gevent leave unset,
and the task's current_worker_task, which they scope to one greenlet -- so a
greenlet a task spawned in those pools still took the web-process branch and
dispatched to its own worker, where it could queue behind its parent and time
out.

The worker's own startup now records it: worker_init fires in every worker's
main process before the pool starts (where solo, threads, eventlet and gevent
run tasks, and what prefork children fork from), and worker_process_init in
each prefork child. worker_ready would be too late -- prefork children are
forked before it fires. The two existing checks stay for anything that runs
tasks without that startup.
This commit is contained in:
Alex committed 2026-09-19 14:33:14 +01:00
1 parent a20402a6dc
commit 877609dfb6
2 files changed
+56 -11

No files matched your search

+22
View File
@@ -317,6 +317,28 @@ class TestInWorker:
with denied_join_result():
assert self._ask_from_a_new_thread() is True
def test_true_on_any_thread_of_a_non_blocking_pool_worker(self, monkeypatch):
# eventlet/gevent leave the join flag unset and scope the current task
# to one greenlet, so only the worker's own startup can say this
# process is a worker. The lifecycle signal records that for every
# thread and greenlet in it.
import docsgpt.celery_init as celery_init
monkeypatch.setattr(celery_init, "_IS_WORKER_PROCESS", False)
assert self._ask_from_a_new_thread() is False
celery_init.worker_init.send(sender=None)
assert self._ask_from_a_new_thread() is True
def test_prefork_children_record_it_on_their_own_start(self, monkeypatch):
import docsgpt.celery_init as celery_init
monkeypatch.setattr(celery_init, "_IS_WORKER_PROCESS", False)
celery_init.worker_process_init.send(sender=None)
assert self._ask_from_a_new_thread() is True
def test_true_on_the_task_thread_of_a_non_blocking_pool(self):
# eventlet/gevent pools leave the process flag unset; the thread
# running the task still knows it is in one.