diff --git a/Sources/Foundation/FileHandle.swift b/Sources/Foundation/FileHandle.swift index 7e9f25f862..a53bbafced 100644 --- a/Sources/Foundation/FileHandle.swift +++ b/Sources/Foundation/FileHandle.swift @@ -634,19 +634,20 @@ open class FileHandle : NSObject { readabilitySource = nil privateAsyncVariablesLock.unlock() - if closeFd { #if os(Windows) + // SR-13822 - Not Closing the file descriptor on Windows causes a Stack Overflow guard CloseHandle(self._handle) else { throw _NSErrorWithWindowsError(GetLastError(), reading: true) } self._handle = INVALID_HANDLE_VALUE #else - guard _close(_fd) >= 0 else { - throw _NSErrorWithErrno(errno, reading: true) + if closeFd { + guard _close(_fd) >= 0 else { + throw _NSErrorWithErrno(errno, reading: true) + } + _fd = -1 } - _fd = -1 #endif - } } // MARK: - diff --git a/Tests/Foundation/Tests/TestFileHandle.swift b/Tests/Foundation/Tests/TestFileHandle.swift index eea4a1f4c5..07d5b291fb 100644 --- a/Tests/Foundation/Tests/TestFileHandle.swift +++ b/Tests/Foundation/Tests/TestFileHandle.swift @@ -580,7 +580,7 @@ class TestFileHandle : XCTestCase { } } -#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT +#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT && !os(Windows) func test_closeOnDealloc() throws { try withTemporaryDirectory() { (url, path) in let data = try XCTUnwrap("hello".data(using: .utf8)) @@ -640,8 +640,14 @@ class TestFileHandle : XCTestCase { ("test_nullDevice", test_nullDevice), ("testHandleCreationAndCleanup", testHandleCreationAndCleanup), ("testOffset", testOffset), + ]) + + #if !os(Windows) + tests.append(contentsOf: [ + /* ⚠️ SR-13822 - closeOnDealloc doesnt work on Windows and so this test is disabled there. */ ("test_closeOnDealloc", test_closeOnDealloc), ]) + #endif #endif return tests