Skip to content

Commit 9ecccb6

Browse files
authored
Merge pull request #195 from unibonn/fix-command-exception-handling
batchspawner/batchspawner: Fix exception handling in run_command.
2 parents 0f7ce8d + 7e72570 commit 9ecccb6

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
@@ -213,18 +213,25 @@ async def run_command(self, cmd, input=None, env=None):
213213
try:
214214
out, eout = await proc.communicate(input=inbytes)
215215
except:
216-
self.log.debug("Exception raised when trying to run command: %s" % command)
216+
self.log.debug("Exception raised when trying to run command: %s" % cmd)
217217
proc.kill()
218-
self.log.debug("Running command failed done kill")
219-
out, eout = await proc.communicate()
220-
out = out.decode.strip()
221-
eout = eout.decode.strip()
222-
self.log.error("Subprocess returned exitcode %s" % proc.returncode)
223-
self.log.error('Stdout:')
224-
self.log.error(out)
225-
self.log.error('Stderr:')
226-
self.log.error(eout)
227-
raise RuntimeError('{} exit status {}: {}'.format(cmd, proc.returncode, eout))
218+
self.log.debug("Running command failed, killed process.")
219+
try:
220+
out, eout = await asyncio.wait_for(proc.communicate(), timeout=2)
221+
out = out.decode().strip()
222+
eout = eout.decode().strip()
223+
self.log.error("Subprocess returned exitcode %s" % proc.returncode)
224+
self.log.error('Stdout:')
225+
self.log.error(out)
226+
self.log.error('Stderr:')
227+
self.log.error(eout)
228+
raise RuntimeError('{} exit status {}: {}'.format(cmd, proc.returncode, eout))
229+
except asyncio.TimeoutError:
230+
self.log.error('Encountered timeout trying to clean up command, process probably killed already: %s' % cmd)
231+
return ""
232+
except:
233+
self.log.error('Encountered exception trying to clean up command: %s' % cmd)
234+
raise
228235
else:
229236
eout = eout.decode().strip()
230237
err = proc.returncode

0 commit comments

Comments
 (0)