From 93bd9c542a18708cf9acd6fb912c27ee906987ef Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Mon, 13 Feb 2023 10:04:55 +0000 Subject: [PATCH 1/2] Make syncShutdown unavailable from async Motivation syncShutdown can cause unbounded thread blocking, we shouldn't allow it in concurrent code. Modification Mark syncShutdown unavailable from async. Result Users are warned if they try to syncShutdown in an async context --- README.md | 2 +- Sources/AsyncHTTPClient/Docs.docc/index.md | 2 +- Sources/AsyncHTTPClient/HTTPClient.swift | 5 ++++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3e705d50c..27354d8da 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ httpClient.get(url: "https://apple.com/").whenComplete { result in } ``` -You should always shut down `HTTPClient` instances you created using `try httpClient.syncShutdown()`. Please note that you must not call `httpClient.syncShutdown` before all requests of the HTTP client have finished, or else the in-flight requests will likely fail because their network connections are interrupted. +You should always shut down `HTTPClient` instances you created using `try httpClient.shutdown()`. Please note that you must not call `httpClient.shutdown` before all requests of the HTTP client have finished, or else the in-flight requests will likely fail because their network connections are interrupted. ### async/await examples diff --git a/Sources/AsyncHTTPClient/Docs.docc/index.md b/Sources/AsyncHTTPClient/Docs.docc/index.md index acb408684..60e928e7d 100644 --- a/Sources/AsyncHTTPClient/Docs.docc/index.md +++ b/Sources/AsyncHTTPClient/Docs.docc/index.md @@ -71,7 +71,7 @@ httpClient.get(url: "https://apple.com/").whenComplete { result in } ``` -You should always shut down ``HTTPClient`` instances you created using ``HTTPClient/syncShutdown()``. Please note that you must not call ``HTTPClient/syncShutdown()`` before all requests of the HTTP client have finished, or else the in-flight requests will likely fail because their network connections are interrupted. +You should always shut down ``HTTPClient`` instances you created using ``HTTPClient/shutdown()-96ayw()``. Please note that you must not call ``HTTPClient/shutdown()-96ayw()`` before all requests of the HTTP client have finished, or else the in-flight requests will likely fail because their network connections are interrupted. ### async/await examples diff --git a/Sources/AsyncHTTPClient/HTTPClient.swift b/Sources/AsyncHTTPClient/HTTPClient.swift index 1089db86c..93c8384c7 100644 --- a/Sources/AsyncHTTPClient/HTTPClient.swift +++ b/Sources/AsyncHTTPClient/HTTPClient.swift @@ -139,7 +139,7 @@ public class HTTPClient { """) case .upAndRunning: preconditionFailure(""" - Client not shut down before the deinit. Please call client.syncShutdown() when no \ + Client not shut down before the deinit. Please call client.shutdown() when no \ longer needed. Otherwise memory will leak. """) } @@ -147,6 +147,9 @@ public class HTTPClient { } /// Shuts down the client and `EventLoopGroup` if it was created by the client. + /// + /// This method blocks the thread indefinitely, prefer using ``shutdown()-96ayw``. + @available(*, noasync, message: "syncShutdown() can block indefinitely, prefer shutdown()", renamed: "shutdown()") public func syncShutdown() throws { try self.syncShutdown(requiresCleanClose: false) } From c833b5021eac0823624eae85f7f24aa451bc72ca Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Mon, 13 Feb 2023 17:19:54 +0000 Subject: [PATCH 2/2] Only noasync on 5.7 --- Sources/AsyncHTTPClient/HTTPClient.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Sources/AsyncHTTPClient/HTTPClient.swift b/Sources/AsyncHTTPClient/HTTPClient.swift index 93c8384c7..2f4368402 100644 --- a/Sources/AsyncHTTPClient/HTTPClient.swift +++ b/Sources/AsyncHTTPClient/HTTPClient.swift @@ -146,6 +146,7 @@ public class HTTPClient { } } + #if swift(>=5.7) /// Shuts down the client and `EventLoopGroup` if it was created by the client. /// /// This method blocks the thread indefinitely, prefer using ``shutdown()-96ayw``. @@ -153,6 +154,14 @@ public class HTTPClient { public func syncShutdown() throws { try self.syncShutdown(requiresCleanClose: false) } + #else + /// Shuts down the client and `EventLoopGroup` if it was created by the client. + /// + /// This method blocks the thread indefinitely, prefer using ``shutdown()-96ayw``. + public func syncShutdown() throws { + try self.syncShutdown(requiresCleanClose: false) + } + #endif /// Shuts down the client and `EventLoopGroup` if it was created by the client. ///