File: debug.rst

package info (click to toggle)
python-pykka 4.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 520 kB
  • sloc: python: 2,817; makefile: 113
file content (89 lines) | stat: -rw-r--r-- 3,752 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
=============
Debug helpers
=============

.. automodule:: pykka.debug
    :members:


Deadlock debugging
==================

This is a complete example of how to use
:func:`log_thread_tracebacks` to debug deadlocks:

.. literalinclude:: ../../examples/deadlock_debugging.py

Running the script outputs the following::

    Setting up logging to get output from signal handler...
    Registering signal handler...
    Starting actors...
    DEBUG:pykka:Registered DeadlockActorA (urn:uuid:60803d09-cf5a-46cc-afdc-0c813e2e6647)
    DEBUG:pykka:Starting DeadlockActorA (urn:uuid:60803d09-cf5a-46cc-afdc-0c813e2e6647)
    DEBUG:pykka:Registered DeadlockActorB (urn:uuid:626adc83-ae35-439c-866a-85a3e29fd42c)
    DEBUG:pykka:Starting DeadlockActorB (urn:uuid:626adc83-ae35-439c-866a-85a3e29fd42c)
    Now doing something stupid that will deadlock the actors...
    DEBUG:root:This is foo calling bar
    DEBUG:root:This is bar calling foo; BOOM!
    Making main thread relax; not block, not quit
    1) Use `kill -SIGUSR1 2284` to log thread tracebacks
    2) Then `kill 2284` to terminate the process

The two actors are now deadlocked waiting for each other while the main
thread is idling, ready to process any signals.

To debug the deadlock, send the ``SIGUSR1`` signal to the process, which has
PID 2284 in this example::

    kill -SIGUSR1 2284

This makes the main thread log the current traceback for each thread.
The logging output shows that the two actors are both waiting for data from the
other actor::

    CRITICAL:pykka:Current state of DeadlockActorB-2 (ident: 140151493752576):
    File "/usr/lib/python3.6/threading.py", line 884, in _bootstrap
        self._bootstrap_inner()
    File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
        self.run()
    File "/usr/lib/python3.6/threading.py", line 864, in run
        self._target(*self._args, **self._kwargs)
    File ".../pykka/actor.py", line 195, in _actor_loop
        response = self._handle_receive(message)
    File ".../pykka/actor.py", line 297, in _handle_receive
        return callee(*message['args'], **message['kwargs'])
    File "examples/deadlock_debugging.py", line 25, in bar
        return self.a.foo().get()
    File ".../pykka/threading.py", line 47, in get
        self._data = self._queue.get(True, timeout)
    File "/usr/lib/python3.6/queue.py", line 164, in get
        self.not_empty.wait()
    File "/usr/lib/python3.6/threading.py", line 295, in wait
        waiter.acquire()

    CRITICAL:pykka:Current state of DeadlockActorA-1 (ident: 140151572883200):
    File "/usr/lib/python3.6/threading.py", line 884, in _bootstrap
        self._bootstrap_inner()
    File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
        self.run()
    File "/usr/lib/python3.6/threading.py", line 864, in run
        self._target(*self._args, **self._kwargs)
    File ".../pykka/actor.py", line 195, in _actor_loop
        response = self._handle_receive(message)
    File ".../pykka/actor.py", line 297, in _handle_receive
        return callee(*message['args'], **message['kwargs'])
    File "examples/deadlock_debugging.py", line 15, in foo
        return b.bar().get()
    File ".../pykka/threading.py", line 47, in get
        self._data = self._queue.get(True, timeout)
    File "/usr/lib/python3.6/queue.py", line 164, in get
        self.not_empty.wait()
    File "/usr/lib/python3.6/threading.py", line 295, in wait
        waiter.acquire()

    CRITICAL:pykka:Current state of MainThread (ident: 140151593330496):
    File ".../examples/deadlock_debugging.py", line 49, in <module>
        time.sleep(1)
    File ".../pykka/debug.py", line 63, in log_thread_tracebacks
        stack = ''.join(traceback.format_stack(frame))