Skip to content

Commit ca48d46

Browse files
authored
Merge pull request #43 from hummingbird-project/ignore-close-errors
Don't error on writing close frame after receiving close frame
2 parents e47325d + 4fa47d1 commit ca48d46

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

Sources/WSCore/WebSocketHandler.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public struct WebSocketCloseFrame: Sendable {
194194
// Close handshake. Wait for responding close or until inbound ends
195195
while let frame = try await inboundIterator.next() {
196196
if case .connectionClose = frame.opcode {
197-
try await self.receivedClose(frame)
197+
await self.receivedClose(frame)
198198
// only the server can close the connection, so clients
199199
// should continue reading from inbound until it is closed
200200
if type == .server {
@@ -319,14 +319,16 @@ public struct WebSocketCloseFrame: Sendable {
319319
}
320320
}
321321

322-
func receivedClose(_ frame: WebSocketFrame) async throws {
322+
func receivedClose(_ frame: WebSocketFrame) async {
323323
guard frame.reservedBits.isEmpty else {
324-
try await self.close(code: .protocolError, reason: nil)
324+
// ignore errors from sending close as the other side may have closed the connection already
325+
try? await self.close(code: .protocolError, reason: nil)
325326
return
326327
}
327328
switch self.stateMachine.receivedClose(frameData: frame.unmaskedData, validateUTF8: self.configuration.validateUTF8) {
328329
case .sendClose(let errorCode):
329-
try await self.sendClose(code: errorCode, reason: nil)
330+
// ignore errors from sending close as the other side may have closed the connection already
331+
try? await self.sendClose(code: errorCode, reason: nil)
330332
// Only server should initiate a connection close. Clients should wait for the
331333
// server to close the connection when it receives the WebSocket close packet
332334
// See https://www.rfc-editor.org/rfc/rfc6455#section-7.1.1

Sources/WSCore/WebSocketInboundStream.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public final class WebSocketInboundStream: AsyncSequence, Sendable {
5454
self.handler.logger.trace("Received \(frame.traceDescription)")
5555
switch frame.opcode {
5656
case .connectionClose:
57-
try await self.handler.receivedClose(frame)
57+
await self.handler.receivedClose(frame)
5858
return nil
5959
case .ping:
6060
try await self.handler.onPing(frame)

0 commit comments

Comments
 (0)