Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2016 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import argparse | 6 import argparse |
| 7 import errno | 7 import errno |
| 8 import multiprocessing | |
| 8 import os | 9 import os |
| 9 import signal | 10 import signal |
| 10 import subprocess | 11 import subprocess |
| 11 import sys | 12 import sys |
| 12 import time | 13 import time |
| 13 | 14 |
| 14 from slave import goma_utils | 15 from slave import goma_utils |
| 15 | 16 |
| 16 | 17 |
| 17 def start_cloudtail(args): | 18 def start_cloudtail(args): |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 42 | 43 |
| 43 try: | 44 try: |
| 44 os.kill(pid, 0) | 45 os.kill(pid, 0) |
| 45 except OSError as e: | 46 except OSError as e: |
| 46 if e.errno == errno.ESRCH or e.errno == errno.EPERM: | 47 if e.errno == errno.ESRCH or e.errno == errno.EPERM: |
| 47 return False | 48 return False |
| 48 raise e | 49 raise e |
| 49 return True | 50 return True |
| 50 | 51 |
| 51 | 52 |
| 53 def waitpid_for_win(pid): | |
| 54 """Return exception if raised in os.waitpid, or None. | |
| 55 | |
| 56 Args: | |
| 57 pid(int): pid of process which this function checks | |
| 58 whether it is running or not. | |
| 59 | |
| 60 Returns: | |
| 61 Exception: if something raised in os.waitpid. | |
|
tikuta
2016/10/21 02:35:04
nit: I prefer to use 'If' here or use 'otherwise'
Yoshisato Yanagisawa
2016/10/21 02:38:15
good catch.
| |
| 62 None: Otherwise. | |
| 63 """ | |
| 64 try: | |
| 65 os.waitpid(pid, 0) | |
| 66 except Exception as e: | |
| 67 return e | |
| 68 return None | |
| 69 | |
| 70 | |
| 52 class NotDiedError(Exception): | 71 class NotDiedError(Exception): |
| 53 def __str__(self): | 72 def __str__(self): |
| 54 return "NotDiedError" | 73 return "NotDiedError" |
| 55 | 74 |
| 56 | 75 |
| 57 def wait_termination(pid): | 76 def wait_termination(pid): |
| 58 """Send SIGINT to pid and wait termination of pid. | 77 """Send SIGINT to pid and wait termination of pid. |
| 59 | 78 |
| 60 Args: | 79 Args: |
| 61 pid(int): pid of process which this function waits termination. | 80 pid(int): pid of process which this function waits termination. |
| 62 | 81 |
| 63 Raises: | 82 Raises: |
| 64 OSError: is_running_posix, os.waitpid and os.kill may throw OSError. | 83 OSError: is_running_posix, os.waitpid and os.kill may throw OSError. |
| 65 NotDiedError: if cloudtail is running after 10 seconds waiting, | 84 NotDiedError: if cloudtail is running after 10 seconds waiting, |
| 66 NotDiedError is raised. | 85 NotDiedError is raised. |
| 67 """ | 86 """ |
| 68 try: | 87 try: |
| 69 os.kill(pid, signal.SIGINT) | 88 os.kill(pid, signal.SIGINT) |
| 70 except OSError as e: | 89 except OSError as e: |
| 71 # Already dead? | 90 # Already dead? |
| 72 if e.errno in (errno.ECHILD, errno.EPERM, errno.ESRCH): | 91 if e.errno in (errno.ECHILD, errno.EPERM, errno.ESRCH): |
| 73 return | 92 return |
| 74 raise | 93 raise |
| 75 | 94 |
| 76 print('SIGINT has been sent to process %d. ' | 95 print('SIGINT has been sent to process %d. ' |
| 77 'Going to wait for the process finishes.' % pid) | 96 'Going to wait for the process finishes.' % pid) |
| 78 if os.name == 'nt': | 97 if os.name == 'nt': |
| 98 pool = multiprocessing.Pool(1) | |
| 99 res = pool.apply_async(waitpid_for_win) | |
| 79 try: | 100 try: |
| 80 os.waitpid(pid, 0) | 101 e = res.get(10) |
| 81 except OSError as e: | 102 except multiprocessing.TimeoutError: |
| 82 if e.errno == errno.ECHILD: | 103 print('process %d running more than 10 seconds' % pid) |
| 104 raise NotDiedError() | |
| 105 if e is None: | |
| 106 return | |
| 107 if isinstance(e, OSError) and e.errno == errno.ECHILD: | |
| 83 print('process %d died before waitpitd' % pid) | 108 print('process %d died before waitpitd' % pid) |
| 84 return | 109 return |
| 85 raise e | 110 raise e |
| 86 else: | 111 else: |
| 87 for _ in xrange(10): | 112 for _ in xrange(10): |
| 88 time.sleep(1) | 113 time.sleep(1) |
| 89 if not is_running_posix(pid): | 114 if not is_running_posix(pid): |
| 90 return | 115 return |
| 91 | 116 |
| 92 print('process %d running more than 10 seconds' % pid) | 117 print('process %d running more than 10 seconds' % pid) |
| 93 raise NotDiedError() | 118 raise NotDiedError() |
| 94 | 119 |
| 95 | 120 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 try: | 155 try: |
| 131 os.kill(pid, signal.SIGTERM) | 156 os.kill(pid, signal.SIGTERM) |
| 132 except OSError as e: | 157 except OSError as e: |
| 133 print('Failed to send SIGTERM to process %d: %s' % (pid, e)) | 158 print('Failed to send SIGTERM to process %d: %s' % (pid, e)) |
| 134 # We do not reraise because I believe not suspending the process | 159 # We do not reraise because I believe not suspending the process |
| 135 # is more important than completely killing cloudtail. | 160 # is more important than completely killing cloudtail. |
| 136 | 161 |
| 137 | 162 |
| 138 if '__main__' == __name__: | 163 if '__main__' == __name__: |
| 139 sys.exit(main()) | 164 sys.exit(main()) |
| OLD | NEW |