Skip to content

Commit c725d8e

Browse files
committed
Make _MultiHandle timeout timer non-repeatable
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.
1 parent 81c3123 commit c725d8e

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

Sources/FoundationNetworking/URLSession/libcurl/MultiHandle.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ class _TimeoutSource {
363363
let delay = UInt64(max(1, milliseconds - 1))
364364
let start = DispatchTime.now() + DispatchTimeInterval.milliseconds(Int(delay))
365365

366-
rawSource.schedule(deadline: start, repeating: .milliseconds(Int(delay)), leeway: (milliseconds == 1) ? .microseconds(Int(1)) : .milliseconds(Int(1)))
366+
rawSource.schedule(deadline: start, repeating: .never, leeway: (milliseconds == 1) ? .microseconds(Int(1)) : .milliseconds(Int(1)))
367367
rawSource.setEventHandler(handler: handler)
368368
rawSource.resume()
369369
}
@@ -388,13 +388,12 @@ fileprivate extension URLSession._MultiHandle {
388388
timeoutSource = nil
389389
queue.async { self.timeoutTimerFired() }
390390
case .milliseconds(let milliseconds):
391-
if (timeoutSource == nil) || timeoutSource!.milliseconds != milliseconds {
392-
//TODO: Could simply change the existing timer by using DispatchSourceTimer again.
393-
let block = DispatchWorkItem { [weak self] in
394-
self?.timeoutTimerFired()
395-
}
396-
timeoutSource = _TimeoutSource(queue: queue, milliseconds: milliseconds, handler: block)
391+
//TODO: Could simply change the existing timer by using DispatchSourceTimer again.
392+
let block = DispatchWorkItem { [weak self] in
393+
self?.timeoutTimerFired()
397394
}
395+
// Note: Previous timer instance would cancel internal Dispatch timer in deinit
396+
timeoutSource = _TimeoutSource(queue: queue, milliseconds: milliseconds, handler: block)
398397
}
399398
}
400399
enum _Timeout {

0 commit comments

Comments
 (0)