Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 33.9k
gh-94182: run the PidfdChildWatcher on the running loop#94184
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
e49365f49cfa813a3d5e260dafa116f1c4ec8ae89d8229efc647302202190f29607d8edad94d96e5615510370784e69faf740e0c27721ea1f4f6fc5cad52315647edb102ba29de21df0d30824f978c822fbeb1a76fe4985592fcb9d27f174871507f5dfc542a4fac11fc2e40065a2db6d25f852File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -4,6 +4,7 @@ | ||
| import sys | ||
| import unittest | ||
| import warnings | ||
| import functools | ||
| from unittest import mock | ||
| import asyncio | ||
| @@ -30,6 +31,19 @@ | ||
| 'sys.stdout.buffer.write(data)'))] | ||
| @functools.cache | ||
| def _has_pidfd_support(): | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This same function is also introduced by @kumaraditya303's PR GH-98024 (which uses PidfdChildWatcher when supported). I propose to refactor things so that everything uses Kumar's version. We can do that in Kumar's PR once this PR is merged. | ||
| if not hasattr(os, 'pidfd_open'): | ||
| return False | ||
| try: | ||
| os.close(os.pidfd_open(os.getpid())) | ||
| except OSError: | ||
| return False | ||
| return True | ||
| def tearDownModule(): | ||
| asyncio.set_event_loop_policy(None) | ||
| @@ -708,17 +722,8 @@ class SubprocessFastWatcherTests(SubprocessWatcherMixin, | ||
| Watcher = unix_events.FastChildWatcher | ||
| def has_pidfd_support(): | ||
| if not hasattr(os, 'pidfd_open'): | ||
| return False | ||
| try: | ||
| os.close(os.pidfd_open(os.getpid())) | ||
| except OSError: | ||
| return False | ||
| return True | ||
| @unittest.skipUnless( | ||
| has_pidfd_support(), | ||
| _has_pidfd_support(), | ||
| "operating system does not support pidfds", | ||
| ) | ||
| class SubprocessPidfdWatcherTests(SubprocessWatcherMixin, | ||
| @@ -751,6 +756,35 @@ async def execute(): | ||
| mock.call.__exit__(RuntimeError, mock.ANY, mock.ANY), | ||
| ]) | ||
| @unittest.skipUnless( | ||
| _has_pidfd_support(), | ||
| "operating system does not support pidfds", | ||
| ) | ||
| def test_create_subprocess_with_pidfd(self): | ||
| async def in_thread(): | ||
| proc = await asyncio.create_subprocess_exec( | ||
| *PROGRAM_CAT, | ||
| stdin=subprocess.PIPE, | ||
| stdout=subprocess.PIPE, | ||
| ) | ||
| stdout, stderr = await proc.communicate(b"some data") | ||
| return proc.returncode, stdout | ||
| async def main(): | ||
| # asyncio.Runner did not call asyncio.set_event_loop() | ||
| with self.assertRaises(RuntimeError): | ||
| asyncio.get_event_loop_policy().get_event_loop() | ||
| return await asyncio.to_thread(asyncio.run, in_thread()) | ||
| asyncio.set_child_watcher(asyncio.PidfdChildWatcher()) | ||
| try: | ||
| with asyncio.Runner(loop_factory=asyncio.new_event_loop) as runner: | ||
| returncode, stdout = runner.run(main()) | ||
| self.assertEqual(returncode, 0) | ||
| self.assertEqual(stdout, b'some data') | ||
| finally: | ||
| asyncio.set_child_watcher(None) | ||
| else: | ||
| # Windows | ||
| class SubprocessProactorTests(SubprocessMixin, test_utils.TestCase): | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| run the :class:`asyncio.PidfdChildWatcher` on the running loop, this allows event loops to run subprocesses when there is no default event loop running on the main thread | ||
1st1 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious why we can't pass the loop as an argument as well? (Though maybe that creates an unhealthy self-reference to the loop.)