-
Notifications
You must be signed in to change notification settings - Fork 141
Prevent Runtime use over forks
#1208
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
Merged
+323
−14
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5a9268c
Raise an assertion error when a Runtime is used by client/worker crea…
VegetarianOrc 41213ef
Add _RuntimeRef to encapsulate default runtime creation. Add Runtime.…
VegetarianOrc 59156b2
remove blank line to fix linter error
VegetarianOrc 5f93b8e
fix use of Self since it's not avaiable in typing until 3.11
VegetarianOrc 811aa64
remove references to ForkContext to avoid exploding in Windows
VegetarianOrc 2461cdd
switch type of fixture to Iterator instead of Generator
VegetarianOrc ba9a8a8
run formatter
VegetarianOrc 9301c51
except the correct error type to prevent breakage on windows
VegetarianOrc c350ed5
Update tests to match error info. Update prevent_default test to demo…
VegetarianOrc db1ede0
fix typo in docstring
VegetarianOrc 0f9bc70
remove empty return in Runtime.set_default. Remove _default_created f…
VegetarianOrc f81fd55
Merge branch 'main' into prevent-fork
VegetarianOrc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| import multiprocessing | ||
| import multiprocessing.context | ||
| import sys | ||
| from dataclasses import dataclass | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| @dataclass | ||
| class _ForkTestResult: | ||
| status: str | ||
| err_name: str | None | ||
| err_msg: str | None | ||
|
|
||
| def __eq__(self, value: object) -> bool: | ||
| if not isinstance(value, _ForkTestResult): | ||
| return False | ||
|
|
||
| valid_err_msg = False | ||
|
|
||
| if self.err_msg and value.err_msg: | ||
| valid_err_msg = ( | ||
| self.err_msg in value.err_msg or value.err_msg in self.err_msg | ||
| ) | ||
|
|
||
| return ( | ||
| value.status == self.status | ||
| and value.err_name == value.err_name | ||
| and valid_err_msg | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def assertion_error(message: str) -> _ForkTestResult: | ||
| return _ForkTestResult( | ||
| status="error", err_name="AssertionError", err_msg=message | ||
| ) | ||
|
|
||
|
|
||
| class _TestFork: | ||
| _expected: _ForkTestResult | ||
|
|
||
| async def coro(self) -> Any: | ||
| raise NotImplementedError() | ||
|
|
||
| def entry(self): | ||
| event_loop = asyncio.new_event_loop() | ||
| asyncio.set_event_loop(event_loop) | ||
| try: | ||
| event_loop.run_until_complete(self.coro()) | ||
| payload = _ForkTestResult(status="ok", err_name=None, err_msg=None) | ||
| except BaseException as err: | ||
| payload = _ForkTestResult( | ||
| status="error", err_name=err.__class__.__name__, err_msg=str(err) | ||
| ) | ||
|
|
||
| self._child_conn.send(payload) | ||
| self._child_conn.close() | ||
|
|
||
| def run(self, mp_fork_context: multiprocessing.context.BaseContext | None): | ||
| process_factory = getattr(mp_fork_context, "Process", None) | ||
|
|
||
| if not mp_fork_context or not process_factory: | ||
| pytest.skip("fork context not available") | ||
|
|
||
| self._parent_conn, self._child_conn = mp_fork_context.Pipe(duplex=False) | ||
| # start fork | ||
| child_process = process_factory(target=self.entry, args=(), daemon=False) | ||
| child_process.start() | ||
| # close parent's handle on child_conn | ||
| self._child_conn.close() | ||
|
|
||
| # get run info from pipe | ||
| payload = self._parent_conn.recv() | ||
| self._parent_conn.close() | ||
|
|
||
| assert payload == self._expected |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.