Skip to content

Commit 6264fe4

Browse files
authored
Merge pull request #2952 from spevans/pr_disable_closeOnDealloc_on_windows_54
[5.4] SR-13822: FileHandle closeOnDealloc causes a Stack Overflow on Windows
2 parents 0e18860 + 8141828 commit 6264fe4

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

Sources/Foundation/FileHandle.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -634,19 +634,20 @@ open class FileHandle : NSObject {
634634
readabilitySource = nil
635635
privateAsyncVariablesLock.unlock()
636636

637-
if closeFd {
638637
#if os(Windows)
638+
// SR-13822 - Not Closing the file descriptor on Windows causes a Stack Overflow
639639
guard CloseHandle(self._handle) else {
640640
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
641641
}
642642
self._handle = INVALID_HANDLE_VALUE
643643
#else
644-
guard _close(_fd) >= 0 else {
645-
throw _NSErrorWithErrno(errno, reading: true)
644+
if closeFd {
645+
guard _close(_fd) >= 0 else {
646+
throw _NSErrorWithErrno(errno, reading: true)
647+
}
648+
_fd = -1
646649
}
647-
_fd = -1
648650
#endif
649-
}
650651
}
651652

652653
// MARK: -

Tests/Foundation/Tests/TestFileHandle.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ class TestFileHandle : XCTestCase {
580580
}
581581
}
582582

583-
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
583+
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT && !os(Windows)
584584
func test_closeOnDealloc() throws {
585585
try withTemporaryDirectory() { (url, path) in
586586
let data = try XCTUnwrap("hello".data(using: .utf8))
@@ -640,8 +640,14 @@ class TestFileHandle : XCTestCase {
640640
("test_nullDevice", test_nullDevice),
641641
("testHandleCreationAndCleanup", testHandleCreationAndCleanup),
642642
("testOffset", testOffset),
643+
])
644+
645+
#if !os(Windows)
646+
tests.append(contentsOf: [
647+
/* ⚠️ SR-13822 - closeOnDealloc doesnt work on Windows and so this test is disabled there. */
643648
("test_closeOnDealloc", test_closeOnDealloc),
644649
])
650+
#endif
645651
#endif
646652

647653
return tests

0 commit comments

Comments
 (0)