Windows: Fix invalid handle value for execProcesses.

Windows. Fix named pipes leak.
This commit is contained in:
cheatfate 2017-12-12 16:53:09 +02:00
commit e672249859

View file

@ -273,7 +273,6 @@ proc execProcesses*(cmds: openArray[string],
discard getExitCodeProcess(q[r].fProcessHandle, status) discard getExitCodeProcess(q[r].fProcessHandle, status)
q[r].exitFlag = true q[r].exitFlag = true
q[r].exitStatus = status q[r].exitStatus = status
discard closeHandle(q[r].fProcessHandle)
break break
else: else:
var status: cint = 1 var status: cint = 1
@ -313,11 +312,14 @@ proc execProcesses*(cmds: openArray[string],
w[r] = q[r].fProcessHandle w[r] = q[r].fProcessHandle
inc(i) inc(i)
else: else:
q[r] = nil
when defined(windows): when defined(windows):
for c in r..MAXIMUM_WAIT_OBJECTS - 2: for k in 0..wcount - 1:
w[c] = w[c + 1] if w[k] == q[r].fProcessHandle:
dec(wcount) w[k] = w[wcount - 1]
w[wcount - 1] = 0
dec(wcount)
break
q[r] = nil
dec(ecount) dec(ecount)
else: else:
for i in 0..high(cmds): for i in 0..high(cmds):
@ -574,17 +576,17 @@ when defined(Windows) and not defined(useNimRtl):
"Requested command not found: '$1'. OS error:" % command) "Requested command not found: '$1'. OS error:" % command)
else: else:
raiseOSError(lastError, command) raiseOSError(lastError, command)
result.fProcessHandle = procInfo.hProcess result.fProcessHandle = procInfo.hProcess
result.fThreadHandle = procInfo.hThread result.fThreadHandle = procInfo.hThread
result.id = procInfo.dwProcessId result.id = procInfo.dwProcessId
result.exitFlag = false result.exitFlag = false
proc close(p: Process) = proc close(p: Process) =
if poInteractive in p.options: if poParentStreams notin p.options:
discard closeHandle(p.inHandle) discard closeHandle(p.inHandle)
discard closeHandle(p.outHandle) discard closeHandle(p.outHandle)
discard closeHandle(p.errHandle) discard closeHandle(p.errHandle)
discard closeHandle(p.fThreadHandle)
discard closeHandle(p.fProcessHandle) discard closeHandle(p.fProcessHandle)
proc suspend(p: Process) = proc suspend(p: Process) =
@ -619,6 +621,7 @@ when defined(Windows) and not defined(useNimRtl):
if status != STILL_ACTIVE: if status != STILL_ACTIVE:
p.exitFlag = true p.exitFlag = true
p.exitStatus = status p.exitStatus = status
discard closeHandle(p.fThreadHandle)
discard closeHandle(p.fProcessHandle) discard closeHandle(p.fProcessHandle)
result = status result = status
else: else:
@ -635,6 +638,7 @@ when defined(Windows) and not defined(useNimRtl):
discard getExitCodeProcess(p.fProcessHandle, status) discard getExitCodeProcess(p.fProcessHandle, status)
p.exitFlag = true p.exitFlag = true
p.exitStatus = status p.exitStatus = status
discard closeHandle(p.fThreadHandle)
discard closeHandle(p.fProcessHandle) discard closeHandle(p.fProcessHandle)
result = status result = status