Fix anvil process call to null

This commit is contained in:
stubbsta 2025-06-10 12:31:42 +02:00
parent 17cf587ce6
commit f8b9f81bbb

View File

@ -603,31 +603,40 @@ proc stopAnvil*(runAnvil: Process) {.used.} =
return return
try: try:
# Close Process object to release resources # Try to reap any zombie process first, before closing
close(runAnvil)
sleep(500)
if isAnvilProcessRunning(anvilPID): if isAnvilProcessRunning(anvilPID):
when not defined(windows): let (psOutput, _) = execCmdEx(fmt"ps -p {anvilPID} -o stat= 2>/dev/null")
# Try SIGTERM first if "Z" in psOutput:
discard execCmdEx(fmt"kill -TERM {anvilPID}") try:
discard waitForExit(runAnvil)
debug "Reaped zombie process before cleanup", anvilPID = anvilPID
return
except:
discard
# Send termination signals before closing Process object
when not defined(windows):
# Try SIGTERM first
discard execCmdEx(fmt"kill -TERM {anvilPID}")
sleep(1000)
if isAnvilProcessRunning(anvilPID):
# Try SIGKILL
discard execCmdEx(fmt"kill -9 {anvilPID}")
sleep(1000) sleep(1000)
else:
discard execCmdEx(fmt"taskkill /F /PID {anvilPID}")
sleep(1000)
if isAnvilProcessRunning(anvilPID): # Close Process object to release resources after sending signals
# Try SIGKILL close(runAnvil)
discard execCmdEx(fmt"kill -9 {anvilPID}")
sleep(1000)
if isAnvilProcessRunning(anvilPID): # Final check and zombie reaping if needed
# Check if it's a zombie process and reap it if isAnvilProcessRunning(anvilPID):
let (psOutput, _) = execCmdEx(fmt"ps -p {anvilPID} -o stat= 2>/dev/null") let (psOutput, _) = execCmdEx(fmt"ps -p {anvilPID} -o stat= 2>/dev/null")
if "Z" in psOutput: if "Z" in psOutput:
try: debug "Process became zombie after signals, will be cleaned up by OS",
discard waitForExit(runAnvil) anvilPID = anvilPID
except:
discard # Process cleanup will happen when parent exits
else:
discard execCmdEx(fmt"taskkill /F /PID {anvilPID}")
debug "Anvil daemon stopped", anvilPID = anvilPID debug "Anvil daemon stopped", anvilPID = anvilPID
except Exception as e: except Exception as e: