Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions FlyingSocks/Sources/AsyncSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,14 @@ public struct AsyncSocket: Sendable {
timeout: TimeInterval = 5) async throws -> Self {
try await withThrowingTimeout(seconds: timeout) {
let socket = try Socket(domain: Int32(type(of: address).family), type: .stream)
let asyncSocket = try AsyncSocket(socket: socket, pool: pool)
try await asyncSocket.connect(to: address)
return asyncSocket
do {
let asyncSocket = try AsyncSocket(socket: socket, pool: pool)
try await asyncSocket.connect(to: address)
return asyncSocket
} catch {
try? socket.close()
throw error
}
}
}

Expand All @@ -134,7 +139,12 @@ public struct AsyncSocket: Sendable {
try await pool.loopUntilReady(for: .connection, on: socket) {
let file = try socket.accept().file
let socket = Socket(file: file)
return try AsyncSocket(socket: socket, pool: pool)
do {
return try AsyncSocket(socket: socket, pool: pool)
} catch {
try? socket.close()
throw error
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions FlyingSocks/Tests/AsyncSocketTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ struct AsyncSocketTests {
try await task.value
}

@Test
func connected_ThrowsError_WhenConnectFails() async throws {
await #expect(throws: SocketError.self) {
_ = try await AsyncSocket.connected(
to: sockaddr_un.unix(path: "/nonexistent/\(UUID().uuidString)"),
pool: DisconnectedPool()
)
}
}

@Test(.disabled("problematic test as file descriptor can be re-opened by another parallel test"))
func socketReadByte_ThrowsDisconnected_WhenSocketIsClosed() async throws {
let s1 = try await AsyncSocket.make()
Expand Down
Loading