Skip to content

Commit 01efe01

Browse files
authored
Merge pull request #3045 from compnerd/sync-sync
2 parents e673722 + 371bd1f commit 01efe01

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Sources/Foundation/FileHandle.swift

+11-1
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,17 @@ open class FileHandle : NSObject {
592592

593593
#if os(Windows)
594594
guard FlushFileBuffers(self._handle) else {
595-
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
595+
let dwError: DWORD = GetLastError()
596+
// If the handle is a handle to the console output,
597+
// `FlushFileBuffers` will fail and return `ERROR_INVALID_HANDLE` as
598+
// console output is not buffered.
599+
if dwError == ERROR_INVALID_HANDLE &&
600+
GetFileType(self._handle) == FILE_TYPE_CHAR {
601+
// Simlar to the Linux, macOS, BSD cases below, ignore the error
602+
// on the special file type.
603+
return
604+
}
605+
throw _NSErrorWithWindowsError(dwError, reading: false)
596606
}
597607
#else
598608
// Linux, macOS, OpenBSD return -1 and errno == EINVAL if trying to sync a special file,

Tests/Foundation/Tests/TestFileHandle.swift

+4
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,11 @@ class TestFileHandle : XCTestCase {
615615

616616
func testSynchronizeOnSpecialFile() throws {
617617
// .synchronize() on a special file shouldnt fail
618+
#if os(Windows)
619+
let fh = try XCTUnwrap(FileHandle(forWritingAtPath: "CON"))
620+
#else
618621
let fh = try XCTUnwrap(FileHandle(forWritingAtPath: "/dev/stdout"))
622+
#endif
619623
XCTAssertNoThrow(try fh.synchronize())
620624
}
621625

0 commit comments

Comments
 (0)