Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes cancelOnGracefulShutdown issue (#135) #136

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/ServiceLifecycle/GracefulShutdown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ public func cancelOnGracefulShutdown<T>(_ operation: @Sendable @escaping () asyn
}

let result = try await group.next()
group.cancelAll() // currently needed, as the group does not always auto-cancel tasks on return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm the comment seems a bit off, this is not accidental the group just waits, there's no implicit cancel at end of it.

Code looks good, just rephrase the comment?

cc @FranzBusch

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh my, totally my fault, thanks for addressing this! I was already feeling bad about spreading misinformation in the github issue.

I was confusing the cancelAll behavior with this for whatever reason (pretty wild in hindsight how I could mix these up). ...oh the shame... ; )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may I offer a PR for the fix:
#137

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

Yeah I don't blame you, there's a lot of behavior to keep in your head smetimes with these... thanks!


switch result {
case .value(let t):
return t
case .gracefulShutdown:
group.cancelAll()
switch try await group.next() {
case .value(let t):
return t
Expand Down
11 changes: 11 additions & 0 deletions Tests/ServiceLifecycleTests/GracefulShutdownTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,17 @@ final class GracefulShutdownTests: XCTestCase {
}
}

func testResumesCancelOnGracefulShutdownWithResult() async throws {
await testGracefulShutdown { _ in
let result = await cancelOnGracefulShutdown {
await Task.yield()
return "hello"
}

XCTAssertEqual(result, "hello")
}
}

func testIsShuttingDownGracefully() async throws {
await testGracefulShutdown { gracefulShutdownTestTrigger in
XCTAssertFalse(Task.isShuttingDownGracefully)
Expand Down