Skip to content

Commit 5074896

Browse files
authored
Fixes cancelOnGracefulShutdown issue (#135) (#136)
Motivation: + cancelOnGracefulShutdown was getting stuck on macOS Modifications: + added explicit group.cancelAll() to all execution paths Co-authored-by: Simon Leeb <[email protected]>
1 parent 1ef3d96 commit 5074896

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

Sources/ServiceLifecycle/GracefulShutdown.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ public func cancelOnGracefulShutdown<T>(_ operation: @Sendable @escaping () asyn
8080
}
8181

8282
let result = try await group.next()
83+
group.cancelAll() // currently needed, as the group does not always auto-cancel tasks on return
8384

8485
switch result {
8586
case .value(let t):
8687
return t
8788
case .gracefulShutdown:
88-
group.cancelAll()
8989
switch try await group.next() {
9090
case .value(let t):
9191
return t

Tests/ServiceLifecycleTests/GracefulShutdownTests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,17 @@ final class GracefulShutdownTests: XCTestCase {
233233
}
234234
}
235235

236+
func testResumesCancelOnGracefulShutdownWithResult() async throws {
237+
await testGracefulShutdown { _ in
238+
let result = await cancelOnGracefulShutdown {
239+
await Task.yield()
240+
return "hello"
241+
}
242+
243+
XCTAssertEqual(result, "hello")
244+
}
245+
}
246+
236247
func testIsShuttingDownGracefully() async throws {
237248
await testGracefulShutdown { gracefulShutdownTestTrigger in
238249
XCTAssertFalse(Task.isShuttingDownGracefully)

0 commit comments

Comments
 (0)