Skip to content

Python 3.14 Compatibility: asyncio.get_event_loop() raises RuntimeError #512

@mbrisx

Description

@mbrisx

When running arq with Python 3.14, the worker fails to start with a RuntimeError due to the deprecated asyncio.get_event_loop() behavior that is now enforced in Python 3.14.

Environment

  • arq version: 0.26.3
  • Python version: 3.14.2
  • OS: Linux (Docker container with python:3.14-slim)

Error

Traceback (most recent call last):
  File "/app/.venv/bin/arq", line 10, in <module>
    sys.exit(cli())
             ~~~^^
  File "/app/.venv/lib/python3.14/site-packages/click/core.py", line 1485, in __call__
    return self.main(*args, **kwargs)
           ~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.14/site-packages/click/core.py", line 1406, in main
    rv = self.invoke(ctx)
  File "/app/.venv/lib/python3.14/site-packages/click/core.py", line 1269, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.14/site-packages/click/core.py", line 824, in invoke
    return callback(*args, **kwargs)
  File "/app/.venv/lib/python3.14/site-packages/arq/cli.py", line 54, in cli
    run_worker(worker_settings_, **kwargs)
    ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.14/site-packages/arq/worker.py", line 893, in run_worker
    worker = create_worker(settings_cls, **kwargs)
  File "/app/.venv/lib/python3.14/site-packages/arq/worker.py", line 889, in create_worker
    return Worker(**{**get_kwargs(settings_cls), **kwargs})
  File "/app/.venv/lib/python3.14/site-packages/arq/worker.py", line 269, in __init__
    self.loop = asyncio.get_event_loop()
                ~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/local/lib/python3.14/asyncio/events.py", line 715, in get_event_loop
    raise RuntimeError('There is no current event loop in thread %r.'
                       % threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'MainThread'.

Root Cause

In Python 3.14, asyncio.get_event_loop() now raises a RuntimeError when called from a thread without a running event loop. This behavior was deprecated since Python 3.10 and is now enforced.

The issue is in arq/worker.py line 269:

self.loop = asyncio.get_event_loop()

Suggested Fix

Replace:

self.loop = asyncio.get_event_loop()

With:

try:
    self.loop = asyncio.get_running_loop()
except RuntimeError:
    self.loop = asyncio.new_event_loop()
    asyncio.set_event_loop(self.loop)

Or use asyncio.new_event_loop() directly where appropriate.

References

Workaround

Currently using Python 3.13 as a workaround until this is resolved.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions