Skip to content

Commit

Permalink
Do not freeze when reading child stdout [fix]
Browse files Browse the repository at this point in the history
  • Loading branch information
hoijui committed May 5, 2023
1 parent 34a1d9b commit cc2c86d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/checks/okh_lint.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import ../state
import ../tools
import std/logging
import std/osproc
import std/streams
import std/strutils
import ./okh_file_exists

Expand Down Expand Up @@ -69,6 +70,8 @@ method run*(this: OkhLintCheck, state: var State): CheckResult =
args = ["val", "--recursive", "--okh-version", "losh", "."],
env = nil,
options = {poUsePath})
process.inputStream.close() # NOTE **Essential** - This prevents hanging/freezing when reading stdout below
process.errorStream.close() # NOTE **Essential** - This prevents hanging/freezing when reading stdout below
let (lines, exCode) = process.readLines()
process.close()
debug fmt"'{OKH_CMD}' run done."
Expand Down
3 changes: 3 additions & 0 deletions src/checks/reuse_lint.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import ../state
import ../tools
import std/logging
import std/osproc
import std/streams

const REUSE_CMD = "reuse"
const REUSE_TOOL_URL = "https://reuse.software/"
Expand Down Expand Up @@ -77,6 +78,8 @@ method run*(this: ReuseLintCheck, state: var State): CheckResult =
args = ["lint"],
env = nil,
options = {poUsePath})
process.inputStream.close() # NOTE **Essential** - This prevents hanging/freezing when reading stdout below
process.errorStream.close() # NOTE **Essential** - This prevents hanging/freezing when reading stdout below
let (lines, exCode) = process.readLines()
process.close()
debug fmt"'{REUSE_CMD}' run done."
Expand Down
11 changes: 9 additions & 2 deletions src/tools.nim
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ proc toolVersion*(binName: string, args: varargs[string]): string =
args = args.toSeq(),
env = newStringTable(), # nil => inherit from parent process
options = {poUsePath}) # NOTE Add for debugging: poParentStreams
process.inputStream.close() # NOTE **Essential** - This prevents hanging/freezing when reading stdout below
process.errorStream.close() # NOTE **Essential** - This prevents hanging/freezing when reading stdout below
let (lines, exCode) = process.readLines()
process.close()
debug fmt"'{binName}' version check done."
Expand Down Expand Up @@ -226,7 +228,10 @@ proc runProjvar*(projRoot: string) : TableRef[string, string] =
env = nil, # nil => inherit from parent process
options = {poUsePath, poParentStreams}) # NOTE Add for debugging: poParentStreams
debug "Waiting for 'projvar' run to end ..."
process.inputStream.close() # NOTE **Essential** - This prevents hanging/freezing when reading stdout below
process.errorStream.close() # NOTE **Essential** - This prevents hanging/freezing when reading stdout below
let exCode = osproc.waitForExit(process)
process.close()
debug fmt"'{PROJVAR_CMD}' run done."
if exCode == 0:
let jsonRoot = parseJson(newFileStream(outFilePath), outFilePath)
Expand Down Expand Up @@ -254,9 +259,9 @@ proc runOshDirStd*(projRoot: string, args: openArray[string], fileListing: seq[s
for path in fileListing:
procStdin.writeLine(path)
debug fmt" {OSH_DIR_STD_TOOL_CMD}: Close stdin (we supposedly should not do this manually, but apparently we have to!) ..."
procStdin.close()
procStdin.close() # NOTE **Essential** - This prevents hanging/freezing when reading stdout below
debug fmt" {OSH_DIR_STD_TOOL_CMD}: And in some cases, this is required to not hang (closing stderr) ... :/"
process.errorStream.close()
process.errorStream.close() # NOTE **Essential** - This prevents hanging/freezing when reading stdout below
debug fmt" {OSH_DIR_STD_TOOL_CMD}: Ask for exit code and stdout ..."
let (lines, exCode) = process.readLines()
process.close()
Expand Down Expand Up @@ -287,6 +292,8 @@ proc extractMarkdownLinks*(config: RunConfig, mdFiles: seq[string]) : LinkOccsCo
args = args,
env = nil,
options = {poUsePath}) # NOTE Add for debugging: poParentStreams
process.inputStream.close() # NOTE **Essential** - This prevents hanging/freezing when reading stdout below
process.errorStream.close() # NOTE **Essential** - This prevents hanging/freezing when reading stdout below
let (lines, exCode) = process.readLines()
process.close()
debug fmt"'{MLE_CMD}' run done."
Expand Down

0 comments on commit cc2c86d

Please sign in to comment.