Skip to content

Commit 3609b9c

Browse files
authored
Merge pull request #1188 from weissi/jw-revert-PR-949
2 parents 9c08115 + bac9f5f commit 3609b9c

File tree

1 file changed

+19
-37
lines changed

1 file changed

+19
-37
lines changed

Foundation/URLSession/http/MultiHandle.swift

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ extension URLSession {
3939
let group = DispatchGroup()
4040
fileprivate var easyHandles: [_EasyHandle] = []
4141
fileprivate var timeoutSource: _TimeoutSource? = nil
42-
43-
//SR-4567: we need to synchronize the register/unregister commands to the epoll machinery in libdispatch
44-
fileprivate let commandQueue: DispatchQueue = DispatchQueue(label: "Register-unregister synchronization")
45-
fileprivate var cancelInProgress: DispatchSemaphore? = nil
46-
42+
4743
init(configuration: URLSession._Configuration, workQueue: DispatchQueue) {
4844
queue = DispatchQueue(label: "MultiHandle.isolation", target: workQueue)
4945
setupCallbacks()
@@ -103,33 +99,25 @@ fileprivate extension URLSession._MultiHandle {
10399
// through libdispatch (DispatchSource) and store the source(s) inside
104100
// a `SocketSources` which we in turn store inside libcurl's multi handle
105101
// by means of curl_multi_assign() -- we retain the object fist.
106-
commandQueue.async {
107-
self.cancelInProgress?.wait()
108-
self.cancelInProgress = nil
109-
110-
let action = _SocketRegisterAction(rawValue: CFURLSessionPoll(value: what))
111-
var socketSources = _SocketSources.from(socketSourcePtr: socketSourcePtr)
112-
if socketSources == nil && action.needsSource {
113-
let s = _SocketSources()
114-
let p = Unmanaged.passRetained(s).toOpaque()
115-
CFURLSessionMultiHandleAssign(self.rawHandle, socket, UnsafeMutableRawPointer(p))
116-
socketSources = s
117-
} else if socketSources != nil && action == .unregister {
118-
//the beginning of an unregister operation
119-
self.cancelInProgress = DispatchSemaphore(value: 0)
120-
// We need to release the stored pointer:
121-
if let opaque = socketSourcePtr {
122-
Unmanaged<_SocketSources>.fromOpaque(opaque).release()
123-
}
124-
socketSources?.tearDown(self.cancelInProgress)
125-
socketSources = nil
102+
let action = _SocketRegisterAction(rawValue: CFURLSessionPoll(value: what))
103+
var socketSources = _SocketSources.from(socketSourcePtr: socketSourcePtr)
104+
if socketSources == nil && action.needsSource {
105+
let s = _SocketSources()
106+
let p = Unmanaged.passRetained(s).toOpaque()
107+
CFURLSessionMultiHandleAssign(rawHandle, socket, UnsafeMutableRawPointer(p))
108+
socketSources = s
109+
} else if socketSources != nil && action == .unregister {
110+
// We need to release the stored pointer:
111+
if let opaque = socketSourcePtr {
112+
Unmanaged<_SocketSources>.fromOpaque(opaque).release()
126113
}
127-
if let ss = socketSources {
128-
let handler = DispatchWorkItem { [weak self] in
129-
self?.performAction(for: socket)
130-
}
131-
ss.createSources(with: action, fileDescriptor: Int(socket), queue: self.queue, handler: handler)
114+
socketSources = nil
115+
}
116+
if let ss = socketSources {
117+
let handler = DispatchWorkItem { [weak self] in
118+
self?.performAction(for: socket)
132119
}
120+
ss.createSources(with: action, fileDescriptor: Int(socket), queue: queue, handler: handler)
133121
}
134122
return 0
135123
}
@@ -411,18 +399,12 @@ fileprivate class _SocketSources {
411399
s.resume()
412400
}
413401

414-
func tearDown(_ cancelInProgress: DispatchSemaphore?) {
415-
let cancelHandler = DispatchWorkItem {
416-
//the real end of an unregister operation!
417-
cancelInProgress?.signal()
418-
}
402+
func tearDown() {
419403
if let s = readSource {
420-
s.setCancelHandler(handler: cancelHandler)
421404
s.cancel()
422405
}
423406
readSource = nil
424407
if let s = writeSource {
425-
s.setCancelHandler(handler: cancelHandler)
426408
s.cancel()
427409
}
428410
writeSource = nil

0 commit comments

Comments
 (0)