Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(293)

Side by Side Diff: scripts/slave/recipe_modules/goma/resources/cloudtail_utils.py

Issue 2444233002: Not wait cloudtail finish forerver on Windows. (Closed)
Patch Set: fixed function comments. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 os 8 import os
9 import signal 9 import signal
10 import subprocess 10 import subprocess
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 return False 47 return False
48 raise e 48 raise e
49 return True 49 return True
50 50
51 51
52 class NotDiedError(Exception): 52 class NotDiedError(Exception):
53 def __str__(self): 53 def __str__(self):
54 return "NotDiedError" 54 return "NotDiedError"
55 55
56 56
57 class Error(Exception):
58 """Raised on something unexpected happens."""
59
60
61 def wait_termination_win(pid):
62 """Send CTRL_C_EVENT or SIGINT to pid and wait termination of pid.
63
64 Args:
65 pid(int): pid of process which this function waits termination.
66
67 Raises:
68 Error: WaitForSingleObject to wait process termination returns neigher of
69 WAIT_TIMEOUT or WAIT_OBJECT_0 (i.e. termination succeeded).
70 NotDiedError: if cloudtail kept on running 10 seconds after it signaled.
71 """
72 import win32api
73 import win32con
74 import win32event
75 import winerror
76 import pywintypes
77 handle = None
78 try:
79 handle = win32api.OpenProcess(
80 win32con.PROCESS_QUERY_INFORMATION | win32con.SYNCHRONIZE,
81 False, pid)
82 try:
83 os.kill(pid, signal.CTRL_C_EVENT)
84 print('CTRL_C_EVENT has been sent to process %d. '
85 'Going to wait for the process finishes.' % pid)
86 except WindowsError as e: # pylint: disable=E0602
87 # If a target process does not share terminal, we cannot send Ctrl-C.
88 if e[0] == winerror.ERROR_INVALID_PARAMETER:
89 os.kill(pid, signal.SIGINT)
90 print('SIGINT has been sent to process %d.' % pid)
91 ret = win32event.WaitForSingleObject(handle, 10 * 10**3)
92 if ret == win32event.WAIT_TIMEOUT:
93 print('process %d running more than 10 seconds' % pid)
94 raise NotDiedError()
95 elif ret == win32event.WAIT_OBJECT_0:
96 return
97 raise Error('Unexpected return code %d for pid %d.' % (ret, pid))
98 except pywintypes.error as e:
99 if e[0] == winerror.ERROR_INVALID_PARAMETER and e[1] == 'OpenProcess':
100 print('Can\'t open process %d. Already dead? error %s.' % (pid, e))
101 return
102 raise
103 except OSError as e:
104 if e.errno in (errno.ECHILD, errno.EPERM, errno.ESRCH):
105 print('Can\'t send SIGINT to process %d. Already dead? Errno %d.' %
106 (pid, e.errno))
107 return
108 raise
109 finally:
110 if handle:
111 win32api.CloseHandle(handle)
112
113
57 def wait_termination(pid): 114 def wait_termination(pid):
58 """Send SIGINT to pid and wait termination of pid. 115 """Send SIGINT to pid and wait termination of pid.
59 116
60 Args: 117 Args:
61 pid(int): pid of process which this function waits termination. 118 pid(int): pid of process which this function waits termination.
62 119
63 Raises: 120 Raises:
64 OSError: is_running_posix, os.waitpid and os.kill may throw OSError. 121 OSError: is_running_posix, os.waitpid and os.kill may throw OSError.
65 NotDiedError: if cloudtail is running after 10 seconds waiting, 122 NotDiedError: if cloudtail is running after 10 seconds waiting,
66 NotDiedError is raised. 123 NotDiedError is raised.
67 """ 124 """
68
69 try:
70 os.kill(pid, signal.SIGINT)
71 except OSError as e:
72 # Already dead?
73 if e.errno in (errno.ECHILD, errno.EPERM, errno.ESRCH):
74 print('Can\'t send SIGINT to process %d. Already dead? Errno %d.' %
75 (pid, e.errno))
76 return
77 raise
78
79 print('SIGINT has been sent to process %d. '
80 'Going to wait for the process finishes.' % pid)
81 if os.name == 'nt': 125 if os.name == 'nt':
126 wait_termination_win(pid)
127 else:
82 try: 128 try:
83 os.waitpid(pid, 0) 129 os.kill(pid, signal.SIGINT)
84 except OSError as e: 130 except OSError as e:
85 if e.errno == errno.ECHILD: 131 if e.errno in (errno.ECHILD, errno.EPERM, errno.ESRCH):
86 print('process %d died before waitpitd' % pid) 132 print('Can\'t send SIGINT to process %d. Already dead? Errno %d.' %
133 (pid, e.errno))
87 return 134 return
88 raise e 135 raise
89 else:
90 for _ in xrange(10): 136 for _ in xrange(10):
91 time.sleep(1) 137 time.sleep(1)
92 if not is_running_posix(pid): 138 if not is_running_posix(pid):
93 return 139 return
94
95 print('process %d running more than 10 seconds' % pid) 140 print('process %d running more than 10 seconds' % pid)
96 raise NotDiedError() 141 raise NotDiedError()
97 142
98 143
99 def main(): 144 def main():
100 parser = argparse.ArgumentParser( 145 parser = argparse.ArgumentParser(
101 description='cloudtail utility for goma recipe module.') 146 description='cloudtail utility for goma recipe module.')
102 147
103 subparsers = parser.add_subparsers(help='commands for cloudtail') 148 subparsers = parser.add_subparsers(help='commands for cloudtail')
104 149
(...skipping 15 matching lines...) Expand all
120 165
121 if args.command == 'start': 166 if args.command == 'start':
122 start_cloudtail(args) 167 start_cloudtail(args)
123 elif args.command == 'stop': 168 elif args.command == 'stop':
124 with open(args.killed_pid_file) as f: 169 with open(args.killed_pid_file) as f:
125 # cloudtail flushes log and terminates 170 # cloudtail flushes log and terminates
126 # within 5 seconds when it recieves SIGINT. 171 # within 5 seconds when it recieves SIGINT.
127 pid = int(f.read()) 172 pid = int(f.read())
128 try: 173 try:
129 wait_termination(pid) 174 wait_termination(pid)
130 except (OSError, NotDiedError) as e: 175 except Exception as e:
131 print('Going to send SIGTERM to process %d due to Error %s' % (pid, e)) 176 print('Going to send SIGTERM to process %d due to Error %s' % (pid, e))
132 # Since Windows does not have SIGKILL, we need to use SIGTERM. 177 # Since Windows does not have SIGKILL, we need to use SIGTERM.
133 try: 178 try:
134 os.kill(pid, signal.SIGTERM) 179 os.kill(pid, signal.SIGTERM)
135 except OSError as e: 180 except OSError as e:
136 print('Failed to send SIGTERM to process %d: %s' % (pid, e)) 181 print('Failed to send SIGTERM to process %d: %s' % (pid, e))
137 # We do not reraise because I believe not suspending the process 182 # We do not reraise because I believe not suspending the process
138 # is more important than completely killing cloudtail. 183 # is more important than completely killing cloudtail.
139 184
140 185
141 if '__main__' == __name__: 186 if '__main__' == __name__:
142 sys.exit(main()) 187 sys.exit(main())
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698