-
-
Notifications
You must be signed in to change notification settings - Fork 392
Add task blocked watchdog. #596
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
Closed
Changes from all commits
Commits
Show all changes
3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Add a watchdog for blocked tasks to Trio. This will automatically print a | ||
| warning (and a full traceback of all threads) if a function is blocked without | ||
| yielding for 5 seconds (by default). |
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,84 @@ | ||
| import sys | ||
|
|
||
| import threading | ||
| import traceback | ||
|
|
||
|
|
||
| class TrioWatchdog(object): | ||
| def __init__(self, timeout=5): | ||
| self._stopped = False | ||
| self._thread = None | ||
| self._notify_event = threading.Event() | ||
| self._timeout = timeout | ||
|
|
||
| self._before_counter = 0 | ||
| self._after_counter = 0 | ||
|
|
||
| def notify_alive_before(self): | ||
| """ | ||
| Notifies the watchdog that the Trio thread is alive before running | ||
| a task. | ||
| """ | ||
| self._before_counter += 1 | ||
| self._notify_event.set() | ||
|
|
||
| def notify_alive_after(self): | ||
| """ | ||
| Notifies the watchdog that the Trio thread is alive after running a | ||
| task. | ||
| """ | ||
| self._after_counter += 1 | ||
|
|
||
| def _main_loop(self): | ||
| while True: | ||
| if self._stopped: | ||
| return | ||
|
|
||
| self._notify_event.clear() | ||
| orig_starts = self._before_counter | ||
| orig_stops = self._after_counter | ||
| if orig_starts == orig_stops: | ||
| # main thread asleep; nothing to do until it wakes up | ||
| self._notify_event.wait() | ||
| if self._stopped: | ||
| return | ||
| else: | ||
| self._notify_event.wait(timeout=self._timeout) | ||
| if self._stopped: | ||
| return | ||
|
|
||
| if orig_starts == self._before_counter \ | ||
| and orig_stops == self._after_counter: | ||
| print( | ||
| "Trio Watchdog has not received any notifications in " | ||
| "5 seconds, main thread is blocked!", | ||
| file=sys.stderr | ||
| ) | ||
| # faulthandler is not very useful to us, honestly | ||
| # faulthandler.dump_traceback(all_threads=True) | ||
| print( | ||
| "Printing the traceback of all threads:", | ||
| file=sys.stderr | ||
| ) | ||
| self._print_all_threads() | ||
|
|
||
| def _print_all_threads(self): | ||
| # separated for indent reasons, damned 80 char limit | ||
| for thread in threading.enumerate(): | ||
| print( | ||
| "Thread {} (most recent call last):".format(thread.name), | ||
| file=sys.stderr | ||
| ) | ||
| # scary internal function! | ||
| traceback.print_stack(sys._current_frames()[thread.ident]) | ||
|
|
||
| def start(self): | ||
| self._thread = threading.Thread( | ||
| target=self._main_loop, name="<trio watchdog>", daemon=True | ||
| ) | ||
| self._thread.start() | ||
|
|
||
| def stop(self): | ||
| self._stopped = True | ||
|
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 should also set the event, so that the thread wakes up promptly and notices that |
||
| self._notify_event.set() | ||
| self._thread.join() | ||
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,30 @@ | ||
| import time | ||
|
|
||
| import contextlib | ||
| from io import StringIO | ||
|
|
||
| from ..tests.tutil import slow | ||
| from ... import _core | ||
| from ..._timeouts import sleep | ||
|
|
||
| MAGIC_TEXT = "Trio Watchdog has not received any notifications in 5 seconds, \ | ||
| main thread is blocked!" | ||
|
|
||
|
|
||
| @slow | ||
| def test_watchdog(): | ||
| async def _inner_test(): | ||
| target = StringIO() | ||
| with contextlib.redirect_stderr(target): | ||
| time.sleep(2) | ||
|
|
||
| assert target.getvalue().startswith(MAGIC_TEXT) | ||
|
|
||
| target = StringIO() | ||
| with contextlib.redirect_stderr(target): | ||
| await sleep(2) | ||
|
|
||
| # if pytest puts garbage in stderr this won't fail | ||
| assert not target.getvalue().startswith(MAGIC_TEXT) | ||
|
|
||
| _core.run(_inner_test, use_watchdog=True, watchdog_timeout=1) |
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.
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.
What's the advantage of doing this by hand instead of using faulthandler?
(This seems like a pretty delicate and race-prone operation, so I'm nervous about trying to get it right ourselves. E.g., what happens if a thread exits while
print_stackis walking its stack?)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.
It prints the code being ran rather than an unhelpful list of lines (backwards, too).