Skip to content

Fix an issue where process handles are leaked in exec on Windows #503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 54 additions & 51 deletions Sources/TSCBasic/misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,71 +93,74 @@ private func quote(_ arguments: [String]) -> String {
public func exec(path: String, args: [String]) throws -> Never {
let cArgs = CStringArray(args)
#if os(Windows)
var hJob: HANDLE
// Wrap body in a do block to ensure closing handles in defer blocks occurs prior to the call to _exit
var dwExitCode: DWORD = DWORD(bitPattern: -1)
do {
var hJob: HANDLE

hJob = CreateJobObjectA(nil, nil)
if hJob == HANDLE(bitPattern: 0) {
throw SystemError.exec(Int32(GetLastError()), path: path, args: args)
}
defer { CloseHandle(hJob) }
hJob = CreateJobObjectA(nil, nil)
if hJob == HANDLE(bitPattern: 0) {
throw SystemError.exec(Int32(GetLastError()), path: path, args: args)
}
defer { CloseHandle(hJob) }

let hPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, nil, 0, 1)
if hPort == HANDLE(bitPattern: 0) {
throw SystemError.exec(Int32(GetLastError()), path: path, args: args)
}
let hPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, nil, 0, 1)
if hPort == HANDLE(bitPattern: 0) {
throw SystemError.exec(Int32(GetLastError()), path: path, args: args)
}

var acpAssociation: JOBOBJECT_ASSOCIATE_COMPLETION_PORT = JOBOBJECT_ASSOCIATE_COMPLETION_PORT()
acpAssociation.CompletionKey = hJob
acpAssociation.CompletionPort = hPort
if !SetInformationJobObject(hJob, JobObjectAssociateCompletionPortInformation,
&acpAssociation, DWORD(MemoryLayout<JOBOBJECT_ASSOCIATE_COMPLETION_PORT>.size)) {
throw SystemError.exec(Int32(GetLastError()), path: path, args: args)
}
var acpAssociation: JOBOBJECT_ASSOCIATE_COMPLETION_PORT = JOBOBJECT_ASSOCIATE_COMPLETION_PORT()
acpAssociation.CompletionKey = hJob
acpAssociation.CompletionPort = hPort
if !SetInformationJobObject(hJob, JobObjectAssociateCompletionPortInformation,
&acpAssociation, DWORD(MemoryLayout<JOBOBJECT_ASSOCIATE_COMPLETION_PORT>.size)) {
throw SystemError.exec(Int32(GetLastError()), path: path, args: args)
}

var eliLimits: JOBOBJECT_EXTENDED_LIMIT_INFORMATION = JOBOBJECT_EXTENDED_LIMIT_INFORMATION()
eliLimits.BasicLimitInformation.LimitFlags =
DWORD(JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE) | DWORD(JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)
if !SetInformationJobObject(hJob, JobObjectExtendedLimitInformation, &eliLimits,
DWORD(MemoryLayout<JOBOBJECT_EXTENDED_LIMIT_INFORMATION>.size)) {
throw SystemError.exec(Int32(GetLastError()), path: path, args: args)
}
var eliLimits: JOBOBJECT_EXTENDED_LIMIT_INFORMATION = JOBOBJECT_EXTENDED_LIMIT_INFORMATION()
eliLimits.BasicLimitInformation.LimitFlags =
DWORD(JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE) | DWORD(JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)
if !SetInformationJobObject(hJob, JobObjectExtendedLimitInformation, &eliLimits,
DWORD(MemoryLayout<JOBOBJECT_EXTENDED_LIMIT_INFORMATION>.size)) {
throw SystemError.exec(Int32(GetLastError()), path: path, args: args)
}


var siInfo: STARTUPINFOW = STARTUPINFOW()
siInfo.cb = DWORD(MemoryLayout<STARTUPINFOW>.size)
var siInfo: STARTUPINFOW = STARTUPINFOW()
siInfo.cb = DWORD(MemoryLayout<STARTUPINFOW>.size)

var piInfo: PROCESS_INFORMATION = PROCESS_INFORMATION()
var piInfo: PROCESS_INFORMATION = PROCESS_INFORMATION()

try quote(args).withCString(encodedAs: UTF16.self) { pwszCommandLine in
if !CreateProcessW(nil,
UnsafeMutablePointer<WCHAR>(mutating: pwszCommandLine),
nil, nil, false,
DWORD(CREATE_SUSPENDED) | DWORD(CREATE_NEW_PROCESS_GROUP),
nil, nil, &siInfo, &piInfo) {
throw SystemError.exec(Int32(GetLastError()), path: path, args: args)
try quote(args).withCString(encodedAs: UTF16.self) { pwszCommandLine in
if !CreateProcessW(nil,
UnsafeMutablePointer<WCHAR>(mutating: pwszCommandLine),
nil, nil, false,
DWORD(CREATE_SUSPENDED) | DWORD(CREATE_NEW_PROCESS_GROUP),
nil, nil, &siInfo, &piInfo) {
throw SystemError.exec(Int32(GetLastError()), path: path, args: args)
}
}
}

defer { CloseHandle(piInfo.hThread) }
defer { CloseHandle(piInfo.hProcess) }
defer { CloseHandle(piInfo.hThread) }
defer { CloseHandle(piInfo.hProcess) }

if !AssignProcessToJobObject(hJob, piInfo.hProcess) {
throw SystemError.exec(Int32(GetLastError()), path: path, args: args)
}
if !AssignProcessToJobObject(hJob, piInfo.hProcess) {
throw SystemError.exec(Int32(GetLastError()), path: path, args: args)
}

_ = ResumeThread(piInfo.hThread)
_ = ResumeThread(piInfo.hThread)

var dwCompletionCode: DWORD = 0
var ulCompletionKey: ULONG_PTR = 0
var lpOverlapped: LPOVERLAPPED?
repeat {
} while GetQueuedCompletionStatus(hPort, &dwCompletionCode, &ulCompletionKey,
&lpOverlapped, INFINITE) &&
!(ulCompletionKey == ULONG_PTR(UInt(bitPattern: hJob)) &&
dwCompletionCode == JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO)
var dwCompletionCode: DWORD = 0
var ulCompletionKey: ULONG_PTR = 0
var lpOverlapped: LPOVERLAPPED?
repeat {
} while GetQueuedCompletionStatus(hPort, &dwCompletionCode, &ulCompletionKey,
&lpOverlapped, INFINITE) &&
!(ulCompletionKey == ULONG_PTR(UInt(bitPattern: hJob)) &&
dwCompletionCode == JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO)

var dwExitCode: DWORD = DWORD(bitPattern: -1)
_ = GetExitCodeProcess(piInfo.hProcess, &dwExitCode)
_ = GetExitCodeProcess(piInfo.hProcess, &dwExitCode)
}
_exit(Int32(bitPattern: dwExitCode))
#elseif (!canImport(Darwin) || os(macOS))
guard execv(path, cArgs.cArray) != -1 else {
Expand Down