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

./python.exe -m test test_types test_inspect fails as (env changed) #128694

Closed
sobolevn opened this issue Jan 9, 2025 · 5 comments
Closed

./python.exe -m test test_types test_inspect fails as (env changed) #128694

sobolevn opened this issue Jan 9, 2025 · 5 comments
Assignees
Labels
tests Tests in the Lib/test dir topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@sobolevn
Copy link
Member

sobolevn commented Jan 9, 2025

Bug report

» ./python.exe -m test test_types test_inspect                                  
Using random seed: 1107570136
0:00:00 load avg: 3.01 Run 2 tests sequentially in a single process
0:00:00 load avg: 3.01 [1/2] test_types
0:00:00 load avg: 3.01 [2/2] test_inspect.test_inspect
Warning -- asyncio.events._event_loop_policy was modified by test_inspect.test_inspect
Warning --   Before: None
Warning --   After:  <asyncio.unix_events._UnixDefaultEventLoopPolicy object at 0x102437760> 
test_inspect.test_inspect failed (env changed)

== Tests result: SUCCESS ==

1 test altered the execution environment (env changed):
    test_inspect.test_inspect

1 test OK.

Total duration: 1.0 sec
Total tests: run=473
Total test files: run=2/2 env_changed=1
Result: SUCCESS

I am working on the fix.

Linked PRs

@sobolevn sobolevn added type-bug An unexpected behavior, bug, or error tests Tests in the Lib/test dir labels Jan 9, 2025
@sobolevn sobolevn self-assigned this Jan 9, 2025
@sobolevn
Copy link
Member Author

sobolevn commented Jan 9, 2025

test_types.py can only contain import unittest.mock to trigger this problem.

@sobolevn
Copy link
Member Author

sobolevn commented Jan 9, 2025

Because import asyncio is called in import unittest.mock

@sobolevn
Copy link
Member Author

sobolevn commented Jan 10, 2025

What happens is:

  • The first test module imports asyncio and saves it to sys.modules
  • This allows self.try_get_module('asyncio') to pass the second time
    def get_asyncio_events__event_loop_policy(self):
    self.try_get_module('asyncio')
    return support.maybe_get_event_loop_policy()
    def restore_asyncio_events__event_loop_policy(self, policy):
    asyncio = self.get_module('asyncio')
    asyncio._set_event_loop_policy(policy)
  • It saves None as the initial value
  • Later IsolatedAsyncioTestCase sets the loop policy via Runner:
    def _setupAsyncioRunner(self):
    assert self._asyncioRunner is None, 'asyncio runner is already initialized'
    runner = asyncio.Runner(debug=True, loop_factory=self.loop_factory)
    self._asyncioRunner = runner
    def _tearDownAsyncioRunner(self):
    runner = self._asyncioRunner
    runner.close()
    def run(self, result=None):
    self._setupAsyncioRunner()
    try:
    return super().run(result)
    finally:
    self._tearDownAsyncioRunner()
  • _set_event_loop_policy(None) is never called in tearDown

Ideas?

CC @vstinner

@vstinner
Copy link
Member

Tests using IsolatedAsyncioTestCase should call _set_event_loop_policy(None).

@sobolevn
Copy link
Member Author

Yes, that's what I did :)
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Tests in the Lib/test dir topic-asyncio type-bug An unexpected behavior, bug, or error
Projects
Status: Done
Development

No branches or pull requests

2 participants