prevent errors during node shutdown

This commit is contained in:
Eric 2025-01-09 15:15:49 +11:00
parent 82e0d81d61
commit d5e3d57b5a
No known key found for this signature in database

View File

@ -128,17 +128,28 @@ method stop*(node: NodeProcess) {.base, async.} =
error "failed to terminate process", errCode = $errCode error "failed to terminate process", errCode = $errCode
trace "waiting for node process to exit" trace "waiting for node process to exit"
let exitCode = await node.process.waitForExit(3.seconds) var backoff = 8
if exitCode > 0: while node.process.running().valueOr false:
backoff = min(backoff*2, 1024) # Exponential backoff
await sleepAsync(backoff)
let exitCode = node.process.peekExitCode().valueOr:
fatal "could not get exit code from process", error
return
if exitCode > 0 and exitCode != 143: # 143 = SIGTERM (initiated above)
error "failed to exit process, check for zombies", exitCode error "failed to exit process, check for zombies", exitCode
trace "closing node process' streams"
await node.process.closeWait()
except CancelledError as error: except CancelledError as error:
raise error raise error
except CatchableError as e: except CatchableError as e:
error "error stopping node process", error = e.msg error "error stopping node process", error = e.msg
finally: finally:
try:
trace "closing node process' streams"
await node.process.closeWait()
except:
discard
node.process = nil node.process = nil
trace "node stopped" trace "node stopped"