From bac9f5f1fa8b79cd2b1fd2db624f0609080c503e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Wei=C3=9F?= Date: Thu, 24 Aug 2017 11:08:05 +0100 Subject: [PATCH] Revert "Fix for a race condition in URLSession (#949)" This reverts commit 015ded4d70a7cf4b2741d7206fc60a3d1ceffe03. --- Foundation/URLSession/http/MultiHandle.swift | 56 +++++++------------- 1 file changed, 19 insertions(+), 37 deletions(-) diff --git a/Foundation/URLSession/http/MultiHandle.swift b/Foundation/URLSession/http/MultiHandle.swift index bb2acafda6..506c9a9f23 100644 --- a/Foundation/URLSession/http/MultiHandle.swift +++ b/Foundation/URLSession/http/MultiHandle.swift @@ -39,11 +39,7 @@ extension URLSession { let group = DispatchGroup() fileprivate var easyHandles: [_EasyHandle] = [] fileprivate var timeoutSource: _TimeoutSource? = nil - - //SR-4567: we need to synchronize the register/unregister commands to the epoll machinery in libdispatch - fileprivate let commandQueue: DispatchQueue = DispatchQueue(label: "Register-unregister synchronization") - fileprivate var cancelInProgress: DispatchSemaphore? = nil - + init(configuration: URLSession._Configuration, workQueue: DispatchQueue) { queue = DispatchQueue(label: "MultiHandle.isolation", target: workQueue) setupCallbacks() @@ -103,33 +99,25 @@ fileprivate extension URLSession._MultiHandle { // through libdispatch (DispatchSource) and store the source(s) inside // a `SocketSources` which we in turn store inside libcurl's multi handle // by means of curl_multi_assign() -- we retain the object fist. - commandQueue.async { - self.cancelInProgress?.wait() - self.cancelInProgress = nil - - let action = _SocketRegisterAction(rawValue: CFURLSessionPoll(value: what)) - var socketSources = _SocketSources.from(socketSourcePtr: socketSourcePtr) - if socketSources == nil && action.needsSource { - let s = _SocketSources() - let p = Unmanaged.passRetained(s).toOpaque() - CFURLSessionMultiHandleAssign(self.rawHandle, socket, UnsafeMutableRawPointer(p)) - socketSources = s - } else if socketSources != nil && action == .unregister { - //the beginning of an unregister operation - self.cancelInProgress = DispatchSemaphore(value: 0) - // We need to release the stored pointer: - if let opaque = socketSourcePtr { - Unmanaged<_SocketSources>.fromOpaque(opaque).release() - } - socketSources?.tearDown(self.cancelInProgress) - socketSources = nil + let action = _SocketRegisterAction(rawValue: CFURLSessionPoll(value: what)) + var socketSources = _SocketSources.from(socketSourcePtr: socketSourcePtr) + if socketSources == nil && action.needsSource { + let s = _SocketSources() + let p = Unmanaged.passRetained(s).toOpaque() + CFURLSessionMultiHandleAssign(rawHandle, socket, UnsafeMutableRawPointer(p)) + socketSources = s + } else if socketSources != nil && action == .unregister { + // We need to release the stored pointer: + if let opaque = socketSourcePtr { + Unmanaged<_SocketSources>.fromOpaque(opaque).release() } - if let ss = socketSources { - let handler = DispatchWorkItem { [weak self] in - self?.performAction(for: socket) - } - ss.createSources(with: action, fileDescriptor: Int(socket), queue: self.queue, handler: handler) + socketSources = nil + } + if let ss = socketSources { + let handler = DispatchWorkItem { [weak self] in + self?.performAction(for: socket) } + ss.createSources(with: action, fileDescriptor: Int(socket), queue: queue, handler: handler) } return 0 } @@ -411,18 +399,12 @@ fileprivate class _SocketSources { s.resume() } - func tearDown(_ cancelInProgress: DispatchSemaphore?) { - let cancelHandler = DispatchWorkItem { - //the real end of an unregister operation! - cancelInProgress?.signal() - } + func tearDown() { if let s = readSource { - s.setCancelHandler(handler: cancelHandler) s.cancel() } readSource = nil if let s = writeSource { - s.setCancelHandler(handler: cancelHandler) s.cancel() } writeSource = nil