Skip to content

RuntimeError: There is no current event loop in thread 'MainThread' on Python 3.14 #40

@ticdenis

Description

@ticdenis

After upgrading to Python 3.14, running any aiocli command fails with the following error:

Traceback (most recent call last):
  ...
  File "aiocli/commander.py", line 143, in wrapper
    loop_ = loop or 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'.

This happens because Python 3.14 removed implicit event loop creation (see PEP 719), so asyncio.get_event_loop() now raises RuntimeError when called outside a running loop.

Expected Behavior

aiocli should automatically create an event loop if none exists, maintaining backward compatibility with Python ≤3.13.

Actual Behavior

RuntimeError: There is no current event loop in thread 'MainThread'.

Suggested Fix

Wrap the call in aiocli/commander.py with safe event loop initialization:

import asyncio

def get_safe_event_loop():
    try:
        return asyncio.get_running_loop()
    except RuntimeError:
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        return loop

Then replace:

loop_ = loop or get_event_loop()

with:

loop_ = loop or get_safe_event_loop()

Additional Notes

Also seeing a warning from pyparsing under Python 3.14:

SyntaxWarning: 'return' in a 'finally' block

(probably unrelated, just noticed in the same run).

Metadata

Metadata

Assignees

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