-
Notifications
You must be signed in to change notification settings - Fork 82
Kill forked submission processes after execution #451
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
Closed
Sakshamm-Goyal
wants to merge
2
commits into
Kattis:master
from
Sakshamm-Goyal:fix/kill-forked-tle-children
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import os | ||
| import resource | ||
| import signal | ||
| import textwrap | ||
| import time | ||
|
|
||
| from problemtools.run.executable import Executable | ||
| from problemtools.run import program | ||
|
|
||
|
|
||
| def _process_exists(pid: int) -> bool: | ||
| try: | ||
| os.kill(pid, 0) | ||
| except ProcessLookupError: | ||
| return False | ||
| return True | ||
|
|
||
|
|
||
| def test_run_kills_forked_children_after_time_limit(tmp_path, monkeypatch): | ||
| # macOS rejects raising the child stack soft limit to its finite hard | ||
| # limit. Keep the CPU limit real; bypass only this unrelated platform | ||
| # limitation so the test exercises the process-group cleanup itself. | ||
| original_try_limit = program.limit.try_limit | ||
|
|
||
| def try_limit(limit, soft, hard): | ||
| if limit == resource.RLIMIT_STACK: | ||
| return | ||
| return original_try_limit(limit, soft, hard) | ||
|
|
||
| monkeypatch.setattr(program.limit, 'try_limit', try_limit) | ||
| child_pid_file = tmp_path / 'child.pid' | ||
| child_pid_tmp_file = tmp_path / 'child.pid.tmp' | ||
| program_path = tmp_path / 'forking-timeout.py' | ||
| program_path.write_text( | ||
| textwrap.dedent( | ||
| f"""\ | ||
| #!/usr/bin/env python3 | ||
| import os | ||
| import signal | ||
| import time | ||
|
|
||
| child_pid = os.fork() | ||
| if child_pid == 0: | ||
| with open({str(child_pid_tmp_file)!r}, 'w') as child_pid_output: | ||
| child_pid_output.write(str(os.getpid())) | ||
| os.replace({str(child_pid_tmp_file)!r}, {str(child_pid_file)!r}) | ||
| while True: | ||
| time.sleep(60) | ||
|
|
||
| while not os.path.exists({str(child_pid_file)!r}): | ||
| time.sleep(0.01) | ||
|
Sakshamm-Goyal marked this conversation as resolved.
|
||
| os.kill(os.getpid(), signal.SIGXCPU) | ||
|
Sakshamm-Goyal marked this conversation as resolved.
|
||
| """ | ||
| ) | ||
| ) | ||
| program_path.chmod(0o755) | ||
|
|
||
| status, _ = Executable(str(program_path)).run(timelim=1, work_dir=str(tmp_path)) | ||
|
|
||
| child_pid = int(child_pid_file.read_text()) | ||
| try: | ||
| assert os.WIFSIGNALED(status) | ||
| assert os.WTERMSIG(status) == signal.SIGXCPU | ||
| for _ in range(200): | ||
| if not _process_exists(child_pid): | ||
| break | ||
| time.sleep(0.01) | ||
| assert not _process_exists(child_pid) | ||
| finally: | ||
| if _process_exists(child_pid): | ||
| os.kill(child_pid, signal.SIGKILL) | ||
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.