Skip to content

Commit 19b344e

Browse files
authored
Improves process handling (#282)
1 parent f5c52e4 commit 19b344e

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

nimcheck.nim

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,4 @@ proc nimCheck*(filePath: string, nimPath: string): Future[seq[CheckResult]] {.as
9999
parseCheckResults(lines)
100100

101101
finally:
102-
if not process.isNil:
103-
discard process.kill()
102+
await shutdownChildProcess(process)

nimexpand.nim

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ proc nimExpandMacro*(nimPath: string, suggest: Suggest, filePath: string): Futur
4444
let output = string.fromBytes(process.stdoutStream.read().await)
4545
result = extractMacroExpansion(output, line)
4646
finally:
47-
if not process.isNil:
48-
discard process.kill()
47+
await shutdownChildProcess(process)
4948

5049

5150
proc extractArcExpansion*(output: string, procName: string): string =
@@ -79,8 +78,7 @@ proc nimExpandArc*(nimPath: string, suggest: Suggest, filePath: string): Future[
7978
result = extractArcExpansion(output, procName)
8079
# debug "nimExpandArc", output = output, result = result
8180
if result.len == 0:
82-
result = &"#Couldnt expand arc for `{procName}`. Showing raw output instead (nimPath). \n"
81+
result = &"#Couldnt expand arc for `{procName}`. Showing raw output instead \n"
8382
result.add output
8483
finally:
85-
if not process.isNil:
86-
discard process.kill()
84+
await shutdownChildProcess(process)

suggestapi.nim

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,7 @@ proc markFailed(self: Project, errMessage: string) {.raises: [].} =
268268

269269
proc stop*(self: Project) =
270270
debug "Stopping nimsuggest for ", root = self.file
271-
try:
272-
let res = self.process.kill()
273-
debug "Stopped nimsuggest ", res = res
274-
except Exception as ex:
275-
writeStackTrace(ex)
271+
asyncSpawn shutdownChildProcess(self.process)
276272

277273
proc doWithTimeout*[T](fut: Future[T], timeout: int, s: string): owned(Future[bool]) =
278274
var retFuture = newFuture[bool]("asyncdispatch.`doWithTimeout`")

utils.nim

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import std/[unicode, uri, strformat, os, strutils, options, json, jsonutils, sugar, net]
2-
import chronos, chronicles
2+
import chronos, chronicles, chronos/asyncproc
33
import "$nim/compiler/pathutils"
44
import json_rpc/private/jrpc_sys
55
type
@@ -339,4 +339,15 @@ proc getNimScriptAPITemplatePath*(): string =
339339

340340
if not result.fileExists:
341341
writeFile(result, NIM_SCRIPT_API_TEMPLATE)
342-
debug "NimScriptApiPath", path = result
342+
debug "NimScriptApiPath", path = result
343+
344+
proc shutdownChildProcess*(p: AsyncProcessRef): Future[void] {.async.} =
345+
try:
346+
let exitCode = await p.terminateAndWaitForExit(2.seconds) # debug "Process terminated with exit code: ", exitCode
347+
except CatchableError:
348+
try:
349+
let forcedExitCode = await p.killAndWaitForExit(3.seconds)
350+
debug "Process forcibly killed with exit code: ", exitCode = forcedExitCode
351+
except CatchableError:
352+
debug "Could not kill process in time either!"
353+
writeStackTrace()

0 commit comments

Comments
 (0)