Mercurial > hg
annotate tests/killdaemons.py @ 35428:71427ff1dff8
workers: handling exceptions in windows workers
This adds handling of exceptions from worker threads and resurfaces them as if the function ran without workers.
If any of the threads throws, the main thread kills all running threads giving them 5 sec to handle the interruption and raises the first exception received.
We don't have to join threads if is_alive() is false
Test Plan:
Ran multiple updates/enable/disable sparse profile and things worked well
Ran test on CentOS- all tests passing on @ passed here
Added a forged exception into the worker code and got it properly resurfaced and the rest of workers killed: P58642088
PS C:\open\<repo>> ..\facebook-hg-rpms\build\hg\hg.exe --config extensions.fsmonitor=! sparse --enable-profile <profile>
updating [==> ] 1300/39166 1m57sException in thread Thread-3:
Traceback (most recent call last):
File "C:\open\facebook-hg-rpms\build\hg\hg-python\lib\threading.py", line 801, in __bootstrap_inner
self.run()
File "C:\open\facebook-hg-rpms\build\hg\mercurial\worker.py", line 244, in run
raise e
Exception: Forged exception
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\open\facebook-hg-rpms\build\hg\hg-python\lib\threading.py", line 801, in __bootstrap_inner
self.run()
File "C:\open\facebook-hg-rpms\build\hg\mercurial\worker.py", line 244, in run
raise e
Exception: Forged exception
<...>
Traceback (most recent call last):
File "C:\open\facebook-hg-rpms\build\hg\hgexe.py", line 41, in <module>
dispatch.run()
File "C:\open\facebook-hg-rpms\build\hg\mercurial\dispatch.py", line 85, in run
status = (dispatch(req) or 0) & 255
File "C:\open\facebook-hg-rpms\build\hg\mercurial\dispatch.py", line 173, in dispatch
ret = _runcatch(req)
File "C:\open\facebook-hg-rpms\build\hg\mercurial\dispatch.py", line 324, in _runcatch
return _callcatch(ui, _runcatchfunc)
File "C:\open\facebook-hg-rpms\build\hg\mercurial\dispatch.py", line 332, in _callcatch
return scmutil.callcatch(ui, func)
File "C:\open\facebook-hg-rpms\build\hg\mercurial\scmutil.py", line 154, in callcatch
return func()
File "C:\open\facebook-hg-rpms\build\hg\mercurial\dispatch.py", line 314, in _runcatchfunc
return _dispatch(req)
File "C:\open\facebook-hg-rpms\build\hg\mercurial\dispatch.py", line 951, in _dispatch
cmdpats, cmdoptions)
File "C:\open\facebook-hg-rpms\build\hg\hg-python\lib\site-packages\remotefilelog\__init__.py", line 415, in runcommand
return orig(lui, repo, *args, **kwargs)
File "C:\open\facebook-hg-rpms\build\hg\hg-python\lib\site-packages\hgext3rd\undo.py", line 118, in _runcommandwrapper
result = orig(lui, repo, cmd, fullargs, *args)
File "C:\open\facebook-hg-rpms\build\hg\hgext\journal.py", line 84, in runcommand
return orig(lui, repo, cmd, fullargs, *args)
File "C:\open\facebook-hg-rpms\build\hg\hg-python\lib\site-packages\hgext3rd\perftweaks.py", line 268, in _tracksparseprofiles
res = runcommand(lui, repo, *args)
File "C:\open\facebook-hg-rpms\build\hg\hg-python\lib\site-packages\hgext3rd\perftweaks.py", line 256, in _trackdirstatesizes
res = runcommand(lui, repo, *args)
File "C:\open\facebook-hg-rpms\build\hg\hg-python\lib\site-packages\hgext3rd\copytrace.py", line 144, in _runcommand
return orig(lui, repo, cmd, fullargs, ui, *args, **kwargs)
File "C:\open\facebook-hg-rpms\build\hg\hg-python\lib\site-packages\hgext3rd\fbamend\hiddenoverride.py", line 119, in runcommand
result = orig(lui, repo, cmd, fullargs, *args)
File "C:\open\facebook-hg-rpms\build\hg\mercurial\dispatch.py", line 712, in runcommand
ret = _runcommand(ui, options, cmd, d)
File "C:\open\facebook-hg-rpms\build\hg\mercurial\dispatch.py", line 959, in _runcommand
return cmdfunc()
File "C:\open\facebook-hg-rpms\build\hg\mercurial\dispatch.py", line 948, in <lambda>
d = lambda: util.checksignature(func)(ui, *args, **strcmdopt)
File "C:\open\facebook-hg-rpms\build\hg\mercurial\util.py", line 1183, in check
return func(*args, **kwargs)
File "C:\open\facebook-hg-rpms\build\hg\hg-python\lib\site-packages\hgext3rd\fbsparse.py", line 860, in sparse
disableprofile=disableprofile, force=force)
File "C:\open\facebook-hg-rpms\build\hg\hg-python\lib\site-packages\hgext3rd\fbsparse.py", line 949, in _config
len, _refresh(ui, repo, oldstatus, oldsparsematch, force))
File "C:\open\facebook-hg-rpms\build\hg\hg-python\lib\site-packages\hgext3rd\fbsparse.py", line 1116, in _refresh
mergemod.applyupdates(repo, typeactions, repo[None], repo['.'], False)
File "C:\open\facebook-hg-rpms\build\hg\hg-python\lib\site-packages\remotefilelog\__init__.py", line 311, in applyupdates
return orig(repo, actions, wctx, mctx, overwrite, labels=labels)
File "C:\open\facebook-hg-rpms\build\hg\mercurial\merge.py", line 1464, in applyupdates
for i, item in prog:
File "C:\open\facebook-hg-rpms\build\hg\mercurial\worker.py", line 286, in _windowsworker
raise t.exception
Exception: Forged exception
PS C:\open\ovrsource>
Differential Revision: https://phab.mercurial-scm.org/D1459
author | Wojciech Lis <wlis@fb.com> |
---|---|
date | Mon, 20 Nov 2017 10:27:41 -0800 |
parents | ed1f376090cd |
children | 89793289c891 |
rev | line source |
---|---|
7344
58fd3c718ca4
tests: add killdaemons helper script
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
58fd3c718ca4
tests: add killdaemons helper script
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
2 |
28942
05cb9c6f310e
py3: use absolute_import in killdaemons.py
Robert Stanca <robert.stanca7@gmail.com>
parents:
25473
diff
changeset
|
3 from __future__ import absolute_import |
05cb9c6f310e
py3: use absolute_import in killdaemons.py
Robert Stanca <robert.stanca7@gmail.com>
parents:
25473
diff
changeset
|
4 import errno |
05cb9c6f310e
py3: use absolute_import in killdaemons.py
Robert Stanca <robert.stanca7@gmail.com>
parents:
25473
diff
changeset
|
5 import os |
05cb9c6f310e
py3: use absolute_import in killdaemons.py
Robert Stanca <robert.stanca7@gmail.com>
parents:
25473
diff
changeset
|
6 import signal |
05cb9c6f310e
py3: use absolute_import in killdaemons.py
Robert Stanca <robert.stanca7@gmail.com>
parents:
25473
diff
changeset
|
7 import sys |
05cb9c6f310e
py3: use absolute_import in killdaemons.py
Robert Stanca <robert.stanca7@gmail.com>
parents:
25473
diff
changeset
|
8 import time |
7344
58fd3c718ca4
tests: add killdaemons helper script
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
9 |
17465
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
10 if os.name =='nt': |
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
11 import ctypes |
20493
b5f43dbf64ca
tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents:
17466
diff
changeset
|
12 |
32857
d644e859d9da
killdaemons: explicitly set the ctypes signatures
Matt Harbison <matt_harbison@yahoo.com>
parents:
32677
diff
changeset
|
13 _BOOL = ctypes.c_long |
d644e859d9da
killdaemons: explicitly set the ctypes signatures
Matt Harbison <matt_harbison@yahoo.com>
parents:
32677
diff
changeset
|
14 _DWORD = ctypes.c_ulong |
d644e859d9da
killdaemons: explicitly set the ctypes signatures
Matt Harbison <matt_harbison@yahoo.com>
parents:
32677
diff
changeset
|
15 _UINT = ctypes.c_uint |
d644e859d9da
killdaemons: explicitly set the ctypes signatures
Matt Harbison <matt_harbison@yahoo.com>
parents:
32677
diff
changeset
|
16 _HANDLE = ctypes.c_void_p |
d644e859d9da
killdaemons: explicitly set the ctypes signatures
Matt Harbison <matt_harbison@yahoo.com>
parents:
32677
diff
changeset
|
17 |
d644e859d9da
killdaemons: explicitly set the ctypes signatures
Matt Harbison <matt_harbison@yahoo.com>
parents:
32677
diff
changeset
|
18 ctypes.windll.kernel32.CloseHandle.argtypes = [_HANDLE] |
d644e859d9da
killdaemons: explicitly set the ctypes signatures
Matt Harbison <matt_harbison@yahoo.com>
parents:
32677
diff
changeset
|
19 ctypes.windll.kernel32.CloseHandle.restype = _BOOL |
d644e859d9da
killdaemons: explicitly set the ctypes signatures
Matt Harbison <matt_harbison@yahoo.com>
parents:
32677
diff
changeset
|
20 |
d644e859d9da
killdaemons: explicitly set the ctypes signatures
Matt Harbison <matt_harbison@yahoo.com>
parents:
32677
diff
changeset
|
21 ctypes.windll.kernel32.GetLastError.argtypes = [] |
d644e859d9da
killdaemons: explicitly set the ctypes signatures
Matt Harbison <matt_harbison@yahoo.com>
parents:
32677
diff
changeset
|
22 ctypes.windll.kernel32.GetLastError.restype = _DWORD |
d644e859d9da
killdaemons: explicitly set the ctypes signatures
Matt Harbison <matt_harbison@yahoo.com>
parents:
32677
diff
changeset
|
23 |
d644e859d9da
killdaemons: explicitly set the ctypes signatures
Matt Harbison <matt_harbison@yahoo.com>
parents:
32677
diff
changeset
|
24 ctypes.windll.kernel32.OpenProcess.argtypes = [_DWORD, _BOOL, _DWORD] |
d644e859d9da
killdaemons: explicitly set the ctypes signatures
Matt Harbison <matt_harbison@yahoo.com>
parents:
32677
diff
changeset
|
25 ctypes.windll.kernel32.OpenProcess.restype = _HANDLE |
d644e859d9da
killdaemons: explicitly set the ctypes signatures
Matt Harbison <matt_harbison@yahoo.com>
parents:
32677
diff
changeset
|
26 |
d644e859d9da
killdaemons: explicitly set the ctypes signatures
Matt Harbison <matt_harbison@yahoo.com>
parents:
32677
diff
changeset
|
27 ctypes.windll.kernel32.TerminateProcess.argtypes = [_HANDLE, _UINT] |
d644e859d9da
killdaemons: explicitly set the ctypes signatures
Matt Harbison <matt_harbison@yahoo.com>
parents:
32677
diff
changeset
|
28 ctypes.windll.kernel32.TerminateProcess.restype = _BOOL |
d644e859d9da
killdaemons: explicitly set the ctypes signatures
Matt Harbison <matt_harbison@yahoo.com>
parents:
32677
diff
changeset
|
29 |
d644e859d9da
killdaemons: explicitly set the ctypes signatures
Matt Harbison <matt_harbison@yahoo.com>
parents:
32677
diff
changeset
|
30 ctypes.windll.kernel32.WaitForSingleObject.argtypes = [_HANDLE, _DWORD] |
d644e859d9da
killdaemons: explicitly set the ctypes signatures
Matt Harbison <matt_harbison@yahoo.com>
parents:
32677
diff
changeset
|
31 ctypes.windll.kernel32.WaitForSingleObject.restype = _DWORD |
d644e859d9da
killdaemons: explicitly set the ctypes signatures
Matt Harbison <matt_harbison@yahoo.com>
parents:
32677
diff
changeset
|
32 |
20493
b5f43dbf64ca
tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents:
17466
diff
changeset
|
33 def _check(ret, expectederr=None): |
b5f43dbf64ca
tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents:
17466
diff
changeset
|
34 if ret == 0: |
b5f43dbf64ca
tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents:
17466
diff
changeset
|
35 winerrno = ctypes.GetLastError() |
b5f43dbf64ca
tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents:
17466
diff
changeset
|
36 if winerrno == expectederr: |
b5f43dbf64ca
tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents:
17466
diff
changeset
|
37 return True |
b5f43dbf64ca
tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents:
17466
diff
changeset
|
38 raise ctypes.WinError(winerrno) |
b5f43dbf64ca
tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents:
17466
diff
changeset
|
39 |
17465
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
40 def kill(pid, logfn, tryhard=True): |
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
41 logfn('# Killing daemon process %d' % pid) |
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
42 PROCESS_TERMINATE = 1 |
20496
acbd19b9fbe1
tests: killdaemons.py for windows distinguishes access violation and terminated
Simon Heimberg <simohe@besonet.ch>
parents:
20495
diff
changeset
|
43 PROCESS_QUERY_INFORMATION = 0x400 |
20698
1147563faf62
killdaemons: drop superfluous L suffix from constant
Augie Fackler <raf@durin42.com>
parents:
20496
diff
changeset
|
44 SYNCHRONIZE = 0x00100000 |
20494
ea3adeac5248
tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents:
20493
diff
changeset
|
45 WAIT_OBJECT_0 = 0 |
ea3adeac5248
tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents:
20493
diff
changeset
|
46 WAIT_TIMEOUT = 258 |
32858
ed1f376090cd
killdaemons: fix WaitForSingleObject() error handling logic on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
32857
diff
changeset
|
47 WAIT_FAILED = _DWORD(0xFFFFFFFF).value |
17465
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
48 handle = ctypes.windll.kernel32.OpenProcess( |
20496
acbd19b9fbe1
tests: killdaemons.py for windows distinguishes access violation and terminated
Simon Heimberg <simohe@besonet.ch>
parents:
20495
diff
changeset
|
49 PROCESS_TERMINATE|SYNCHRONIZE|PROCESS_QUERY_INFORMATION, |
acbd19b9fbe1
tests: killdaemons.py for windows distinguishes access violation and terminated
Simon Heimberg <simohe@besonet.ch>
parents:
20495
diff
changeset
|
50 False, pid) |
32857
d644e859d9da
killdaemons: explicitly set the ctypes signatures
Matt Harbison <matt_harbison@yahoo.com>
parents:
32677
diff
changeset
|
51 if handle is None: |
20495
0cf1c8c452d3
tests: killdaemons.py for checks reason when getting no process handle
Simon Heimberg <simohe@besonet.ch>
parents:
20494
diff
changeset
|
52 _check(0, 87) # err 87 when process not found |
20493
b5f43dbf64ca
tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents:
17466
diff
changeset
|
53 return # process not found, already finished |
b5f43dbf64ca
tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents:
17466
diff
changeset
|
54 try: |
20496
acbd19b9fbe1
tests: killdaemons.py for windows distinguishes access violation and terminated
Simon Heimberg <simohe@besonet.ch>
parents:
20495
diff
changeset
|
55 r = ctypes.windll.kernel32.WaitForSingleObject(handle, 100) |
acbd19b9fbe1
tests: killdaemons.py for windows distinguishes access violation and terminated
Simon Heimberg <simohe@besonet.ch>
parents:
20495
diff
changeset
|
56 if r == WAIT_OBJECT_0: |
acbd19b9fbe1
tests: killdaemons.py for windows distinguishes access violation and terminated
Simon Heimberg <simohe@besonet.ch>
parents:
20495
diff
changeset
|
57 pass # terminated, but process handle still available |
acbd19b9fbe1
tests: killdaemons.py for windows distinguishes access violation and terminated
Simon Heimberg <simohe@besonet.ch>
parents:
20495
diff
changeset
|
58 elif r == WAIT_TIMEOUT: |
acbd19b9fbe1
tests: killdaemons.py for windows distinguishes access violation and terminated
Simon Heimberg <simohe@besonet.ch>
parents:
20495
diff
changeset
|
59 _check(ctypes.windll.kernel32.TerminateProcess(handle, -1)) |
32858
ed1f376090cd
killdaemons: fix WaitForSingleObject() error handling logic on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
32857
diff
changeset
|
60 elif r == WAIT_FAILED: |
ed1f376090cd
killdaemons: fix WaitForSingleObject() error handling logic on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
32857
diff
changeset
|
61 _check(0) # err stored in GetLastError() |
20494
ea3adeac5248
tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents:
20493
diff
changeset
|
62 |
ea3adeac5248
tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents:
20493
diff
changeset
|
63 # TODO?: forcefully kill when timeout |
ea3adeac5248
tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents:
20493
diff
changeset
|
64 # and ?shorter waiting time? when tryhard==True |
ea3adeac5248
tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents:
20493
diff
changeset
|
65 r = ctypes.windll.kernel32.WaitForSingleObject(handle, 100) |
ea3adeac5248
tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents:
20493
diff
changeset
|
66 # timeout = 100 ms |
ea3adeac5248
tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents:
20493
diff
changeset
|
67 if r == WAIT_OBJECT_0: |
ea3adeac5248
tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents:
20493
diff
changeset
|
68 pass # process is terminated |
ea3adeac5248
tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents:
20493
diff
changeset
|
69 elif r == WAIT_TIMEOUT: |
ea3adeac5248
tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents:
20493
diff
changeset
|
70 logfn('# Daemon process %d is stuck') |
32858
ed1f376090cd
killdaemons: fix WaitForSingleObject() error handling logic on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
32857
diff
changeset
|
71 elif r == WAIT_FAILED: |
ed1f376090cd
killdaemons: fix WaitForSingleObject() error handling logic on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
32857
diff
changeset
|
72 _check(0) # err stored in GetLastError() |
20493
b5f43dbf64ca
tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents:
17466
diff
changeset
|
73 except: #re-raises |
b5f43dbf64ca
tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents:
17466
diff
changeset
|
74 ctypes.windll.kernel32.CloseHandle(handle) # no _check, keep error |
b5f43dbf64ca
tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents:
17466
diff
changeset
|
75 raise |
b5f43dbf64ca
tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents:
17466
diff
changeset
|
76 _check(ctypes.windll.kernel32.CloseHandle(handle)) |
b5f43dbf64ca
tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents:
17466
diff
changeset
|
77 |
17465
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
78 else: |
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
79 def kill(pid, logfn, tryhard=True): |
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
80 try: |
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
81 os.kill(pid, 0) |
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
82 logfn('# Killing daemon process %d' % pid) |
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
83 os.kill(pid, signal.SIGTERM) |
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
84 if tryhard: |
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
85 for i in range(10): |
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
86 time.sleep(0.05) |
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
87 os.kill(pid, 0) |
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
88 else: |
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
89 time.sleep(0.1) |
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
90 os.kill(pid, 0) |
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
91 logfn('# Daemon process %d is stuck - really killing it' % pid) |
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
92 os.kill(pid, signal.SIGKILL) |
25031
0adc22a0b6b3
python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents:
21194
diff
changeset
|
93 except OSError as err: |
17465
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
94 if err.errno != errno.ESRCH: |
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
95 raise |
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
96 |
17464
eddfb9a550d0
run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents:
10905
diff
changeset
|
97 def killdaemons(pidfile, tryhard=True, remove=False, logfn=None): |
eddfb9a550d0
run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents:
10905
diff
changeset
|
98 if not logfn: |
eddfb9a550d0
run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents:
10905
diff
changeset
|
99 logfn = lambda s: s |
eddfb9a550d0
run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents:
10905
diff
changeset
|
100 # Kill off any leftover daemon processes |
eddfb9a550d0
run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents:
10905
diff
changeset
|
101 try: |
32677
f840b2621cce
killdaemons: close pid file before killing processes
Matt Harbison <matt_harbison@yahoo.com>
parents:
29811
diff
changeset
|
102 pids = [] |
f840b2621cce
killdaemons: close pid file before killing processes
Matt Harbison <matt_harbison@yahoo.com>
parents:
29811
diff
changeset
|
103 with open(pidfile) as fp: |
f840b2621cce
killdaemons: close pid file before killing processes
Matt Harbison <matt_harbison@yahoo.com>
parents:
29811
diff
changeset
|
104 for line in fp: |
f840b2621cce
killdaemons: close pid file before killing processes
Matt Harbison <matt_harbison@yahoo.com>
parents:
29811
diff
changeset
|
105 try: |
f840b2621cce
killdaemons: close pid file before killing processes
Matt Harbison <matt_harbison@yahoo.com>
parents:
29811
diff
changeset
|
106 pid = int(line) |
f840b2621cce
killdaemons: close pid file before killing processes
Matt Harbison <matt_harbison@yahoo.com>
parents:
29811
diff
changeset
|
107 if pid <= 0: |
f840b2621cce
killdaemons: close pid file before killing processes
Matt Harbison <matt_harbison@yahoo.com>
parents:
29811
diff
changeset
|
108 raise ValueError |
f840b2621cce
killdaemons: close pid file before killing processes
Matt Harbison <matt_harbison@yahoo.com>
parents:
29811
diff
changeset
|
109 except ValueError: |
f840b2621cce
killdaemons: close pid file before killing processes
Matt Harbison <matt_harbison@yahoo.com>
parents:
29811
diff
changeset
|
110 logfn('# Not killing daemon process %s - invalid pid' |
f840b2621cce
killdaemons: close pid file before killing processes
Matt Harbison <matt_harbison@yahoo.com>
parents:
29811
diff
changeset
|
111 % line.rstrip()) |
f840b2621cce
killdaemons: close pid file before killing processes
Matt Harbison <matt_harbison@yahoo.com>
parents:
29811
diff
changeset
|
112 continue |
f840b2621cce
killdaemons: close pid file before killing processes
Matt Harbison <matt_harbison@yahoo.com>
parents:
29811
diff
changeset
|
113 pids.append(pid) |
f840b2621cce
killdaemons: close pid file before killing processes
Matt Harbison <matt_harbison@yahoo.com>
parents:
29811
diff
changeset
|
114 for pid in pids: |
17465
2d4a096e213c
killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents:
17464
diff
changeset
|
115 kill(pid, logfn, tryhard) |
17464
eddfb9a550d0
run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents:
10905
diff
changeset
|
116 if remove: |
eddfb9a550d0
run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents:
10905
diff
changeset
|
117 os.unlink(pidfile) |
eddfb9a550d0
run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents:
10905
diff
changeset
|
118 except IOError: |
eddfb9a550d0
run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents:
10905
diff
changeset
|
119 pass |
eddfb9a550d0
run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents:
10905
diff
changeset
|
120 |
eddfb9a550d0
run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents:
10905
diff
changeset
|
121 if __name__ == '__main__': |
25473
123c99034cb6
tests: make killdaemons.py use DAEMON_PIDS by default
Matt Mackall <mpm@selenic.com>
parents:
25031
diff
changeset
|
122 if len(sys.argv) > 1: |
123c99034cb6
tests: make killdaemons.py use DAEMON_PIDS by default
Matt Mackall <mpm@selenic.com>
parents:
25031
diff
changeset
|
123 path, = sys.argv[1:] |
123c99034cb6
tests: make killdaemons.py use DAEMON_PIDS by default
Matt Mackall <mpm@selenic.com>
parents:
25031
diff
changeset
|
124 else: |
123c99034cb6
tests: make killdaemons.py use DAEMON_PIDS by default
Matt Mackall <mpm@selenic.com>
parents:
25031
diff
changeset
|
125 path = os.environ["DAEMON_PIDS"] |
123c99034cb6
tests: make killdaemons.py use DAEMON_PIDS by default
Matt Mackall <mpm@selenic.com>
parents:
25031
diff
changeset
|
126 |
17466
d5a3bda6e170
killdaemons: take file argument explicitely
Patrick Mezard <patrick@mezard.eu>
parents:
17465
diff
changeset
|
127 killdaemons(path) |