Skip to content

Revert "Fix for a race condition in URLSession (#949)" #1188

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

Merged
merged 1 commit into from
Sep 7, 2017
Merged
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
56 changes: 19 additions & 37 deletions Foundation/URLSession/http/MultiHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down