Skip to content
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

Fix potential race conditions when cancelling read/write idle timers #720

Merged
merged 2 commits into from
Dec 18, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,13 @@ final class HTTP1ClientChannelHandler: ChannelDuplexHandler {
private var idleReadTimeoutStateMachine: IdleReadStateMachine?
private var idleReadTimeoutTimer: Scheduled<Void>?

/// Cancelling a task in NIO does *not* guarantee that the task will not execute under certain race conditions.
/// We therefore give each timer an ID and increase the ID every time we reset or cancel it.
/// We check in the task if the timer ID has changed in the meantime and do not execute any action if has changed.
private var currentIdleReadTimeoutTimerID: Int = 0

private var idleWriteTimeoutStateMachine: IdleWriteStateMachine?
private var idleWriteTimeoutTimer: Scheduled<Void>?

/// Cancelling a task in NIO does *not* guarantee that the task will not execute under certain race conditions.
/// We therefore give each timer an ID and increase the ID every time we reset or cancel it.
/// We check in the task if the timer ID has changed in the meantime and do not execute any action if has changed.
private var currentIdleReadTimeoutTimerID: Int = 0
private var currentIdleWriteTimeoutTimerID: Int = 0

private let backgroundLogger: Logger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ final class HTTP2ClientRequestHandler: ChannelDuplexHandler {
private var idleWriteTimeoutStateMachine: IdleWriteStateMachine?
private var idleWriteTimeoutTimer: Scheduled<Void>?

/// Cancelling a task in NIO does *not* guarantee that the task will not execute under certain race conditions.
/// We therefore give each timer an ID and increase the ID every time we reset or cancel it.
/// We check in the task if the timer ID has changed in the meantime and do not execute any action if has changed.
private var currentIdleReadTimeoutTimerID: Int = 0
private var currentIdleWriteTimeoutTimerID: Int = 0

init(eventLoop: EventLoop) {
self.eventLoop = eventLoop
}
Expand Down Expand Up @@ -295,8 +301,9 @@ final class HTTP2ClientRequestHandler: ChannelDuplexHandler {
case .startIdleReadTimeoutTimer(let timeAmount):
assert(self.idleReadTimeoutTimer == nil, "Expected there is no timeout timer so far.")

let timerID = self.currentIdleReadTimeoutTimerID
self.idleReadTimeoutTimer = self.eventLoop.scheduleTask(in: timeAmount) {
guard self.idleReadTimeoutTimer != nil else { return }
guard self.currentIdleReadTimeoutTimerID == timerID else { return }
let action = self.state.idleReadTimeoutTriggered()
self.run(action, context: context)
}
Expand All @@ -306,14 +313,17 @@ final class HTTP2ClientRequestHandler: ChannelDuplexHandler {
oldTimer.cancel()
}

self.currentIdleReadTimeoutTimerID &+= 1
let timerID = self.currentIdleReadTimeoutTimerID
self.idleReadTimeoutTimer = self.eventLoop.scheduleTask(in: timeAmount) {
guard self.idleReadTimeoutTimer != nil else { return }
guard self.currentIdleReadTimeoutTimerID == timerID else { return }
let action = self.state.idleReadTimeoutTriggered()
self.run(action, context: context)
}
case .clearIdleReadTimeoutTimer:
if let oldTimer = self.idleReadTimeoutTimer {
self.idleReadTimeoutTimer = nil
self.currentIdleReadTimeoutTimerID &+= 1
oldTimer.cancel()
}

Expand All @@ -327,8 +337,9 @@ final class HTTP2ClientRequestHandler: ChannelDuplexHandler {
case .startIdleWriteTimeoutTimer(let timeAmount):
assert(self.idleWriteTimeoutTimer == nil, "Expected there is no timeout timer so far.")

let timerID = self.currentIdleWriteTimeoutTimerID
self.idleWriteTimeoutTimer = self.eventLoop.scheduleTask(in: timeAmount) {
guard self.idleWriteTimeoutTimer != nil else { return }
guard self.currentIdleWriteTimeoutTimerID == timerID else { return }
let action = self.state.idleWriteTimeoutTriggered()
self.run(action, context: context)
}
Expand All @@ -337,14 +348,17 @@ final class HTTP2ClientRequestHandler: ChannelDuplexHandler {
oldTimer.cancel()
}

self.currentIdleWriteTimeoutTimerID &+= 1
let timerID = self.currentIdleWriteTimeoutTimerID
self.idleWriteTimeoutTimer = self.eventLoop.scheduleTask(in: timeAmount) {
guard self.idleWriteTimeoutTimer != nil else { return }
guard self.currentIdleWriteTimeoutTimerID == timerID else { return }
let action = self.state.idleWriteTimeoutTriggered()
self.run(action, context: context)
}
case .clearIdleWriteTimeoutTimer:
if let oldTimer = self.idleWriteTimeoutTimer {
self.idleWriteTimeoutTimer = nil
self.currentIdleWriteTimeoutTimerID &+= 1
oldTimer.cancel()
}
case .none:
Expand Down