Skip to content

Fix asyncio event loop creation for Python 3.10+ compatibility #29

@NetPenguins

Description

@NetPenguins

Issue: Fix asyncio event loop creation for Python 3.10+ compatibility

Problem

Code Location
The current implementation of start_and_run_forever() fails on Python 3.10+ due to changes in asyncio.get_event_loop() behavior.
Breaking Change in 3.14

Error:

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

Root Cause

asyncio.get_event_loop() behavior changed in Python 3.10+:

  • Python <3.10: Automatically creates event loop if none exists
  • Python 3.10-3.13: Emits DeprecationWarning when no loop exists (Deprecation in 3.10+, PEP reference)
  • Python 3.14+: Raises RuntimeError instead of creating loop (docs)

Solution

Explicitly create and set the event loop before use, following Python 3.10+ best practices.

Code Changes

File: mythic_container/mythic_service.py

Current code:

def start_and_run_forever():
    try:
        loop = asyncio.get_event_loop()
        loop.run_until_complete(start_services())
        loop.run_forever()
        logger.error("start_and_run_forever finished")
    except KeyboardInterrupt:
        sys.exit(0)

Proposed fix:

def start_and_run_forever():
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    try:
        loop.run_until_complete(start_services())
        loop.run_forever()
        logger.error("start_and_run_forever finished")
    except KeyboardInterrupt:
        sys.exit(0)
    finally:
        loop.close()

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions