From 1ac11332a96cb586566776a1d5e6d89d3ec647ad Mon Sep 17 00:00:00 2001 From: Lily Vulcano Date: Thu, 3 Oct 2019 14:30:40 -0700 Subject: [PATCH] Revert "[URL Session] Avoid Closing Sockets Before Cancelling Event Sources" This reverts commit 02656bf6637d75ead56d628960c02569330b99c8. Fixes SR-11557. --- .../URL.subproj/CFURLSessionInterface.c | 4 --- .../URL.subproj/CFURLSessionInterface.h | 2 +- .../URLSession/libcurl/EasyHandle.swift | 8 ------ .../URLSession/libcurl/MultiHandle.swift | 27 ++----------------- 4 files changed, 3 insertions(+), 38 deletions(-) diff --git a/CoreFoundation/URL.subproj/CFURLSessionInterface.c b/CoreFoundation/URL.subproj/CFURLSessionInterface.c index 35f820ebe1..31a91a0ece 100644 --- a/CoreFoundation/URL.subproj/CFURLSessionInterface.c +++ b/CoreFoundation/URL.subproj/CFURLSessionInterface.c @@ -110,10 +110,6 @@ CFURLSessionEasyCode CFURLSession_easy_setopt_tc(CFURLSessionEasyHandle _Nonnull return MakeEasyCode(curl_easy_setopt(curl, option.value, a)); } -CFURLSessionEasyCode CFURLSession_easy_setopt_csf(CFURLSessionEasyHandle _Nonnull curl, CFURLSessionOption option, int (*_Nonnull a)(void *_Nullable clientp, CFURLSession_socket_t)) { - return MakeEasyCode(curl_easy_setopt(curl, option.value, a)); -} - CFURLSessionEasyCode CFURLSession_easy_getinfo_long(CFURLSessionEasyHandle _Nonnull curl, CFURLSessionInfo info, long *_Nonnull a) { return MakeEasyCode(curl_easy_getinfo(curl, info.value, a)); } diff --git a/CoreFoundation/URL.subproj/CFURLSessionInterface.h b/CoreFoundation/URL.subproj/CFURLSessionInterface.h index 47cf08a0fc..816212d8f6 100644 --- a/CoreFoundation/URL.subproj/CFURLSessionInterface.h +++ b/CoreFoundation/URL.subproj/CFURLSessionInterface.h @@ -601,7 +601,7 @@ CF_EXPORT CFURLSessionMultiCode CFURLSession_multi_setopt_ptr(CFURLSessionMultiH CF_EXPORT CFURLSessionMultiCode CFURLSession_multi_setopt_l(CFURLSessionMultiHandle _Nonnull multi_handle, CFURLSessionMultiOption option, long a); CF_EXPORT CFURLSessionMultiCode CFURLSession_multi_setopt_sf(CFURLSessionMultiHandle _Nonnull multi_handle, CFURLSessionMultiOption option, int (*_Nonnull a)(CFURLSessionEasyHandle _Nonnull, CFURLSession_socket_t, int, void *_Nullable, void *_Nullable)); CF_EXPORT CFURLSessionMultiCode CFURLSession_multi_setopt_tf(CFURLSessionMultiHandle _Nonnull multi_handle, CFURLSessionMultiOption option, int (*_Nonnull a)(CFURLSessionMultiHandle _Nonnull, long, void *_Nullable)); -CF_EXPORT CFURLSessionEasyCode CFURLSession_easy_setopt_csf(CFURLSessionEasyHandle _Nonnull curl, CFURLSessionOption option, int (*_Nonnull a)(void* _Nullable clientp, CFURLSession_socket_t)); + CF_EXPORT CFURLSessionEasyCode CFURLSessionInit(void); diff --git a/Foundation/URLSession/libcurl/EasyHandle.swift b/Foundation/URLSession/libcurl/EasyHandle.swift index 9ab572a386..65e124c540 100644 --- a/Foundation/URLSession/libcurl/EasyHandle.swift +++ b/Foundation/URLSession/libcurl/EasyHandle.swift @@ -486,14 +486,6 @@ fileprivate extension _EasyHandle { return 1 } }.asError() - - try! CFURLSession_easy_setopt_csf(rawHandle, CFURLSessionOptionCLOSESOCKETFUNCTION) { (clientp: UnsafeMutableRawPointer?, socket: CFURLSession_socket_t) -> Int32 in - // Don't let CURL close the socket here because the - // dispatch sources are associated with it and we need to - // cancel them before closing the file descriptor. - return 0 - }.asError() - // seeking in input stream try! CFURLSession_easy_setopt_ptr(rawHandle, CFURLSessionOptionSEEKDATA, UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())).asError() try! CFURLSession_easy_setopt_seek(rawHandle, CFURLSessionOptionSEEKFUNCTION, { (userdata, offset, origin) -> Int32 in diff --git a/Foundation/URLSession/libcurl/MultiHandle.swift b/Foundation/URLSession/libcurl/MultiHandle.swift index 8a389c5212..13d246e8f1 100644 --- a/Foundation/URLSession/libcurl/MultiHandle.swift +++ b/Foundation/URLSession/libcurl/MultiHandle.swift @@ -122,9 +122,7 @@ fileprivate extension URLSession._MultiHandle { } else if socketSources != nil && action == .unregister { // We need to release the stored pointer: if let opaque = socketSourcePtr { - let s: Unmanaged<_SocketSources> = Unmanaged<_SocketSources>.fromOpaque(opaque) - s.takeUnretainedValue().tearDown(socket: socket, queue: queue) - s.release() + Unmanaged<_SocketSources>.fromOpaque(opaque).release() } socketSources = nil } @@ -409,7 +407,6 @@ fileprivate extension URLSession._MultiHandle._Timeout { fileprivate class _SocketSources { var readSource: DispatchSource? var writeSource: DispatchSource? - let activeSockets = DispatchGroup() func createReadSource(socket: CFURLSession_socket_t, queue: DispatchQueue, handler: DispatchWorkItem) { guard readSource == nil else { return } @@ -418,13 +415,7 @@ fileprivate class _SocketSources { #else let s = DispatchSource.makeReadSource(fileDescriptor: socket, queue: queue) #endif - activeSockets.enter() s.setEventHandler(handler: handler) - s.setCancelHandler(handler: DispatchWorkItem { [weak self] in - guard let self = self else { return } - self.activeSockets.leave() - }) - readSource = s as? DispatchSource s.resume() } @@ -436,26 +427,12 @@ fileprivate class _SocketSources { #else let s = DispatchSource.makeWriteSource(fileDescriptor: socket, queue: queue) #endif - activeSockets.enter() - s.setCancelHandler(handler: DispatchWorkItem { [weak self] in - guard let self = self else { return } - self.activeSockets.leave() - }) s.setEventHandler(handler: handler) writeSource = s as? DispatchSource s.resume() } - func tearDown(socket: CFURLSession_socket_t, queue: DispatchQueue) { - activeSockets.notify(queue: queue) { - withExtendedLifetime(self) { -#if os(Windows) - closesocket(socket) -#else - close(socket) -#endif - } - } + func tearDown() { if let s = readSource { s.cancel() }