Skip to content

Commit 7e72570

Browse files
committed
batchspawner/batchspawner: Fix exception handling in run_command.
The exception handling in run_command had several issues which this commit fixes: - Don't use undefined variable. - communicate() may block forever if process already closed the pipes. - exceptions during exception handling were not handled.
1 parent ab0e00e commit 7e72570

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

batchspawner/batchspawner.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -206,18 +206,25 @@ async def run_command(self, cmd, input=None, env=None):
206206
try:
207207
out, eout = await proc.communicate(input=inbytes)
208208
except:
209-
self.log.debug("Exception raised when trying to run command: %s" % command)
209+
self.log.debug("Exception raised when trying to run command: %s" % cmd)
210210
proc.kill()
211-
self.log.debug("Running command failed done kill")
212-
out, eout = await proc.communicate()
213-
out = out.decode.strip()
214-
eout = eout.decode.strip()
215-
self.log.error("Subprocess returned exitcode %s" % proc.returncode)
216-
self.log.error('Stdout:')
217-
self.log.error(out)
218-
self.log.error('Stderr:')
219-
self.log.error(eout)
220-
raise RuntimeError('{} exit status {}: {}'.format(cmd, proc.returncode, eout))
211+
self.log.debug("Running command failed, killed process.")
212+
try:
213+
out, eout = await asyncio.wait_for(proc.communicate(), timeout=2)
214+
out = out.decode().strip()
215+
eout = eout.decode().strip()
216+
self.log.error("Subprocess returned exitcode %s" % proc.returncode)
217+
self.log.error('Stdout:')
218+
self.log.error(out)
219+
self.log.error('Stderr:')
220+
self.log.error(eout)
221+
raise RuntimeError('{} exit status {}: {}'.format(cmd, proc.returncode, eout))
222+
except asyncio.TimeoutError:
223+
self.log.error('Encountered timeout trying to clean up command, process probably killed already: %s' % cmd)
224+
return ""
225+
except:
226+
self.log.error('Encountered exception trying to clean up command: %s' % cmd)
227+
raise
221228
else:
222229
eout = eout.decode().strip()
223230
err = proc.returncode

0 commit comments

Comments
 (0)