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. | |
| 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 | 87 |
| 69 try: | 88 try: |
| 70 os.kill(pid, signal.SIGINT) | 89 os.kill(pid, signal.SIGINT) |
|
Vadim Sh.
2016/10/21 03:01:48
I've just discovered that on Windows this doesn't
| |
| 71 except OSError as e: | 90 except OSError as e: |
| 72 # Already dead? | 91 # Already dead? |
| 73 if e.errno in (errno.ECHILD, errno.EPERM, errno.ESRCH): | 92 if e.errno in (errno.ECHILD, errno.EPERM, errno.ESRCH): |
| 74 print('Can\'t send SIGINT to process %d. Already dead? Errno %d.' % | 93 print('Can\'t send SIGINT to process %d. Already dead? Errno %d.' % |
| 75 (pid, e.errno)) | 94 (pid, e.errno)) |
| 76 return | 95 return |
| 77 raise | 96 raise |
| 78 | 97 |
| 79 print('SIGINT has been sent to process %d. ' | 98 print('SIGINT has been sent to process %d. ' |
| 80 'Going to wait for the process finishes.' % pid) | 99 'Going to wait for the process finishes.' % pid) |
| 81 if os.name == 'nt': | 100 if os.name == 'nt': |
| 101 pool = multiprocessing.Pool(1) | |
| 102 res = pool.apply_async(waitpid_for_win) | |
| 82 try: | 103 try: |
| 83 os.waitpid(pid, 0) | 104 e = res.get(10) |
| 84 except OSError as e: | 105 except multiprocessing.TimeoutError: |
| 85 if e.errno == errno.ECHILD: | 106 print('process %d running more than 10 seconds' % pid) |
| 86 print('process %d died before waitpitd' % pid) | 107 raise NotDiedError() |
| 87 return | 108 if e is None: |
| 88 raise e | 109 return |
| 110 if isinstance(e, OSError) and e.errno == errno.ECHILD: | |
| 111 print('process %d died before waitpitd' % pid) | |
| 112 return | |
| 113 raise e | |
| 89 else: | 114 else: |
| 90 for _ in xrange(10): | 115 for _ in xrange(10): |
| 91 time.sleep(1) | 116 time.sleep(1) |
| 92 if not is_running_posix(pid): | 117 if not is_running_posix(pid): |
| 93 return | 118 return |
| 94 | 119 |
| 95 print('process %d running more than 10 seconds' % pid) | 120 print('process %d running more than 10 seconds' % pid) |
| 96 raise NotDiedError() | 121 raise NotDiedError() |
| 97 | 122 |
| 98 | 123 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 try: | 158 try: |
| 134 os.kill(pid, signal.SIGTERM) | 159 os.kill(pid, signal.SIGTERM) |
| 135 except OSError as e: | 160 except OSError as e: |
| 136 print('Failed to send SIGTERM to process %d: %s' % (pid, e)) | 161 print('Failed to send SIGTERM to process %d: %s' % (pid, e)) |
| 137 # We do not reraise because I believe not suspending the process | 162 # We do not reraise because I believe not suspending the process |
| 138 # is more important than completely killing cloudtail. | 163 # is more important than completely killing cloudtail. |
| 139 | 164 |
| 140 | 165 |
| 141 if '__main__' == __name__: | 166 if '__main__' == __name__: |
| 142 sys.exit(main()) | 167 sys.exit(main()) |
| OLD | NEW |