Skip to content

Commit

Permalink
fix(IRC): handle socket disconnected error when leaving last room (#16)
Browse files Browse the repository at this point in the history
kevinrpb authored Sep 23, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 34896d0 commit 4ae19df
Showing 2 changed files with 18 additions and 3 deletions.
13 changes: 12 additions & 1 deletion Sources/Twitch/IRC/IRCConnection.swift
Original file line number Diff line number Diff line change
@@ -54,7 +54,18 @@ internal actor IRCConnection {
}
}
} catch {
continuation.finish(throwing: error)
// If we get the 'socket disconnected' error, check whether we have parted from all rooms.
// Doing so is what causes us to get this error on the next websocket.receive() call
let nsError = error as NSError
if nsError.domain == "NSPOSIXErrorDomain", nsError.code == 57,
joinedChannels.isEmpty
{
continuation.finish()
} else {
// If we can't identify the origin of the error, return it in the stream to be hanlded upstream
continuation.finish(throwing: error)
}

self.disconnect()
}
}
8 changes: 6 additions & 2 deletions Sources/Twitch/IRC/IRCConnectionPool.swift
Original file line number Diff line number Diff line change
@@ -83,8 +83,12 @@ public actor IRCConnectionPool {
let messageStream = try await connection.connect()

Task {
for try await message in messageStream {
self.continuation?.yield(message)
do {
for try await message in messageStream {
self.continuation?.yield(message)
}
} catch {
self.continuation?.finish(throwing: error)
}
}

0 comments on commit 4ae19df

Please sign in to comment.