-
Notifications
You must be signed in to change notification settings - Fork 74
[#722] fix segfault and hung threads on KeyboardIinterrupt during parallel get #728
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
Open
d-w-moore
wants to merge
25
commits into
irods:main
Choose a base branch
from
d-w-moore:segfault_parallel_io_722.m
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
a3dd06e
[_722] fix segfault and hung threads on SIGINT during parallel get
d-w-moore 481952c
use subtest.
d-w-moore 1213d2b
try to preserve latest synchronous parallel put/get for orderly shutd…
d-w-moore 59fd131
can now abort parallel transfers with SIGINT/^C or SIGTERM
d-w-moore ba0407a
[_722] update readme for signals and parallel put/get
d-w-moore dddcc95
prevent auto_close
d-w-moore b96f805
satisfy static typing.
d-w-moore ed25eda
revise README
d-w-moore 9d2ff7a
forward ref needed for mypy?
d-w-moore 0aaf747
patch test
d-w-moore ade42ea
more informative error message when retcodes do not match
d-w-moore 8272b5a
delete unnecessary "import irods"
d-w-moore 7584b71
Update README.md
d-w-moore 368e08e
add a finite timeout
d-w-moore 0765f71
review comments
d-w-moore 9ec506b
comments regarding futures returning None
d-w-moore 1b42f97
test condition wait is ten minutes is the default, no need to specify…
d-w-moore 1740e80
catch was a no-op
d-w-moore c5824cc
remove TODO's
d-w-moore 92474be
[_722] test a data put is sanely interruptable
d-w-moore df05ce1
[squashed multiple commits] tighten up all the quit logic:
d-w-moore 14037f9
[another_squash] tidy, fix, add put test
d-w-moore 107ef8d
add tools.py with shared functions.
d-w-moore f7b5a73
make doc string more thorough, for abort_parallel_transfers().
d-w-moore d242b90
codacy, review
d-w-moore 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
Some comments aren't visible on the classic Files Changed page.
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
136 changes: 136 additions & 0 deletions
136
irods/test/modules/test_signal_handling_in_multithread_get.py
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,136 @@ | ||
| import os | ||
| import re | ||
| import signal | ||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
| import time | ||
|
|
||
| import irods.helpers | ||
| from irods.test import modules as test_modules | ||
| from irods.parallel import abort_parallel_transfers | ||
|
|
||
| OBJECT_SIZE = 2 * 1024**3 | ||
| OBJECT_NAME = "data_get_issue__722" | ||
| LOCAL_TEMPFILE_NAME = "data_object_for_issue_722.dat" | ||
|
|
||
|
|
||
| _clock_polling_interval = max(0.01, time.clock_getres(time.CLOCK_BOOTTIME)) | ||
|
|
||
|
|
||
| def wait_till_true(function, timeout=None): | ||
d-w-moore marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| start_time = time.clock_gettime_ns(time.CLOCK_BOOTTIME) | ||
| while not (truth_value := function()): | ||
| if ( | ||
| timeout is not None | ||
| and (time.clock_gettime_ns(time.CLOCK_BOOTTIME) - start_time) * 1e-9 | ||
| > timeout | ||
| ): | ||
| break | ||
| time.sleep(_clock_polling_interval) | ||
| return truth_value | ||
|
|
||
|
|
||
| def test(test_case, signal_names=("SIGTERM", "SIGINT")): | ||
| """Creates a child process executing a long get() and ensures the process can be | ||
| terminated using SIGINT or SIGTERM. | ||
| """ | ||
| program = os.path.join(test_modules.__path__[0], os.path.basename(__file__)) | ||
|
|
||
| for signal_name in signal_names: | ||
|
|
||
| with test_case.subTest(f"Testing with signal {signal_name}"): | ||
|
|
||
| # Call into this same module as a command. This will initiate another Python process that | ||
| # performs a lengthy data object "get" operation (see the main body of the script, below.) | ||
| process = subprocess.Popen( | ||
| [sys.executable, program], | ||
| stderr=subprocess.PIPE, | ||
| stdout=subprocess.PIPE, | ||
| text=True, | ||
| ) | ||
|
|
||
| # Wait for download process to reach the point of spawning data transfer threads. In Python 3.9+ versions | ||
| # of the concurrent.futures module, these are nondaemon threads and will block the exit of the main thread | ||
| # unless measures are taken (#722). | ||
| localfile = process.stdout.readline().strip() | ||
| test_case.assertTrue( | ||
| wait_till_true( | ||
| lambda: os.path.exists(localfile) | ||
| and os.stat(localfile).st_size > OBJECT_SIZE // 2 | ||
| ), | ||
| "Parallel download from data_objects.get() probably experienced a fatal error before spawning auxiliary data transfer threads.", | ||
| ) | ||
|
|
||
| sig = getattr(signal, signal_name) | ||
|
|
||
| signal_offset_return_code = lambda s: 128 - s if s < 0 else s | ||
| signal_plus_128 = lambda sig: 128 + sig | ||
|
|
||
| # Interrupt the subprocess with the given signal. | ||
| process.send_signal(sig) | ||
|
|
||
| # Assert that this signal is what killed the subprocess, rather than a timed out process "wait" or a natural exit | ||
| # due to misproper or incomplete handling of the signal. | ||
| try: | ||
| translated_return_code = signal_offset_return_code(process.wait(timeout=15)) | ||
| test_case.assertEqual( | ||
| translated_return_code, | ||
| signal_plus_128(sig), | ||
| f"Expected subprocess return code of {signal_plus_128(sig) = }; got {translated_return_code = }", | ||
| ) | ||
| except subprocess.TimeoutExpired as timeout_exc: | ||
| test_case.fail( | ||
| f"Subprocess timed out before terminating. " | ||
| "Non-daemon thread(s) probably prevented subprocess's main thread from exiting." | ||
| ) | ||
| # Assert that in the case of SIGINT, the process registered a KeyboardInterrupt. | ||
| if sig == signal.SIGINT: | ||
| test_case.assertTrue( | ||
| re.search("KeyboardInterrupt", process.stderr.read()), | ||
| "Did not find expected string 'KeyboardInterrupt' in log output.", | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| # These lines are run only if the module is launched as a process. | ||
| session = irods.helpers.make_session() | ||
| hc = irods.helpers.home_collection(session) | ||
| TESTFILE_FILL = b"_" * (1024 * 1024) | ||
| object_path = f"{hc}/{OBJECT_NAME}" | ||
|
|
||
| # Create the object to be downloaded. | ||
| with session.data_objects.open(object_path, "w") as f: | ||
| for y in range(OBJECT_SIZE // len(TESTFILE_FILL)): | ||
| f.write(TESTFILE_FILL) | ||
| local_path = None | ||
| # Establish where (ie absolute path) to place the downloaded file, i.e. the get() target. | ||
| try: | ||
| with tempfile.NamedTemporaryFile( | ||
| prefix="local_file_issue_722.dat", delete=True | ||
| ) as t: | ||
| local_path = t.name | ||
|
|
||
| # Tell the parent process the name of the local file being "get"ted (got) from iRODS | ||
| print(local_path) | ||
| sys.stdout.flush() | ||
|
Comment on lines
+101
to
+103
Contributor
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 appears to be used as signal in the test on line 70 that the transfer threads have spawned. Is that understanding correct? If so, let's explicitly mention that in the comment as well because that seems really important. |
||
|
|
||
| def handler(sig,*_): | ||
| abort_parallel_transfers() | ||
| exit(128+sig) | ||
alanking marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| signal.signal(signal.SIGTERM, handler) | ||
d-w-moore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| try: | ||
| # download the object | ||
| session.data_objects.get(object_path, local_path) | ||
| except KeyboardInterrupt: | ||
| abort_parallel_transfers() | ||
| raise | ||
|
|
||
| finally: | ||
| # Clean up, whether or not the download succeeded. | ||
| if local_path is not None and os.path.exists(local_path): | ||
| os.unlink(local_path) | ||
| if session.data_objects.exists(object_path): | ||
| session.data_objects.unlink(object_path, force=True) | ||
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.