Skip to content

ValueError: signal only works in main thread of the main interpreter #284

@Avasam

Description

@Avasam

Describe the bug
When trying to enqueue a worker, I get the following error:

Traceback (most recent call last):
  File "/root/.cache/pypoetry/virtualenvs/backend-MATOk_fk-py3.12/lib/python3.12/site-packages/scheduler/helpers/queues/queue_logic.py", line 48, in perform_job
    job_model.success_callback(job_model, connection, result)  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/backend-MATOk_fk-py3.12/lib/python3.12/site-packages/scheduler/helpers/callback.py", line 36, in __call__
    with SCHEDULER_CONFIG.DEATH_PENALTY_CLASS(self.timeout, JobTimeoutException):
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/backend-MATOk_fk-py3.12/lib/python3.12/site-packages/scheduler/timeouts.py", line 32, in __enter__
    self.setup_death_penalty()
  File "/root/.cache/pypoetry/virtualenvs/backend-MATOk_fk-py3.12/lib/python3.12/site-packages/scheduler/timeouts.py", line 62, in setup_death_penalty
    signal.signal(signal.SIGALRM, self.handle_death_penalty)
  File "/usr/local/lib/python3.12/signal.py", line 58, in signal
    handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: signal only works in main thread of the main interpreter



During handling of the above exception, another exception occurred:



Traceback (most recent call last):
  File "/root/.cache/pypoetry/virtualenvs/backend-MATOk_fk-py3.12/lib/python3.12/site-packages/scheduler/helpers/queues/queue_logic.py", line 289, in run_sync
    result = perform_job(job, self.connection)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/backend-MATOk_fk-py3.12/lib/python3.12/site-packages/scheduler/helpers/queues/queue_logic.py", line 52, in perform_job
    job_model.failure_callback(job_model, connection, *sys.exc_info())  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/backend-MATOk_fk-py3.12/lib/python3.12/site-packages/scheduler/helpers/callback.py", line 36, in __call__
    with SCHEDULER_CONFIG.DEATH_PENALTY_CLASS(self.timeout, JobTimeoutException):
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/backend-MATOk_fk-py3.12/lib/python3.12/site-packages/scheduler/timeouts.py", line 32, in __enter__
    self.setup_death_penalty()
  File "/root/.cache/pypoetry/virtualenvs/backend-MATOk_fk-py3.12/lib/python3.12/site-packages/scheduler/timeouts.py", line 62, in setup_death_penalty
    signal.signal(signal.SIGALRM, self.handle_death_penalty)
  File "/usr/local/lib/python3.12/signal.py", line 58, in signal
    handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: signal only works in main thread of the main interpreter

Image

To Reproduce
Steps to reproduce the behavior:

  1. I created a method sync_shopify_event_orders(event_id: int) under api/tasks.py with some logs inside.
  2. I created a new task in the Django Admin Panel with the following configs:
    Image
  3. Go to queues
  4. Press enqueue

Expected behavior
No error

Screenshots
(see above)

Desktop (please complete the following information):

  • OS: Docker python:3.12-slim
  • python version: 3.12
  • django version: 5.0.12
  • pyproject dependencies:
dependencies = [
  "django ~=5.0.6",
  "django-allauth[socialaccount] ~=0.62.1",
  "django-anymail ~=10.3",
  "django-cors-headers ~=4.3.1",
  "django-environ ~=0.11.2",
  "django-tasks-scheduler ~=4.0.5",
  "django-widget-tweaks ~=1.5.0",
  "djangorestframework ~=3.15.1",
  "djangorestframework-camel-case ~=1.4.2",
  "gunicorn[gevent] ~=22.0.0",
  "pillow ~=10.4.0",
  "psycogreen ~=1.0.2",
  "psycopg2-binary ~=2.9.9",
  "uvicorn[standard] ~=0.29.0",
  "zygoat-django ~=1.0.1",
]

Additional context
This is my Django server start command in the dockerfile:

CMD poetry run ./manage.py wait_for_db \
  && poetry run ./manage.py migrate \
  && poetry run ./manage.py migrate --database=frontend_data \
  && poetry run ./manage.py createcachetable \
  && poetry run uvicorn --host 0.0.0.0 --port 3001 --reload backend.asgi:application

If I run poetry run ./manage.py scheduler_worker directly in the Docker, I see my task's own logs, followed by WARNING:scheduler:Job default:4:20250630180506946254 failed with exception: signal only works in main thread of the main interpreter.

And it runs every 10 seconds.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions