Skip to content

Commit

Permalink
Fix service.terminate_process return value documentation and prevent …
Browse files Browse the repository at this point in the history
…it from throwing psutil.NoSuchProcess
  • Loading branch information
themylogin committed Jul 20, 2021
1 parent 6de9cda commit 02ca757
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/middlewared/middlewared/plugins/service.py
Expand Up @@ -302,27 +302,31 @@ async def identify_process(self, procname):
return service_name

@accepts(Int("pid"), Int("timeout", default=10))
@returns(Bool('process_terminated'))
@returns(Bool(
"process_terminated_nicely",
description="`true` is process has been successfully terminated with `TERM` and `false` if we had to use `KILL`"
))
def terminate_process(self, pid, timeout):
"""
Terminate process by `pid`.
First send `TERM` signal, then, if was not terminated in `timeout` seconds, send `KILL` signal.
Returns `true` is process has been successfully terminated with `TERM` and `false` if we had to use `KILL`.
"""
try:
process = psutil.Process(pid)
process.terminate()
gone, alive = psutil.wait_procs([process], timeout)
except psutil.NoSuchProcess:
raise CallError("Process does not exist")

process.terminate()

gone, alive = psutil.wait_procs([process], timeout)
if not alive:
return True

alive[0].kill()
try:
alive[0].kill()
except psutil.NoSuchProcess:
return True

return False


Expand Down

0 comments on commit 02ca757

Please sign in to comment.