2 Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 See https://llvm.org/LICENSE.txt for license information.
4 SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 Provides classes used by the test results reporting infrastructure
7 within the LLDB test suite.
10 This module provides process-management support for the LLDB test
11 running infrastructure.
23 class CommunicatorThread(threading
.Thread
):
24 """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.
30 self
.process
= process
31 self
.pid
= process
.pid
33 self
.output_file
= output_file
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
43 self
.output_file
.write(
44 "exception while using communicate() for pid: {}\n".format(
49 # Signal that the thread's run is complete.
53 # Provides a regular expression for matching gtimeout-based durations.
54 TIMEOUT_REGEX
= re
.compile(r
"(^\d+)([smhd])?$")
57 def timeout_to_seconds(timeout
):
58 """Converts timeout/gtimeout timeout values into seconds.
60 @param timeout a timeout in the form of xm representing x minutes.
62 @return None if timeout is None, or the number of seconds as a float
63 if a valid timeout format was specified.
68 match
= TIMEOUT_REGEX
.match(timeout
)
70 value
= float(match
.group(1))
71 units
= match
.group(2)
73 # default is seconds. No conversion necessary.
76 # Seconds. No conversion necessary.
79 # Value is in minutes.
83 return (60.0 * 60.0) * value
86 return 24 * (60.0 * 60.0) * value
88 raise Exception("unexpected units value '{}'".format(units
))
90 raise Exception("could not parse TIMEOUT spec '{}'".format(timeout
))
93 class ProcessHelper(object):
94 """Provides an interface for accessing process-related functionality.
96 This class provides a factory method that gives the caller a
97 platform-specific implementation instance of the class.
99 Clients of the class should stick to the methods provided in this
102 \see ProcessHelper.process_helper()
106 super(ProcessHelper
, self
).__init
__()
109 def process_helper(cls
):
110 """Returns a platform-specific ProcessHelper instance.
111 @return a ProcessHelper instance that does the right thing for
112 the current platform.
115 # If you add a new platform, create an instance here and
118 return WindowsProcessHelper()
120 # For all POSIX-like systems.
121 return UnixProcessHelper()
123 def create_piped_process(self
, command
, new_process_group
=True):
124 # pylint: disable=no-self-use,unused-argument
125 # As expected. We want derived classes to implement this.
126 """Creates a subprocess.Popen-based class with I/O piped to the parent.
128 @param command the command line list as would be passed to
129 subprocess.Popen(). Use the list form rather than the string form.
131 @param new_process_group indicates if the caller wants the
132 process to be created in its own process group. Each OS handles
133 this concept differently. It provides a level of isolation and
134 can simplify or enable terminating the process tree properly.
136 @return a subprocess.Popen-like object.
138 raise Exception("derived class must implement")
140 def supports_soft_terminate(self
):
141 # pylint: disable=no-self-use
142 # As expected. We want derived classes to implement this.
143 """Indicates if the platform supports soft termination.
145 Soft termination is the concept of a terminate mechanism that
146 allows the target process to shut down nicely, but with the
147 catch that the process might choose to ignore it.
149 Platform supporter note: only mark soft terminate as supported
150 if the target process has some way to evade the soft terminate
151 request; otherwise, just support the hard terminate method.
153 @return True if the platform supports a soft terminate mechanism.
155 # By default, we do not support a soft terminate mechanism.
158 def soft_terminate(self
, popen_process
, log_file
=None, want_core
=True):
159 # pylint: disable=no-self-use,unused-argument
160 # As expected. We want derived classes to implement this.
161 """Attempts to terminate the process in a polite way.
163 This terminate method is intended to give the child process a
164 chance to clean up and exit on its own, possibly with a request
165 to drop a core file or equivalent (i.e. [mini-]crashdump, crashlog,
166 etc.) If new_process_group was set in the process creation method
167 and the platform supports it, this terminate call will attempt to
168 kill the whole process tree rooted in this child process.
170 @param popen_process the subprocess.Popen-like object returned
171 by one of the process-creation methods of this class.
173 @param log_file file-like object used to emit error-related
174 logging info. May be None if no error-related info is desired.
176 @param want_core True if the caller would like to get a core
177 dump (or the analogous crash report) from the terminated process.
179 popen_process
.terminate()
181 def hard_terminate(self
, popen_process
, log_file
=None):
182 # pylint: disable=no-self-use,unused-argument
183 # As expected. We want derived classes to implement this.
184 """Attempts to terminate the process immediately.
186 This terminate method is intended to kill child process in
187 a manner in which the child process has no ability to block,
188 and also has no ability to clean up properly. If new_process_group
189 was specified when creating the process, and if the platform
190 implementation supports it, this will attempt to kill the
191 whole process tree rooted in the child process.
193 @param popen_process the subprocess.Popen-like object returned
194 by one of the process-creation methods of this class.
196 @param log_file file-like object used to emit error-related
197 logging info. May be None if no error-related info is desired.
201 def was_soft_terminate(self
, returncode
, with_core
):
202 # pylint: disable=no-self-use,unused-argument
203 # As expected. We want derived classes to implement this.
204 """Returns if Popen-like object returncode matches soft terminate.
206 @param returncode the returncode from the Popen-like object that
207 terminated with a given return code.
209 @param with_core indicates whether the returncode should match
210 a core-generating return signal.
212 @return True when the returncode represents what the system would
213 issue when a soft_terminate() with the given with_core arg occurred;
216 if not self
.supports_soft_terminate():
217 # If we don't support soft termination on this platform,
218 # then this should always be False.
221 # Once a platform claims to support soft terminate, it
222 # needs to be able to identify it by overriding this method.
223 raise Exception("platform needs to implement")
225 def was_hard_terminate(self
, returncode
):
226 # pylint: disable=no-self-use,unused-argument
227 # As expected. We want derived classes to implement this.
228 """Returns if Popen-like object returncode matches that of a hard
231 @param returncode the returncode from the Popen-like object that
232 terminated with a given return code.
234 @return True when the returncode represents what the system would
235 issue when a hard_terminate() occurred; False
238 raise Exception("platform needs to implement")
240 def soft_terminate_signals(self
):
241 # pylint: disable=no-self-use
242 """Retrieve signal numbers that can be sent to soft terminate.
243 @return a list of signal numbers that can be sent to soft terminate
244 a process, or None if not applicable.
248 def is_exceptional_exit(self
, popen_status
):
249 """Returns whether the program exit status is exceptional.
251 Returns whether the return code from a Popen process is exceptional
252 (e.g. signals on POSIX systems).
254 Derived classes should override this if they can detect exceptional
257 @return True if the given popen_status represents an exceptional
258 program exit; False otherwise.
262 def exceptional_exit_details(self
, popen_status
):
263 """Returns the normalized exceptional exit code and a description.
265 Given an exceptional exit code, returns the integral value of the
266 exception (e.g. signal number for POSIX) and a description (e.g.
267 signal name on POSIX) for the result.
269 Derived classes should override this if they can detect exceptional
272 It is fine to not implement this so long as is_exceptional_exit()
273 always returns False.
275 @return (normalized exception code, symbolic exception description)
277 raise Exception("exception_exit_details() called on unsupported class")
280 class UnixProcessHelper(ProcessHelper
):
281 """Provides a ProcessHelper for Unix-like operating systems.
283 This implementation supports anything that looks Posix-y
284 (e.g. Darwin, Linux, *BSD, etc.)
288 super(UnixProcessHelper
, self
).__init
__()
291 def _create_new_process_group(cls
):
292 """Creates a new process group for the calling process."""
293 os
.setpgid(os
.getpid(), os
.getpid())
295 def create_piped_process(self
, command
, new_process_group
=True):
296 # Determine what to run after the fork but before the exec.
297 if new_process_group
:
298 preexec_func
= self
._create
_new
_process
_group
302 # Create the process.
303 process
= subprocess
.Popen(
305 stdin
=subprocess
.PIPE
,
306 stdout
=subprocess
.PIPE
,
307 stderr
=subprocess
.PIPE
,
308 universal_newlines
=True, # Elicits automatic byte -> string decoding in Py3
310 preexec_fn
=preexec_func
,
313 # Remember whether we're using process groups for this
315 process
.using_process_groups
= new_process_group
318 def supports_soft_terminate(self
):
319 # POSIX does support a soft terminate via:
320 # * SIGTERM (no core requested)
321 # * SIGQUIT (core requested if enabled, see ulimit -c)
325 def _validate_pre_terminate(cls
, popen_process
, log_file
):
327 if popen_process
is None:
328 raise ValueError("popen_process is None")
330 # Ensure we have something that looks like a valid process.
331 if popen_process
.pid
< 1:
333 log_file
.write("skipping soft_terminate(): no process id")
336 # We only do the process liveness check if we're not using
337 # process groups. With process groups, checking if the main
338 # inferior process is dead and short circuiting here is no
339 # good - children of it in the process group could still be
340 # alive, and they should be killed during a timeout.
341 if not popen_process
.using_process_groups
:
342 # Don't kill if it's already dead.
344 if popen_process
.returncode
is not None:
345 # It has a returncode. It has already stopped.
348 "requested to terminate pid {} but it has already "
349 "terminated, returncode {}".format(
350 popen_process
.pid
, popen_process
.returncode
359 def _kill_with_signal(self
, popen_process
, log_file
, signum
):
360 # Validate we're ready to terminate this.
361 if not self
._validate
_pre
_terminate
(popen_process
, log_file
):
364 # Choose kill mechanism based on whether we're targeting
365 # a process group or just a process.
367 if popen_process
.using_process_groups
:
370 # "sending signum {} to process group {} now\n".format(
371 # signum, popen_process.pid))
372 os
.killpg(popen_process
.pid
, signum
)
376 # "sending signum {} to process {} now\n".format(
377 # signum, popen_process.pid))
378 os
.kill(popen_process
.pid
, signum
)
379 except OSError as error
:
382 if error
.errno
== errno
.ESRCH
:
383 # This is okay - failed to find the process. It may be that
384 # that the timeout pre-kill hook eliminated the process. We'll
390 def soft_terminate(self
, popen_process
, log_file
=None, want_core
=True):
391 # Choose signal based on desire for core file.
393 # SIGQUIT will generate core by default. Can be caught.
394 signum
= signal
.SIGQUIT
396 # SIGTERM is the traditional nice way to kill a process.
397 # Can be caught, doesn't generate a core.
398 signum
= signal
.SIGTERM
400 self
._kill
_with
_signal
(popen_process
, log_file
, signum
)
402 def hard_terminate(self
, popen_process
, log_file
=None):
403 self
._kill
_with
_signal
(popen_process
, log_file
, signal
.SIGKILL
)
405 def was_soft_terminate(self
, returncode
, with_core
):
407 return returncode
== -signal
.SIGQUIT
409 return returncode
== -signal
.SIGTERM
411 def was_hard_terminate(self
, returncode
):
412 return returncode
== -signal
.SIGKILL
414 def soft_terminate_signals(self
):
415 return [signal
.SIGQUIT
, signal
.SIGTERM
]
417 def is_exceptional_exit(self
, popen_status
):
418 return popen_status
< 0
421 def _signal_names_by_number(cls
):
424 for v
, k
in reversed(sorted(signal
.__dict
__.items()))
425 if v
.startswith("SIG") and not v
.startswith("SIG_")
428 def exceptional_exit_details(self
, popen_status
):
429 signo
= -popen_status
430 signal_names_by_number
= self
._signal
_names
_by
_number
()
431 signal_name
= signal_names_by_number
.get(signo
, "")
432 return (signo
, signal_name
)
435 class WindowsProcessHelper(ProcessHelper
):
436 """Provides a Windows implementation of the ProcessHelper class."""
439 super(WindowsProcessHelper
, self
).__init
__()
441 def create_piped_process(self
, command
, new_process_group
=True):
442 if new_process_group
:
443 # We need this flag if we want os.kill() to work on the subprocess.
444 creation_flags
= subprocess
.CREATE_NEW_PROCESS_GROUP
448 return subprocess
.Popen(
450 stdin
=subprocess
.PIPE
,
451 stdout
=subprocess
.PIPE
,
452 stderr
=subprocess
.PIPE
,
453 universal_newlines
=True, # Elicits automatic byte -> string decoding in Py3
454 creationflags
=creation_flags
,
457 def was_hard_terminate(self
, returncode
):
458 return returncode
!= 0
461 class ProcessDriver(object):
462 """Drives a child process, notifies on important events, and can timeout.
464 Clients are expected to derive from this class and override the
465 on_process_started and on_process_exited methods if they want to
466 hook either of those.
468 This class supports timing out the child process in a platform-agnostic
469 way. The on_process_exited method is informed if the exit was natural
470 or if it was due to a timeout.
473 def __init__(self
, soft_terminate_timeout
=10.0):
474 super(ProcessDriver
, self
).__init
__()
475 self
.process_helper
= ProcessHelper
.process_helper()
477 # Create the synchronization event for notifying when the
478 # inferior dotest process is complete.
479 self
.done_event
= threading
.Event()
480 self
.io_thread
= None
482 # Number of seconds to wait for the soft terminate to
483 # wrap up, before moving to more drastic measures.
484 # Might want this longer if core dumps are generated and
485 # take a long time to write out.
486 self
.soft_terminate_timeout
= soft_terminate_timeout
487 # Number of seconds to wait for the hard terminate to
488 # wrap up, before giving up on the io thread. This should
490 self
.hard_terminate_timeout
= 5.0
491 self
.returncode
= None
493 # =============================================
494 # Methods for subclasses to override if desired.
495 # =============================================
497 def on_process_started(self
):
500 def on_process_exited(self
, command
, output
, was_timeout
, exit_status
):
503 def on_timeout_pre_kill(self
):
504 """Called after the timeout interval elapses but before killing it.
506 This method is added to enable derived classes the ability to do
507 something to the process prior to it being killed. For example,
508 this would be a good spot to run a program that samples the process
509 to see what it was doing (or not doing).
511 Do not attempt to reap the process (i.e. use wait()) in this method.
512 That will interfere with the kill mechanism and return code processing.
515 def write(self
, content
):
516 # pylint: disable=no-self-use
517 # Intended - we want derived classes to be able to override
518 # this and use any self state they may contain.
519 sys
.stdout
.write(content
)
521 # ==============================================================
522 # Operations used to drive processes. Clients will want to call
524 # ==============================================================
526 def run_command(self
, command
):
527 # Start up the child process and the thread that does the
528 # communication pump.
529 self
._start
_process
_and
_io
_thread
(command
)
531 # Wait indefinitely for the child process to finish
532 # communicating. This indicates it has closed stdout/stderr
534 self
.io_thread
.join()
535 self
.returncode
= self
.process
.wait()
536 if self
.returncode
is None:
538 "no exit status available for pid {} after the "
539 " inferior dotest.py should have completed".format(self
.process
.pid
)
542 # Notify of non-timeout exit.
543 self
.on_process_exited(command
, self
.io_thread
.output
, False, self
.returncode
)
545 def run_command_with_timeout(self
, command
, timeout
, want_core
):
546 # Figure out how many seconds our timeout description is requesting.
547 timeout_seconds
= timeout_to_seconds(timeout
)
549 # Start up the child process and the thread that does the
550 # communication pump.
551 self
._start
_process
_and
_io
_thread
(command
)
553 self
._wait
_with
_timeout
(timeout_seconds
, command
, want_core
)
559 def _start_process_and_io_thread(self
, command
):
560 # Create the process.
561 self
.process
= self
.process_helper
.create_piped_process(command
)
562 self
.pid
= self
.process
.pid
563 self
.on_process_started()
565 # Ensure the event is cleared that is used for signaling
566 # from the communication() thread when communication is
567 # complete (i.e. the inferior process has finished).
568 self
.done_event
.clear()
570 self
.io_thread
= CommunicatorThread(self
.process
, self
.done_event
, self
.write
)
571 self
.io_thread
.start()
573 def _attempt_soft_kill(self
, want_core
):
574 # The inferior dotest timed out. Attempt to clean it
575 # with a non-drastic method (so it can clean up properly
576 # and/or generate a core dump). Often the OS can't guarantee
577 # that the process will really terminate after this.
578 self
.process_helper
.soft_terminate(
579 self
.process
, want_core
=want_core
, log_file
=self
582 # Now wait up to a certain timeout period for the io thread
583 # to say that the communication ended. If that wraps up
584 # within our soft terminate timeout, we're all done here.
585 self
.io_thread
.join(self
.soft_terminate_timeout
)
586 if not self
.io_thread
.is_alive():
587 # stdout/stderr were closed on the child process side. We
588 # should be able to wait and reap the child process here.
589 self
.returncode
= self
.process
.wait()
590 # We terminated, and the done_trying result is n/a
595 "soft kill attempt of process {} timed out "
596 "after {} seconds\n".format(
597 self
.process
.pid
, self
.soft_terminate_timeout
602 return terminated
, done_trying
604 def _attempt_hard_kill(self
):
605 # Instruct the process to terminate and really force it to
606 # happen. Don't give the process a chance to ignore.
607 self
.process_helper
.hard_terminate(self
.process
, log_file
=self
)
609 # Reap the child process. This should not hang as the
610 # hard_kill() mechanism is supposed to really kill it.
611 # Improvement option:
612 # If this does ever hang, convert to a self.process.poll()
613 # loop checking on self.process.returncode until it is not
614 # None or the timeout occurs.
615 self
.returncode
= self
.process
.wait()
617 # Wait a few moments for the io thread to finish...
618 self
.io_thread
.join(self
.hard_terminate_timeout
)
619 if self
.io_thread
.is_alive():
620 # ... but this is not critical if it doesn't end for some
623 "hard kill of process {} timed out after {} seconds waiting "
624 "for the io thread (ignoring)\n".format(
625 self
.process
.pid
, self
.hard_terminate_timeout
629 # Set if it terminated. (Set up for optional improvement above).
630 terminated
= self
.returncode
is not None
631 # Nothing else to try.
634 return terminated
, done_trying
636 def _attempt_termination(self
, attempt_count
, want_core
):
637 if self
.process_helper
.supports_soft_terminate():
638 # When soft termination is supported, we first try to stop
639 # the process with a soft terminate. Failing that, we try
640 # the hard terminate option.
641 if attempt_count
== 1:
642 return self
._attempt
_soft
_kill
(want_core
)
643 elif attempt_count
== 2:
644 return self
._attempt
_hard
_kill
()
646 # We don't have anything else to try.
647 terminated
= self
.returncode
is not None
649 return terminated
, done_trying
651 # We only try the hard terminate option when there
652 # is no soft terminate available.
653 if attempt_count
== 1:
654 return self
._attempt
_hard
_kill
()
656 # We don't have anything else to try.
657 terminated
= self
.returncode
is not None
659 return terminated
, done_trying
661 def _wait_with_timeout(self
, timeout_seconds
, command
, want_core
):
662 # Allow up to timeout seconds for the io thread to wrap up.
663 # If that completes, the child process should be done.
664 completed_normally
= self
.done_event
.wait(timeout_seconds
)
665 if completed_normally
:
666 # Reap the child process here.
667 self
.returncode
= self
.process
.wait()
669 # Allow derived classes to do some work after we detected
670 # a timeout but before we touch the timed-out process.
671 self
.on_timeout_pre_kill()
673 # Prepare to stop the process
674 process_terminated
= completed_normally
675 terminate_attempt_count
= 0
677 # Try as many attempts as we support for trying to shut down
678 # the child process if it's not already shut down.
679 while not process_terminated
:
680 terminate_attempt_count
+= 1
681 # Attempt to terminate.
682 process_terminated
, done_trying
= self
._attempt
_termination
(
683 terminate_attempt_count
, want_core
685 # Check if there's nothing more to try.
687 # Break out of our termination attempt loop.
690 # At this point, we're calling it good. The process
691 # finished gracefully, was shut down after one or more
692 # attempts, or we failed but gave it our best effort.
693 self
.on_process_exited(
694 command
, self
.io_thread
.output
, not completed_normally
, self
.returncode
698 def patched_init(self
, *args
, **kwargs
):
699 self
.original_init(*args
, **kwargs
)
700 # Initialize our condition variable that protects wait()/poll().
701 self
.wait_condition
= threading
.Condition()
704 def patched_wait(self
, *args
, **kwargs
):
705 self
.wait_condition
.acquire()
707 result
= self
.original_wait(*args
, **kwargs
)
708 # The process finished. Signal the condition.
709 self
.wait_condition
.notify_all()
712 self
.wait_condition
.release()
715 def patched_poll(self
, *args
, **kwargs
):
716 self
.wait_condition
.acquire()
718 result
= self
.original_poll(*args
, **kwargs
)
719 if self
.returncode
is not None:
720 # We did complete, and we have the return value.
721 # Signal the event to indicate we're done.
722 self
.wait_condition
.notify_all()
725 self
.wait_condition
.release()
728 def patch_up_subprocess_popen():
729 subprocess
.Popen
.original_init
= subprocess
.Popen
.__init
__
730 subprocess
.Popen
.__init
__ = patched_init
732 subprocess
.Popen
.original_wait
= subprocess
.Popen
.wait
733 subprocess
.Popen
.wait
= patched_wait
735 subprocess
.Popen
.original_poll
= subprocess
.Popen
.poll
736 subprocess
.Popen
.poll
= patched_poll
739 # Replace key subprocess.Popen() threading-unprotected methods with
740 # threading-protected versions.
741 patch_up_subprocess_popen()