source: trunk/src/allmydata/test/blocking.py

Last change on this file was 72041c0, checked in by Itamar Turner-Trauring <itamar@…>, at 2023-10-16T15:44:29Z

More reasonable defaults

  • Property mode set to 100644
File size: 1.1 KB
Line 
1import sys
2import traceback
3import signal
4import threading
5
6from twisted.internet import reactor
7
8
9def print_stacks():
10    print("Uh oh, something is blocking the event loop!")
11    current_thread = threading.get_ident()
12    for thread_id, frame in sys._current_frames().items():
13        if thread_id == current_thread:
14            traceback.print_stack(frame, limit=10)
15            break
16
17
18def catch_blocking_in_event_loop(test=None):
19    """
20    Print tracebacks if the event loop is blocked for more than a short amount
21    of time.
22    """
23    signal.signal(signal.SIGALRM, lambda *args: print_stacks())
24
25    current_scheduled = [None]
26
27    def cancel_and_rerun():
28        signal.setitimer(signal.ITIMER_REAL, 0)
29        signal.setitimer(signal.ITIMER_REAL, 0.015)
30        current_scheduled[0] = reactor.callLater(0.01, cancel_and_rerun)
31
32    cancel_and_rerun()
33
34    def cleanup():
35        signal.signal(signal.SIGALRM, signal.SIG_DFL)
36        signal.setitimer(signal.ITIMER_REAL, 0)
37        current_scheduled[0].cancel()
38
39    if test is not None:
40        test.addCleanup(cleanup)
Note: See TracBrowser for help on using the repository browser.