osproc: fix double close on POSIX (#5724)
Calling close() in some cases issued two close() syscalls to one FD, which is incorrect in multithreaded programs.
This commit is contained in:
parent
412cd61dab
commit
b2060acbc4
2 changed files with 38 additions and 6 deletions
|
|
@ -1001,12 +1001,20 @@ elif not defined(useNimRtl):
|
||||||
{.pop}
|
{.pop}
|
||||||
|
|
||||||
proc close(p: Process) =
|
proc close(p: Process) =
|
||||||
if p.inStream != nil: close(p.inStream)
|
|
||||||
if p.outStream != nil: close(p.outStream)
|
|
||||||
if p.errStream != nil: close(p.errStream)
|
|
||||||
if poParentStreams notin p.options:
|
if poParentStreams notin p.options:
|
||||||
|
if p.inStream != nil:
|
||||||
|
close(p.inStream)
|
||||||
|
else:
|
||||||
discard close(p.inHandle)
|
discard close(p.inHandle)
|
||||||
|
|
||||||
|
if p.outStream != nil:
|
||||||
|
close(p.outStream)
|
||||||
|
else:
|
||||||
discard close(p.outHandle)
|
discard close(p.outHandle)
|
||||||
|
|
||||||
|
if p.errStream != nil:
|
||||||
|
close(p.errStream)
|
||||||
|
else:
|
||||||
discard close(p.errHandle)
|
discard close(p.errHandle)
|
||||||
|
|
||||||
proc suspend(p: Process) =
|
proc suspend(p: Process) =
|
||||||
|
|
|
||||||
24
tests/osproc/tclose.nim
Normal file
24
tests/osproc/tclose.nim
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
discard """
|
||||||
|
exitcode: 0
|
||||||
|
"""
|
||||||
|
|
||||||
|
when defined(linux):
|
||||||
|
import osproc, os
|
||||||
|
|
||||||
|
proc countFds(): int =
|
||||||
|
result = 0
|
||||||
|
for i in walkDir("/proc/self/fd"):
|
||||||
|
result += 1
|
||||||
|
|
||||||
|
let initCount = countFds()
|
||||||
|
|
||||||
|
let p = osproc.startProcess("echo", options={poUsePath})
|
||||||
|
assert countFds() == initCount + 3
|
||||||
|
p.close
|
||||||
|
assert countFds() == initCount
|
||||||
|
|
||||||
|
let p1 = osproc.startProcess("echo", options={poUsePath})
|
||||||
|
discard p1.inputStream
|
||||||
|
assert countFds() == initCount + 3
|
||||||
|
p.close
|
||||||
|
assert countFds() == initCount
|
||||||
Loading…
Add table
Add a link
Reference in a new issue