Skip to content

Commit 8238d23

Browse files
authored
Merge pull request #2238 from gmittert/UseTwoHandsWhenDuplicating
Duplicate the Handle instead of the fd on Windows
2 parents 5577346 + 42f3903 commit 8238d23

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

Foundation/FileHandle.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,17 @@ open class FileHandle : NSObject {
111111

112112
// Duplicate the file descriptor.
113113
// Closing the file descriptor while Dispatch is monitoring it leads to undefined behavior; guard against that.
114+
#if os(Windows)
115+
var dupHandle: HANDLE?
116+
if !DuplicateHandle(GetCurrentProcess(), handle, GetCurrentProcess(), &dupHandle,
117+
/*dwDesiredAccess:*/0, /*bInheritHandle:*/true, DWORD(DUPLICATE_SAME_ACCESS)) {
118+
fatalError("DuplicateHandleFailed: \(GetLastError())")
119+
}
120+
121+
let fd = _open_osfhandle(intptr_t(bitPattern: dupHandle), 0)
122+
#else
114123
let fd = dup(fileDescriptor)
124+
#endif
115125
let source: DispatchSourceProtocol
116126
if reading {
117127
source = DispatchSource.makeReadSource(fileDescriptor: fd, queue: queue)

0 commit comments

Comments
 (0)