Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.25.1 regression Python 3.9 asyncio_mode="auto" # RuntimeError: There is no current event loop in thread 'MainThread' #1039

Open
dimaqq opened this issue Jan 7, 2025 · 3 comments

Comments

@dimaqq
Copy link

dimaqq commented Jan 7, 2025

Happy new year.

0.25.1 was released recently and somehow it fails on Python 3.9 specifically.

In my case, asyncio_mode = "auto" is used, so maybe similar to #658, however I don't know if that's required for the regression.

Meanwhile, Python 3.8 is OK (0.25.x can't be used, 0.24 is used instead)
And Python 3.10, 3.11, 3.12, 3.13 are OK (with the new version)

Version resolution:

    { name = "pytest-asyncio", version = "0.24.0", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.9'" },
    { name = "pytest-asyncio", version = "0.25.1", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.9'" },

Run log: https://github.com/juju/python-libjuju/actions/runs/12423507633

Screenshot for the above:
Screenshot 2025-01-07 at 14 43 51

I've manually validated that 0.25.0 works fine in my setup.

@dimaqq
Copy link
Author

dimaqq commented Jan 7, 2025

Log, if helps any:

self = <tests.unit.test_model.TestModelState testMethod=test_apply_delta>

    def test_apply_delta(self):
>       model = Model()

tests/unit/test_model.py:72:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
juju/model/__init__.py:677: in __init__
    self._watch_stopping = asyncio.Event()
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/locks.py:177: in __init__
    self._loop = events.get_event_loop()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <asyncio.unix_events._UnixDefaultEventLoopPolicy object at 0x105a521f0>

    def get_event_loop(self):
        """Get the event loop for the current context.

        Returns an instance of EventLoop or raises an exception.
        """
        if (self._local._loop is None and
                not self._local._set_called and
                threading.current_thread() is threading.main_thread()):
            self.set_event_loop(self.new_event_loop())

        if self._local._loop is None:
>           raise RuntimeError('There is no current event loop in thread %r.'
                               % threading.current_thread().name)
E           RuntimeError: There is no current event loop in thread 'MainThread'.

@dimaqq
Copy link
Author

dimaqq commented Jan 7, 2025

My config, since the most likely cause is the change that fixed interleaved loop scopes:

[tool.pytest.ini_options]
addopts = "-ra -v"
asyncio_default_fixture_loop_scope = "function"
asyncio_mode = "auto"
filterwarnings = "ignore::DeprecationWarning:websockets"
markers = [
    "serial: mark a test that must run by itself",
    "wait_for_idle: mark a test that waits for the model to be idle",
    "bundle: mark a test that uses a bundle",
]

I don't have custom loop_scope's in the tests, only this config.

I do, however, have a mix of modern pytest tests (module-level test_foo(): ...) and older class-based tests class TestFoo(unittest.TestCase): def test_foo(self): ...)

Easy reproducer:

git clone [email protected]:juju/python-libjuju.git
cd python-libjuju
# currently at a58645e
uvx -p 3.9 tox -e unit

@seifertm
Copy link
Contributor

seifertm commented Jan 8, 2025

Thanks for the detailed report!
The failing test is a synchronous test. Maybe #1029 introduced a regression here.

jujubot added a commit to juju/python-libjuju that referenced this issue Jan 9, 2025
#1250

- update type hints in juju/application.py, pyright is now happy
- correct type hints for facade.Type
- work around pytest-dev/pytest-asyncio#1039
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants