Skip to content

Commit 821c87b

Browse files
committed
Fix flakiness in asyncReadFileDescriptor test
It's not safe to "modify" a file descriptor while it's in use by DispatchIO, per the documentation, which is the likely cause of flakiness here.
1 parent 50c8453 commit 821c87b

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

Sources/SWBUtil/FSProxy.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -529,13 +529,9 @@ class LocalFS: FSProxy, @unchecked Sendable {
529529

530530
func write(_ path: Path, contents: (FileDescriptor) async throws -> Void) async throws {
531531
let fd = try FileDescriptor.open(FilePath(path.str), .writeOnly, options: [.create, .truncate], permissions: [.ownerReadWrite, .groupRead, .otherRead])
532-
do {
532+
return try await fd.closeAfter {
533533
try await contents(fd)
534-
} catch {
535-
_ = try? fd.close()
536-
throw error
537534
}
538-
try fd.close()
539535
}
540536

541537
func append(_ path: Path, contents: ByteString) throws {

Tests/SWBUtilTests/FileHandleTests.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,28 +69,30 @@ import SystemPackage
6969
// Read while triggering an I/O error
7070
do {
7171
let fd = try Path.root.str.withPlatformString { try FileDescriptor.open($0, .readOnly) }
72-
let fh = FileHandle(fileDescriptor: fd.rawValue, closeOnDealloc: false)
72+
try await fd.closeAfter {
73+
let fh = FileHandle(fileDescriptor: fd.rawValue, closeOnDealloc: false)
7374

74-
if #available(macOS 15, iOS 18, tvOS 18, watchOS 11, visionOS 2, *) {
75-
var it = fh.bytes().makeAsyncIterator()
76-
try fd.close()
75+
if #available(macOS 15, iOS 18, tvOS 18, watchOS 11, visionOS 2, *) {
76+
var it = fh.bytes().makeAsyncIterator()
7777

78-
await #expect(throws: (any Error).self) {
79-
while let _ = try await it.next() {
78+
// Can't read a directory
79+
await #expect(throws: (any Error).self) {
80+
while let _ = try await it.next() {
81+
}
8082
}
81-
}
82-
} else {
83-
var it = fh._bytes().makeAsyncIterator()
84-
try fd.close()
83+
} else {
84+
var it = fh._bytes().makeAsyncIterator()
8585

86-
await #expect(throws: (any Error).self) {
87-
while let _ = try await it.next() {
86+
// Can't read a directory
87+
await #expect(throws: (any Error).self) {
88+
while let _ = try await it.next() {
89+
}
8890
}
8991
}
9092
}
9193
}
9294

93-
// Read part of a file, then close the handle
95+
// Read part of a file, then cancel the task
9496
do {
9597
let condition = CancellableWaitCondition()
9698

0 commit comments

Comments
 (0)