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

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: add docstring, close handler, etc.. 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 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 29 matching lines...) Expand all
47 return False 48 return False
48 raise e 49 raise e
49 return True 50 return True
50 51
51 52
52 class NotDiedError(Exception): 53 class NotDiedError(Exception):
53 def __str__(self): 54 def __str__(self):
54 return "NotDiedError" 55 return "NotDiedError"
55 56
56 57
58 def wait_termination_win(pid):
59 """Send CTRL_C_EVENT or SIGINT to pid and wait termination of pid.
60
61 Args:
62 pid(int): pid of process which this function waits termination.
63
64 Returns:
65 None: if pid has already finished or process finished succesfully.
66 instance of Exception: if raised unexpectedly.
67 """
68 import win32api
69 import win32con
70 import pywintypes
71 handle = None
72 try:
73 handle = win32api.OpenProcess(
74 win32con.PROCESS_QUERY_INFORMATION | win32con.SYNCHRONIZE,
75 False, pid)
76 try:
77 os.kill(pid, signal.CTRL_C_EVENT)
78 print('CTRL_C_EVENT has been sent to process %d. '
79 'Going to wait for the process finishes.' % pid)
80 except WindowsError as e: # pylint: disable=E0602
81 # If a target process does not share terminal, we cannot send Ctrl-C.
82 if e.errno == 87: # 87 == invalid parameter
83 os.kill(pid, signal.SIGINT)
84 os.waitpid(handle, 0)
Vadim Sh. 2016/10/25 18:35:41 I think it would be simpler to use WaitForSingleOb
Yoshisato Yanagisawa 2016/10/26 01:17:45 Done.
85 return None
86 except pywintypes.error as e:
87 if e[0] == 87 and e[1] == 'OpenProcess':
88 print('Can\'t open process %d. Already dead? error %d.' % (pid, e))
89 return None
90 raise
91 except OSError as e:
92 if e.errno in (errno.ECHILD, errno.EPERM, errno.ESRCH):
93 print('Can\'t send SIGINT to process %d. Already dead? Errno %d.' %
94 (pid, e.errno))
95 return None
96 raise
97 except Exception as e:
98 return e
Vadim Sh. 2016/10/25 18:35:41 this is weird... is it because of 'multiprocessing
Yoshisato Yanagisawa 2016/10/26 01:17:45 right. revised without multiprocessing.
99 finally:
100 if handle:
101 win32api.CloseHandle(handle)
102
103
57 def wait_termination(pid): 104 def wait_termination(pid):
58 """Send SIGINT to pid and wait termination of pid. 105 """Send SIGINT to pid and wait termination of pid.
59 106
60 Args: 107 Args:
61 pid(int): pid of process which this function waits termination. 108 pid(int): pid of process which this function waits termination.
62 109
63 Raises: 110 Raises:
64 OSError: is_running_posix, os.waitpid and os.kill may throw OSError. 111 OSError: is_running_posix, os.waitpid and os.kill may throw OSError.
65 NotDiedError: if cloudtail is running after 10 seconds waiting, 112 NotDiedError: if cloudtail is running after 10 seconds waiting,
66 NotDiedError is raised. 113 NotDiedError is raised.
67 """ 114 """
68 115 if os.name == 'nt':
69 try: 116 pool = multiprocessing.Pool(1)
70 os.kill(pid, signal.SIGINT) 117 res = pool.apply_async(wait_termination_win, [pid])
71 except OSError as e: 118 try:
72 # Already dead? 119 e = res.get(10)
73 if e.errno in (errno.ECHILD, errno.EPERM, errno.ESRCH): 120 except multiprocessing.TimeoutError:
74 print('Can\'t send SIGINT to process %d. Already dead? Errno %d.' % 121 print('process %d running more than 10 seconds.' % pid)
75 (pid, e.errno)) 122 raise NotDiedError()
123 if e is None:
76 return 124 return
77 raise 125 raise e
78 126 else:
79 print('SIGINT has been sent to process %d. '
80 'Going to wait for the process finishes.' % pid)
81 if os.name == 'nt':
82 try: 127 try:
83 os.waitpid(pid, 0) 128 os.kill(pid, signal.SIGINT)
84 except OSError as e: 129 except OSError as e:
85 if e.errno == errno.ECHILD: 130 if e.errno in (errno.ECHILD, errno.EPERM, errno.ESRCH):
86 print('process %d died before waitpitd' % pid) 131 print('Can\'t send SIGINT to process %d. Already dead? Errno %d.' %
132 (pid, e.errno))
87 return 133 return
88 raise e
89 else:
90 for _ in xrange(10): 134 for _ in xrange(10):
91 time.sleep(1) 135 time.sleep(1)
92 if not is_running_posix(pid): 136 if not is_running_posix(pid):
93 return 137 return
94
95 print('process %d running more than 10 seconds' % pid) 138 print('process %d running more than 10 seconds' % pid)
96 raise NotDiedError() 139 raise NotDiedError()
97 140
98 141
99 def main(): 142 def main():
100 parser = argparse.ArgumentParser( 143 parser = argparse.ArgumentParser(
101 description='cloudtail utility for goma recipe module.') 144 description='cloudtail utility for goma recipe module.')
102 145
103 subparsers = parser.add_subparsers(help='commands for cloudtail') 146 subparsers = parser.add_subparsers(help='commands for cloudtail')
104 147
(...skipping 15 matching lines...) Expand all
120 163
121 if args.command == 'start': 164 if args.command == 'start':
122 start_cloudtail(args) 165 start_cloudtail(args)
123 elif args.command == 'stop': 166 elif args.command == 'stop':
124 with open(args.killed_pid_file) as f: 167 with open(args.killed_pid_file) as f:
125 # cloudtail flushes log and terminates 168 # cloudtail flushes log and terminates
126 # within 5 seconds when it recieves SIGINT. 169 # within 5 seconds when it recieves SIGINT.
127 pid = int(f.read()) 170 pid = int(f.read())
128 try: 171 try:
129 wait_termination(pid) 172 wait_termination(pid)
130 except (OSError, NotDiedError) as e: 173 except Exception as e:
131 print('Going to send SIGTERM to process %d due to Error %s' % (pid, e)) 174 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. 175 # Since Windows does not have SIGKILL, we need to use SIGTERM.
133 try: 176 try:
134 os.kill(pid, signal.SIGTERM) 177 os.kill(pid, signal.SIGTERM)
135 except OSError as e: 178 except OSError as e:
136 print('Failed to send SIGTERM to process %d: %s' % (pid, e)) 179 print('Failed to send SIGTERM to process %d: %s' % (pid, e))
137 # We do not reraise because I believe not suspending the process 180 # We do not reraise because I believe not suspending the process
138 # is more important than completely killing cloudtail. 181 # is more important than completely killing cloudtail.
139 182
140 183
141 if '__main__' == __name__: 184 if '__main__' == __name__:
142 sys.exit(main()) 185 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