Skip to content

Commit d379584

Browse files
authored
Workaround DiscardingTaskGroup non-conformance with nightly compilers (#478)
1 parent e62cc88 commit d379584

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

Sources/ConnectionPoolModule/ConnectionPool.swift

+14-6
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ public final class ConnectionPool<
419419

420420
@inlinable
421421
/*private*/ func makeConnection(for request: StateMachine.ConnectionRequest, in taskGroup: inout some TaskGroupProtocol) {
422-
taskGroup.addTask {
422+
taskGroup.addTask_ {
423423
self.observabilityDelegate.startedConnecting(id: request.connectionID)
424424

425425
do {
@@ -468,7 +468,7 @@ public final class ConnectionPool<
468468
/*private*/ func runKeepAlive(_ connection: Connection, in taskGroup: inout some TaskGroupProtocol) {
469469
self.observabilityDelegate.keepAliveTriggered(id: connection.id)
470470

471-
taskGroup.addTask {
471+
taskGroup.addTask_ {
472472
do {
473473
try await self.keepAliveBehavior.runKeepAlive(for: connection)
474474

@@ -503,7 +503,7 @@ public final class ConnectionPool<
503503

504504
@inlinable
505505
/*private*/ func runTimer(_ timer: StateMachine.Timer, in poolGroup: inout some TaskGroupProtocol) {
506-
poolGroup.addTask { () async -> () in
506+
poolGroup.addTask_ { () async -> () in
507507
await withTaskGroup(of: TimerRunResult.self, returning: Void.self) { taskGroup in
508508
taskGroup.addTask {
509509
do {
@@ -587,17 +587,25 @@ extension AsyncStream {
587587

588588
@usableFromInline
589589
protocol TaskGroupProtocol {
590-
mutating func addTask(operation: @escaping @Sendable () async -> Void)
590+
// We need to call this `addTask_` because some Swift versions define this
591+
// under exactly this name and others have different attributes. So let's pick
592+
// a name that doesn't clash anywhere and implement it using the standard `addTask`.
593+
mutating func addTask_(operation: @escaping @Sendable () async -> Void)
591594
}
592595

593596
#if swift(>=5.8) && os(Linux) || swift(>=5.9)
594597
@available(macOS 14.0, iOS 17.0, tvOS 17.0, watchOS 10.0, *)
595-
extension DiscardingTaskGroup: TaskGroupProtocol {}
598+
extension DiscardingTaskGroup: TaskGroupProtocol {
599+
@inlinable
600+
mutating func addTask_(operation: @escaping @Sendable () async -> Void) {
601+
self.addTask(priority: nil, operation: operation)
602+
}
603+
}
596604
#endif
597605

598606
extension TaskGroup<Void>: TaskGroupProtocol {
599607
@inlinable
600-
mutating func addTask(operation: @escaping @Sendable () async -> Void) {
608+
mutating func addTask_(operation: @escaping @Sendable () async -> Void) {
601609
self.addTask(priority: nil, operation: operation)
602610
}
603611
}

Tests/ConnectionPoolModuleTests/ConnectionPoolTests.swift

+7-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class ConnectionPoolTests: XCTestCase {
2626
// the same connection is reused 1000 times
2727

2828
await withTaskGroup(of: Void.self) { taskGroup in
29-
taskGroup.addTask {
29+
taskGroup.addTask_ {
3030
await pool.run()
3131
}
3232

@@ -82,14 +82,14 @@ final class ConnectionPoolTests: XCTestCase {
8282
}
8383

8484
await withTaskGroup(of: Void.self) { taskGroup in
85-
taskGroup.addTask {
85+
taskGroup.addTask_ {
8686
await pool.run()
8787
}
8888

8989
let (blockCancelStream, blockCancelContinuation) = AsyncStream.makeStream(of: Void.self)
9090
let (blockConnCreationStream, blockConnCreationContinuation) = AsyncStream.makeStream(of: Void.self)
9191

92-
taskGroup.addTask {
92+
taskGroup.addTask_ {
9393
_ = try? await factory.nextConnectAttempt { _ in
9494
blockCancelContinuation.yield()
9595
var iterator = blockConnCreationStream.makeAsyncIterator()
@@ -127,7 +127,7 @@ final class ConnectionPoolTests: XCTestCase {
127127
}
128128

129129
await withTaskGroup(of: Void.self) { taskGroup in
130-
taskGroup.addTask {
130+
taskGroup.addTask_ {
131131
await pool.run()
132132
}
133133

@@ -170,12 +170,12 @@ final class ConnectionPoolTests: XCTestCase {
170170
// the same connection is reused 1000 times
171171

172172
await withTaskGroup(of: Void.self) { taskGroup in
173-
taskGroup.addTask {
173+
taskGroup.addTask_ {
174174
await pool.run()
175175
XCTAssertFalse(hasFinished.compareExchange(expected: false, desired: true, ordering: .relaxed).original)
176176
}
177177

178-
taskGroup.addTask {
178+
taskGroup.addTask_ {
179179
var usedConnectionIDs = Set<Int>()
180180
for _ in 0..<config.maximumConnectionHardLimit {
181181
await factory.nextConnectAttempt { connectionID in
@@ -192,7 +192,7 @@ final class ConnectionPoolTests: XCTestCase {
192192
let (stream, continuation) = AsyncStream.makeStream(of: Void.self)
193193

194194
for _ in 0..<iterations {
195-
taskGroup.addTask {
195+
taskGroup.addTask_ {
196196
do {
197197
let leasedConnection = try await pool.leaseConnection()
198198
pool.releaseConnection(leasedConnection)

0 commit comments

Comments
 (0)