Skip to content

Commit

Permalink
temp fix for slow shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Mar 22, 2024
1 parent 614bcc7 commit 1e2524c
Showing 1 changed file with 43 additions and 11 deletions.
54 changes: 43 additions & 11 deletions src/misc/async_process.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,9 @@ proc destroy*[T](channel: AsyncChannel[T]) =
channel.chan.deallocShared
channel.chan = nil

proc destroy*(process: AsyncProcess) =
process.dontRestart = true

if not process.process.isNil:
process.process.terminate()

process.inputStreamChannel[].send nil
process.outputStreamChannel[].send nil
process.errorStreamChannel[].send nil
proc destroyProcess2(process: ptr AsyncProcess) =
# todo: probably needs a lock for the process RC
let process = process[]

if not process.readerFlowVar.isNil:
blockUntil process.readerFlowVar[]
Expand All @@ -65,6 +59,44 @@ proc destroy*(process: AsyncProcess) =
process.outputStreamChannel.deallocShared
process.serverDiedNotifications.deallocShared

proc destroy*(process: AsyncProcess) =
log lvlInfo, fmt"Destroying process {process.name}"
process.dontRestart = true

if not process.process.isNil:
process.process.terminate()

process.inputStreamChannel[].send nil
process.outputStreamChannel[].send nil
process.errorStreamChannel[].send nil

spawn destroyProcess2(process.addr)
# todo: should probably wait for the other thread to increment the process RC

# if not process.readerFlowVar.isNil:
# debugf"wait for reader"
# blockUntil process.readerFlowVar[]
# if not process.writerFlowVar.isNil:
# debugf"wait for writer"
# blockUntil process.writerFlowVar[]
# if not process.errorReaderFlowVar.isNil:
# debugf"wait for error reader"
# blockUntil process.errorReaderFlowVar[]

# debugf"close channels"
# process.inputStreamChannel[].close()
# process.errorStreamChannel[].close()
# process.outputStreamChannel[].close()
# process.serverDiedNotifications[].close()
# debugf"destroy channels"
# process.input.destroy()
# process.error.destroy()
# process.output.destroy()
# process.inputStreamChannel.deallocShared
# process.errorStreamChannel.deallocShared
# process.outputStreamChannel.deallocShared
# process.serverDiedNotifications.deallocShared

proc recv*[T: char](achan: AsyncChannel[T], amount: int): Future[string] {.async.} =
var buffer = ""
while buffer.len < amount:
Expand Down Expand Up @@ -157,7 +189,7 @@ proc readInput(chan: ptr Channel[Stream], serverDiedNotifications: ptr Channel[b
serverDiedNotifications[].send true
break
except:
echo &"readInput: {getCurrentExceptionMsg()}\n{getCurrentException().getStackTrace()}"
# echo &"readInput: {getCurrentExceptionMsg()}\n{getCurrentException().getStackTrace()}"
break

return true
Expand Down Expand Up @@ -188,7 +220,7 @@ proc writeOutput(chan: ptr Channel[Stream], data: ptr Channel[Option[string]]):

except:
# echo "ioerror"
echo &"writeOutput: {getCurrentExceptionMsg()}\n{getCurrentException().getStackTrace()}"
# echo &"writeOutput: {getCurrentExceptionMsg()}\n{getCurrentException().getStackTrace()}"
break

return true
Expand Down

0 comments on commit 1e2524c

Please sign in to comment.