summaryrefslogtreecommitdiff
path: root/buildscripts/resmokelib/sighandler.py
blob: 082deea763032eb6ce6d586dd8ea52ef368a47c2 (plain)
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
"""
Utility to support asynchronously signaling the current process.
"""

from __future__ import absolute_import

import signal
import sys
import traceback

from . import reportfile


def register(logger, suites):
    """
    Registers a signal handler for the SIGUSR1 signal.
    """

    def _handle_sigusr1(signum, frame):
        """
        Signal handler that will dump the stacks of all threads and
        then write out the report file.
        """

        _dump_stacks(logger)
        reportfile.write(suites)

    try:
        signal.signal(signal.SIGUSR1, _handle_sigusr1)
    except AttributeError:
        logger.warn("Cannot catch signals on Windows")


def _dump_stacks(logger):
    """
    Signal handler that will dump the stacks of all threads.
    """

    header_msg = "Dumping stacks due to SIGUSR1 signal"

    sb = []
    sb.append(header_msg)

    frames = sys._current_frames()
    sb.append("Total threads: %d" % (len(frames)))
    sb.append("")

    for thread_id in frames:
        stack = frames[thread_id]
        sb.append("Thread %d:" % (thread_id))
        sb.append("".join(traceback.format_stack(stack)))

    logger.info("\n".join(sb))