Skip to content

Commit

Permalink
Make _MultiHandle timeout timer non-repeatable
Browse files Browse the repository at this point in the history
CURL documentation (https://curl.se/libcurl/c/CURLMOPT_TIMERFUNCTION.html)
explicitly says that the timer should be one-time. We basically have to
follow CURL requests for setting, resetting and disarming such timers.

Current logic eventually leaves a 1ms repeating timer forever, because
CURL assumes it fires once, and may not ask us to remove it explicitly.

Also, being used as request timeout trigger, this timer also has no sense
to be repeated.
  • Loading branch information
lxbndr committed Dec 25, 2023
1 parent 1b514e4 commit 7258a8d
Showing 1 changed file with 6 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ class _TimeoutSource {
let delay = UInt64(max(1, milliseconds - 1))
let start = DispatchTime.now() + DispatchTimeInterval.milliseconds(Int(delay))

rawSource.schedule(deadline: start, repeating: .milliseconds(Int(delay)), leeway: (milliseconds == 1) ? .microseconds(Int(1)) : .milliseconds(Int(1)))
rawSource.schedule(deadline: start, repeating: .never, leeway: (milliseconds == 1) ? .microseconds(Int(1)) : .milliseconds(Int(1)))
rawSource.setEventHandler(handler: handler)
rawSource.resume()
}
Expand All @@ -384,13 +384,12 @@ fileprivate extension URLSession._MultiHandle {
timeoutSource = nil
queue.async { self.timeoutTimerFired() }
case .milliseconds(let milliseconds):
if (timeoutSource == nil) || timeoutSource!.milliseconds != milliseconds {
//TODO: Could simply change the existing timer by using DispatchSourceTimer again.
let block = DispatchWorkItem { [weak self] in
self?.timeoutTimerFired()
}
timeoutSource = _TimeoutSource(queue: queue, milliseconds: milliseconds, handler: block)
//TODO: Could simply change the existing timer by using DispatchSourceTimer again.
let block = DispatchWorkItem { [weak self] in
self?.timeoutTimerFired()
}
// Note: Previous timer instance would cancel internal Dispatch timer in deinit
timeoutSource = _TimeoutSource(queue: queue, milliseconds: milliseconds, handler: block)
}
}
enum _Timeout {
Expand Down

0 comments on commit 7258a8d

Please sign in to comment.