xref: /llvm-project/lldb/packages/Python/lldbsuite/test/test_runner/process_control.py (revision 52bd8012bd0f440e21ddec0c1d6ed89605adbb3b)
1"""
2The LLVM Compiler Infrastructure
3
4This file is distributed under the University of Illinois Open Source
5License. See LICENSE.TXT for details.
6
7Provides classes used by the test results reporting infrastructure
8within the LLDB test suite.
9
10
11This module provides process-management support for the LLDB test
12running infrasructure.
13"""
14
15# System imports
16import os
17import re
18import signal
19import subprocess
20import sys
21import threading
22
23
24class CommunicatorThread(threading.Thread):
25    """Provides a thread class that communicates with a subprocess."""
26    def __init__(self, process, event, output_file):
27        super(CommunicatorThread, self).__init__()
28        # Don't let this thread prevent shutdown.
29        self.daemon = True
30        self.process = process
31        self.pid = process.pid
32        self.event = event
33        self.output_file = output_file
34        self.output = None
35
36    def run(self):
37        try:
38            # Communicate with the child process.
39            # This will not complete until the child process terminates.
40            self.output = self.process.communicate()
41        except Exception as exception:  # pylint: disable=broad-except
42            if self.output_file:
43                self.output_file.write(
44                    "exception while using communicate() for pid: {}\n".format(
45                        exception))
46        finally:
47            # Signal that the thread's run is complete.
48            self.event.set()
49
50
51# Provides a regular expression for matching gtimeout-based durations.
52TIMEOUT_REGEX = re.compile(r"(^\d+)([smhd])?$")
53
54
55def timeout_to_seconds(timeout):
56    """Converts timeout/gtimeout timeout values into seconds.
57
58    @param timeout a timeout in the form of xm representing x minutes.
59
60    @return None if timeout is None, or the number of seconds as a float
61    if a valid timeout format was specified.
62    """
63    if timeout is None:
64        return None
65    else:
66        match = TIMEOUT_REGEX.match(timeout)
67        if match:
68            value = float(match.group(1))
69            units = match.group(2)
70            if units is None:
71                # default is seconds.  No conversion necessary.
72                return value
73            elif units == 's':
74                # Seconds.  No conversion necessary.
75                return value
76            elif units == 'm':
77                # Value is in minutes.
78                return 60.0 * value
79            elif units == 'h':
80                # Value is in hours.
81                return (60.0 * 60.0) * value
82            elif units == 'd':
83                # Value is in days.
84                return 24 * (60.0 * 60.0) * value
85            else:
86                raise Exception("unexpected units value '{}'".format(units))
87        else:
88            raise Exception("could not parse TIMEOUT spec '{}'".format(
89                timeout))
90
91
92class ProcessHelper(object):
93    """Provides an interface for accessing process-related functionality.
94
95    This class provides a factory method that gives the caller a
96    platform-specific implementation instance of the class.
97
98    Clients of the class should stick to the methods provided in this
99    base class.
100
101    @see ProcessHelper.process_helper()
102    """
103    def __init__(self):
104        super(ProcessHelper, self).__init__()
105
106    @classmethod
107    def process_helper(cls):
108        """Returns a platform-specific ProcessHelper instance.
109        @return a ProcessHelper instance that does the right thing for
110        the current platform.
111        """
112
113        # If you add a new platform, create an instance here and
114        # return it.
115        if os.name == "nt":
116            return WindowsProcessHelper()
117        else:
118            # For all POSIX-like systems.
119            return UnixProcessHelper()
120
121    def create_piped_process(self, command, new_process_group=True):
122        # pylint: disable=no-self-use,unused-argument
123        # As expected.  We want derived classes to implement this.
124        """Creates a subprocess.Popen-based class with I/O piped to the parent.
125
126        @param command the command line list as would be passed to
127        subprocess.Popen().  Use the list form rather than the string form.
128
129        @param new_process_group indicates if the caller wants the
130        process to be created in its own process group.  Each OS handles
131        this concept differently.  It provides a level of isolation and
132        can simplify or enable terminating the process tree properly.
133
134        @return a subprocess.Popen-like object.
135        """
136        raise Exception("derived class must implement")
137
138    def supports_soft_terminate(self):
139        # pylint: disable=no-self-use
140        # As expected.  We want derived classes to implement this.
141        """Indicates if the platform supports soft termination.
142
143        Soft termination is the concept of a terminate mechanism that
144        allows the target process to shut down nicely, but with the
145        catch that the process might choose to ignore it.
146
147        Platform supporter note: only mark soft terminate as supported
148        if the target process has some way to evade the soft terminate
149        request; otherwise, just support the hard terminate method.
150
151        @return True if the platform supports a soft terminate mechanism.
152        """
153        # By default, we do not support a soft terminate mechanism.
154        return False
155
156    def soft_terminate(self, popen_process, log_file=None, want_core=True):
157        # pylint: disable=no-self-use,unused-argument
158        # As expected.  We want derived classes to implement this.
159        """Attempts to terminate the process in a polite way.
160
161        This terminate method is intended to give the child process a
162        chance to clean up and exit on its own, possibly with a request
163        to drop a core file or equivalent (i.e. [mini-]crashdump, crashlog,
164        etc.)  If new_process_group was set in the process creation method
165        and the platform supports it, this terminate call will attempt to
166        kill the whole process tree rooted in this child process.
167
168        @param popen_process the subprocess.Popen-like object returned
169        by one of the process-creation methods of this class.
170
171        @param log_file file-like object used to emit error-related
172        logging info.  May be None if no error-related info is desired.
173
174        @param want_core True if the caller would like to get a core
175        dump (or the analogous crash report) from the terminated process.
176        """
177        popen_process.terminate()
178
179    def hard_terminate(self, popen_process, log_file=None):
180        # pylint: disable=no-self-use,unused-argument
181        # As expected.  We want derived classes to implement this.
182        """Attempts to terminate the process immediately.
183
184        This terminate method is intended to kill child process in
185        a manner in which the child process has no ability to block,
186        and also has no ability to clean up properly.  If new_process_group
187        was specified when creating the process, and if the platform
188        implementation supports it, this will attempt to kill the
189        whole process tree rooted in the child process.
190
191        @param popen_process the subprocess.Popen-like object returned
192        by one of the process-creation methods of this class.
193
194        @param log_file file-like object used to emit error-related
195        logging info.  May be None if no error-related info is desired.
196        """
197        popen_process.kill()
198
199    def was_soft_terminate(self, returncode, with_core):
200        # pylint: disable=no-self-use,unused-argument
201        # As expected.  We want derived classes to implement this.
202        """Returns if Popen-like object returncode matches soft terminate.
203
204        @param returncode the returncode from the Popen-like object that
205        terminated with a given return code.
206
207        @param with_core indicates whether the returncode should match
208        a core-generating return signal.
209
210        @return True when the returncode represents what the system would
211        issue when a soft_terminate() with the given with_core arg occurred;
212        False otherwise.
213        """
214        if not self.supports_soft_terminate():
215            # If we don't support soft termination on this platform,
216            # then this should always be False.
217            return False
218        else:
219            # Once a platform claims to support soft terminate, it
220            # needs to be able to identify it by overriding this method.
221            raise Exception("platform needs to implement")
222
223    def was_hard_terminate(self, returncode):
224        # pylint: disable=no-self-use,unused-argument
225        # As expected.  We want derived classes to implement this.
226        """Returns if Popen-like object returncode matches that of a hard
227        terminate attempt.
228
229        @param returncode the returncode from the Popen-like object that
230        terminated with a given return code.
231
232        @return True when the returncode represents what the system would
233        issue when a hard_terminate() occurred; False
234        otherwise.
235        """
236        raise Exception("platform needs to implement")
237
238    def soft_terminate_signals(self):
239        # pylint: disable=no-self-use
240        """Retrieve signal numbers that can be sent to soft terminate.
241        @return a list of signal numbers that can be sent to soft terminate
242        a process, or None if not applicable.
243        """
244        return None
245
246    def is_exceptional_exit(self, popen_status):
247        """Returns whether the program exit status is exceptional.
248
249        Returns whether the return code from a Popen process is exceptional.
250
251        @return True if the given popen_status represents an exceptional
252        program exit; False otherwise.
253        """
254        return popen_status != 0
255
256    def exceptional_exit_details(self, popen_status):
257        """Returns the normalized exceptional exit code and a description.
258
259        Given an exceptional exit code, returns the integral value of the
260        exception and a description for the result.
261
262        Derived classes can override this if they want to want custom
263        exceptional exit code handling.
264
265        @return (normalized exception code, symbolic exception description)
266        """
267        return (popen_status, "exit")
268
269
270class UnixProcessHelper(ProcessHelper):
271    """Provides a ProcessHelper for Unix-like operating systems.
272
273    This implementation supports anything that looks Posix-y
274    (e.g. Darwin, Linux, *BSD, etc.)
275    """
276    def __init__(self):
277        super(UnixProcessHelper, self).__init__()
278
279    @classmethod
280    def _create_new_process_group(cls):
281        """Creates a new process group for the calling process."""
282        os.setpgid(os.getpid(), os.getpid())
283
284    def create_piped_process(self, command, new_process_group=True):
285        # Determine what to run after the fork but before the exec.
286        if new_process_group:
287            preexec_func = self._create_new_process_group
288        else:
289            preexec_func = None
290
291        # Create the process.
292        process = subprocess.Popen(
293            command,
294            stdin=subprocess.PIPE,
295            stdout=subprocess.PIPE,
296            stderr=subprocess.PIPE,
297            universal_newlines=True, # Elicits automatic byte -> string decoding in Py3
298            close_fds=True,
299            preexec_fn=preexec_func)
300
301        # Remember whether we're using process groups for this
302        # process.
303        process.using_process_groups = new_process_group
304        return process
305
306    def supports_soft_terminate(self):
307        # POSIX does support a soft terminate via:
308        # * SIGTERM (no core requested)
309        # * SIGQUIT (core requested if enabled, see ulimit -c)
310        return True
311
312    @classmethod
313    def _validate_pre_terminate(cls, popen_process, log_file):
314        # Validate args.
315        if popen_process is None:
316            raise ValueError("popen_process is None")
317
318        # Ensure we have something that looks like a valid process.
319        if popen_process.pid < 1:
320            if log_file:
321                log_file.write("skipping soft_terminate(): no process id")
322            return False
323
324        # We only do the process liveness check if we're not using
325        # process groups.  With process groups, checking if the main
326        # inferior process is dead and short circuiting here is no
327        # good - children of it in the process group could still be
328        # alive, and they should be killed during a timeout.
329        if not popen_process.using_process_groups:
330            # Don't kill if it's already dead.
331            popen_process.poll()
332            if popen_process.returncode is not None:
333                # It has a returncode.  It has already stopped.
334                if log_file:
335                    log_file.write(
336                        "requested to terminate pid {} but it has already "
337                        "terminated, returncode {}".format(
338                            popen_process.pid, popen_process.returncode))
339                # Move along...
340                return False
341
342        # Good to go.
343        return True
344
345    def _kill_with_signal(self, popen_process, log_file, signum):
346        # Validate we're ready to terminate this.
347        if not self._validate_pre_terminate(popen_process, log_file):
348            return
349
350        # Choose kill mechanism based on whether we're targeting
351        # a process group or just a process.
352        if popen_process.using_process_groups:
353            # if log_file:
354            #    log_file.write(
355            #        "sending signum {} to process group {} now\n".format(
356            #            signum, popen_process.pid))
357            os.killpg(popen_process.pid, signum)
358        else:
359            # if log_file:
360            #    log_file.write(
361            #        "sending signum {} to process {} now\n".format(
362            #            signum, popen_process.pid))
363            os.kill(popen_process.pid, signum)
364
365    def soft_terminate(self, popen_process, log_file=None, want_core=True):
366        # Choose signal based on desire for core file.
367        if want_core:
368            # SIGQUIT will generate core by default.  Can be caught.
369            signum = signal.SIGQUIT
370        else:
371            # SIGTERM is the traditional nice way to kill a process.
372            # Can be caught, doesn't generate a core.
373            signum = signal.SIGTERM
374
375        self._kill_with_signal(popen_process, log_file, signum)
376
377    def hard_terminate(self, popen_process, log_file=None):
378        self._kill_with_signal(popen_process, log_file, signal.SIGKILL)
379
380    def was_soft_terminate(self, returncode, with_core):
381        if with_core:
382            return returncode == -signal.SIGQUIT
383        else:
384            return returncode == -signal.SIGTERM
385
386    def was_hard_terminate(self, returncode):
387        return returncode == -signal.SIGKILL
388
389    def soft_terminate_signals(self):
390        return [signal.SIGQUIT, signal.SIGTERM]
391
392    @classmethod
393    def _signal_names_by_number(cls):
394        return dict(
395            (k, v) for v, k in reversed(sorted(signal.__dict__.items()))
396            if v.startswith('SIG') and not v.startswith('SIG_'))
397
398    def exceptional_exit_details(self, popen_status):
399        if popen_status >= 0:
400            return (popen_status, "exit")
401        signo = -popen_status
402        signal_names_by_number = self._signal_names_by_number()
403        signal_name = signal_names_by_number.get(signo, "")
404        return (signo, signal_name)
405
406class WindowsProcessHelper(ProcessHelper):
407    """Provides a Windows implementation of the ProcessHelper class."""
408    def __init__(self):
409        super(WindowsProcessHelper, self).__init__()
410
411    def create_piped_process(self, command, new_process_group=True):
412        if new_process_group:
413            # We need this flag if we want os.kill() to work on the subprocess.
414            creation_flags = subprocess.CREATE_NEW_PROCESS_GROUP
415        else:
416            creation_flags = 0
417
418        return subprocess.Popen(
419            command,
420            stdin=subprocess.PIPE,
421            stdout=subprocess.PIPE,
422            stderr=subprocess.PIPE,
423            universal_newlines=True, # Elicits automatic byte -> string decoding in Py3
424            creationflags=creation_flags)
425
426    def was_hard_terminate(self, returncode):
427        return returncode != 0
428
429
430class ProcessDriver(object):
431    """Drives a child process, notifies on important events, and can timeout.
432
433    Clients are expected to derive from this class and override the
434    on_process_started and on_process_exited methods if they want to
435    hook either of those.
436
437    This class supports timing out the child process in a platform-agnostic
438    way.  The on_process_exited method is informed if the exit was natural
439    or if it was due to a timeout.
440    """
441    def __init__(self, soft_terminate_timeout=10.0):
442        super(ProcessDriver, self).__init__()
443        self.process_helper = ProcessHelper.process_helper()
444        self.pid = None
445        # Create the synchronization event for notifying when the
446        # inferior dotest process is complete.
447        self.done_event = threading.Event()
448        self.io_thread = None
449        self.process = None
450        # Number of seconds to wait for the soft terminate to
451        # wrap up, before moving to more drastic measures.
452        # Might want this longer if core dumps are generated and
453        # take a long time to write out.
454        self.soft_terminate_timeout = soft_terminate_timeout
455        # Number of seconds to wait for the hard terminate to
456        # wrap up, before giving up on the io thread.  This should
457        # be fast.
458        self.hard_terminate_timeout = 5.0
459        self.returncode = None
460
461    # =============================================
462    # Methods for subclasses to override if desired.
463    # =============================================
464
465    def on_process_started(self):
466        pass
467
468    def on_process_exited(self, command, output, was_timeout, exit_status):
469        pass
470
471    def write(self, content):
472        # pylint: disable=no-self-use
473        # Intended - we want derived classes to be able to override
474        # this and use any self state they may contain.
475        sys.stdout.write(content)
476
477    # ==============================================================
478    # Operations used to drive processes.  Clients will want to call
479    # one of these.
480    # ==============================================================
481
482    def run_command(self, command):
483        # Start up the child process and the thread that does the
484        # communication pump.
485        self._start_process_and_io_thread(command)
486
487        # Wait indefinitely for the child process to finish
488        # communicating.  This indicates it has closed stdout/stderr
489        # pipes and is done.
490        self.io_thread.join()
491        self.returncode = self.process.wait()
492        if self.returncode is None:
493            raise Exception(
494                "no exit status available for pid {} after the "
495                " inferior dotest.py should have completed".format(
496                    self.process.pid))
497
498        # Notify of non-timeout exit.
499        self.on_process_exited(
500            command,
501            self.io_thread.output,
502            False,
503            self.returncode)
504
505    def run_command_with_timeout(self, command, timeout, want_core):
506        # Figure out how many seconds our timeout description is requesting.
507        timeout_seconds = timeout_to_seconds(timeout)
508
509        # Start up the child process and the thread that does the
510        # communication pump.
511        self._start_process_and_io_thread(command)
512
513        self._wait_with_timeout(timeout_seconds, command, want_core)
514
515    # ================
516    # Internal details.
517    # ================
518
519    def _start_process_and_io_thread(self, command):
520        # Create the process.
521        self.process = self.process_helper.create_piped_process(command)
522        self.pid = self.process.pid
523        self.on_process_started()
524
525        # Ensure the event is cleared that is used for signaling
526        # from the communication() thread when communication is
527        # complete (i.e. the inferior process has finished).
528        self.done_event.clear()
529
530        self.io_thread = CommunicatorThread(
531            self.process, self.done_event, self.write)
532        self.io_thread.start()
533
534    def _attempt_soft_kill(self, want_core):
535        # The inferior dotest timed out.  Attempt to clean it
536        # with a non-drastic method (so it can clean up properly
537        # and/or generate a core dump).  Often the OS can't guarantee
538        # that the process will really terminate after this.
539        self.process_helper.soft_terminate(
540            self.process,
541            want_core=want_core,
542            log_file=self)
543
544        # Now wait up to a certain timeout period for the io thread
545        # to say that the communication ended.  If that wraps up
546        # within our soft terminate timeout, we're all done here.
547        self.io_thread.join(self.soft_terminate_timeout)
548        if not self.io_thread.is_alive():
549            # stdout/stderr were closed on the child process side. We
550            # should be able to wait and reap the child process here.
551            self.returncode = self.process.wait()
552            # We terminated, and the done_trying result is n/a
553            terminated = True
554            done_trying = None
555        else:
556            self.write("soft kill attempt of process {} timed out "
557                       "after {} seconds\n".format(
558                           self.process.pid, self.soft_terminate_timeout))
559            terminated = False
560            done_trying = False
561        return terminated, done_trying
562
563    def _attempt_hard_kill(self):
564        # Instruct the process to terminate and really force it to
565        # happen.  Don't give the process a chance to ignore.
566        self.process_helper.hard_terminate(
567            self.process,
568            log_file=self)
569
570        # Reap the child process.  This should not hang as the
571        # hard_kill() mechanism is supposed to really kill it.
572        # Improvement option:
573        # If this does ever hang, convert to a self.process.poll()
574        # loop checking on self.process.returncode until it is not
575        # None or the timeout occurs.
576        self.returncode = self.process.wait()
577
578        # Wait a few moments for the io thread to finish...
579        self.io_thread.join(self.hard_terminate_timeout)
580        if self.io_thread.is_alive():
581            # ... but this is not critical if it doesn't end for some
582            # reason.
583            self.write(
584                "hard kill of process {} timed out after {} seconds waiting "
585                "for the io thread (ignoring)\n".format(
586                    self.process.pid, self.hard_terminate_timeout))
587
588        # Set if it terminated.  (Set up for optional improvement above).
589        terminated = self.returncode is not None
590        # Nothing else to try.
591        done_trying = True
592
593        return terminated, done_trying
594
595    def _attempt_termination(self, attempt_count, want_core):
596        if self.process_helper.supports_soft_terminate():
597            # When soft termination is supported, we first try to stop
598            # the process with a soft terminate.  Failing that, we try
599            # the hard terminate option.
600            if attempt_count == 1:
601                return self._attempt_soft_kill(want_core)
602            elif attempt_count == 2:
603                return self._attempt_hard_kill()
604            else:
605                # We don't have anything else to try.
606                terminated = self.returncode is not None
607                done_trying = True
608                return terminated, done_trying
609        else:
610            # We only try the hard terminate option when there
611            # is no soft terminate available.
612            if attempt_count == 1:
613                return self._attempt_hard_kill()
614            else:
615                # We don't have anything else to try.
616                terminated = self.returncode is not None
617                done_trying = True
618                return terminated, done_trying
619
620    def _wait_with_timeout(self, timeout_seconds, command, want_core):
621        # Allow up to timeout seconds for the io thread to wrap up.
622        # If that completes, the child process should be done.
623        completed_normally = self.done_event.wait(timeout_seconds)
624        if completed_normally:
625            # Reap the child process here.
626            self.returncode = self.process.wait()
627        else:
628            # Prepare to stop the process
629            process_terminated = completed_normally
630            terminate_attempt_count = 0
631
632            # Try as many attempts as we support for trying to shut down
633            # the child process if it's not already shut down.
634            while not process_terminated:
635                terminate_attempt_count += 1
636                # Attempt to terminate.
637                process_terminated, done_trying = self._attempt_termination(
638                    terminate_attempt_count, want_core)
639                # Check if there's nothing more to try.
640                if done_trying:
641                    # Break out of our termination attempt loop.
642                    break
643
644        # At this point, we're calling it good.  The process
645        # finished gracefully, was shut down after one or more
646        # attempts, or we failed but gave it our best effort.
647        self.on_process_exited(
648            command,
649            self.io_thread.output,
650            not completed_normally,
651            self.returncode)
652
653
654def patched_init(self, *args, **kwargs):
655    self.original_init(*args, **kwargs)
656    # Initialize our condition variable that protects wait()/poll().
657    self.wait_condition = threading.Condition()
658
659
660def patched_wait(self, *args, **kwargs):
661    self.wait_condition.acquire()
662    try:
663        result = self.original_wait(*args, **kwargs)
664        # The process finished.  Signal the condition.
665        self.wait_condition.notify_all()
666        return result
667    finally:
668        self.wait_condition.release()
669
670
671def patched_poll(self, *args, **kwargs):
672    self.wait_condition.acquire()
673    try:
674        result = self.original_poll(*args, **kwargs)
675        if self.returncode is not None:
676            # We did complete, and we have the return value.
677            # Signal the event to indicate we're done.
678            self.wait_condition.notify_all()
679        return result
680    finally:
681        self.wait_condition.release()
682
683
684def patch_up_subprocess_popen():
685    subprocess.Popen.original_init = subprocess.Popen.__init__
686    subprocess.Popen.__init__ = patched_init
687
688    subprocess.Popen.original_wait = subprocess.Popen.wait
689    subprocess.Popen.wait = patched_wait
690
691    subprocess.Popen.original_poll = subprocess.Popen.poll
692    subprocess.Popen.poll = patched_poll
693
694# Replace key subprocess.Popen() threading-unprotected methods with
695# threading-protected versions.
696patch_up_subprocess_popen()
697