Skip to content

fix: ping race condition #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 11 additions & 4 deletions Sources/WebSocketKit/WebSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public final class WebSocket: Sendable {
public var closeCode: WebSocketErrorCode? {
_closeCode.withLockedValue { $0 }
}

private let _closeCode: NIOLockedValueBox<WebSocketErrorCode?>

public var onClose: EventLoopFuture<Void> {
Expand Down Expand Up @@ -65,11 +65,11 @@ public final class WebSocket: Sendable {
@preconcurrency public func onBinary(_ callback: @Sendable @escaping (WebSocket, ByteBuffer) -> ()) {
self.onBinaryCallback.value = callback
}

public func onPong(_ callback: @Sendable @escaping (WebSocket, ByteBuffer) -> ()) {
self.onPongCallback.value = callback
}

@available(*, deprecated, message: "Please use `onPong { socket, data in /* … */ }` with the additional `data` parameter.")
@preconcurrency public func onPong(_ callback: @Sendable @escaping (WebSocket) -> ()) {
self.onPongCallback.value = { ws, _ in callback(ws) }
Expand All @@ -78,7 +78,7 @@ public final class WebSocket: Sendable {
public func onPing(_ callback: @Sendable @escaping (WebSocket, ByteBuffer) -> ()) {
self.onPingCallback.value = callback
}

@available(*, deprecated, message: "Please use `onPing { socket, data in /* … */ }` with the additional `data` parameter.")
@preconcurrency public func onPing(_ callback: @Sendable @escaping (WebSocket) -> ()) {
self.onPingCallback.value = { ws, _ in callback(ws) }
Expand Down Expand Up @@ -308,6 +308,13 @@ public final class WebSocket: Sendable {

@Sendable
private func pingAndScheduleNextTimeoutTask() {
if !eventLoop.inEventLoop {
eventLoop.execute {
self.pingAndScheduleNextTimeoutTask()
}
return
}

guard channel.isActive, let pingInterval = pingInterval else {
return
}
Expand Down