-
Notifications
You must be signed in to change notification settings - Fork 9
Fix potential crash in withDeadline
#5
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the SwiftNIO open source project | ||
| // | ||
| // Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.txt for the list of SwiftNIO project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| private enum TaskResult<T: Sendable>: Sendable { | ||
| case success(T) | ||
| case error(any Error) | ||
| case timedOut | ||
| case cancelled | ||
| } | ||
|
|
||
| package struct TimeOutError: Error, CustomStringConvertible, CustomDebugStringConvertible { | ||
| var underlying: any Error | ||
|
|
||
| package var description: String { | ||
| "TimeOutError(\(String(describing: underlying))" | ||
| } | ||
|
|
||
| package var debugDescription: String { | ||
| description | ||
| } | ||
| } | ||
|
|
||
| @available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) | ||
| package func withTimeout<T: Sendable, Clock: _Concurrency.Clock>( | ||
| in timeout: Clock.Duration, | ||
| clock: Clock, | ||
| isolation: isolated (any Actor)? = #isolation, | ||
| body: sending @escaping @isolated(any) () async throws -> T | ||
| ) async throws -> T { | ||
| // This is needed so we can make body sending since we don't have call-once closures yet | ||
| let body = { body } | ||
| let result: Result<T, any Error> = await withTaskGroup(of: TaskResult<T>.self) { group in | ||
| let body = body() | ||
| group.addTask { | ||
| do { | ||
| return .success(try await body()) | ||
| } catch { | ||
| return .error(error) | ||
| } | ||
| } | ||
| group.addTask { | ||
| do { | ||
| try await clock.sleep(for: timeout, tolerance: .zero) | ||
| return .timedOut | ||
| } catch { | ||
| return .cancelled | ||
| } | ||
| } | ||
|
|
||
| switch await group.next() { | ||
| case .success(let result): | ||
| // Work returned a result. Cancel the timer task and return | ||
| group.cancelAll() | ||
| return .success(result) | ||
| case .error(let error): | ||
| // Work threw. Cancel the timer task and rethrow | ||
| group.cancelAll() | ||
| return .failure(error) | ||
| case .timedOut: | ||
| // Timed out, cancel the work task. | ||
| group.cancelAll() | ||
|
|
||
| switch await group.next() { | ||
| case .success(let result): | ||
| return .success(result) | ||
| case .error(let error): | ||
| return .failure(TimeOutError(underlying: error)) | ||
| case .timedOut, .cancelled, .none: | ||
| // We already got a result from the sleeping task so we can't get another one or none. | ||
| fatalError("Unexpected task result") | ||
| } | ||
| case .cancelled: | ||
| switch await group.next() { | ||
| case .success(let result): | ||
| return .success(result) | ||
| case .error(let error): | ||
| return .failure(TimeOutError(underlying: error)) | ||
| case .timedOut, .cancelled, .none: | ||
| // We already got a result from the sleeping task so we can't get another one or none. | ||
| fatalError("Unexpected task result") | ||
| } | ||
| case .none: | ||
| fatalError("Unexpected task result") | ||
| } | ||
| } | ||
| return try result.get() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the SwiftNIO open source project | ||
| // | ||
| // Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.txt for the list of SwiftNIO project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import NTPClient | ||
| import Testing | ||
|
|
||
| @Suite | ||
| struct TimeoutTests { | ||
| @Test | ||
| func workCompletes() async throws { | ||
| let expectedValue = "success" | ||
|
|
||
| let result = try await withTimeout(in: .seconds(1), clock: .continuous) { | ||
| expectedValue | ||
| } | ||
|
|
||
| #expect(result == expectedValue) | ||
| } | ||
|
|
||
| @Test | ||
| func workTimesOut() async throws { | ||
|
|
||
| let result = await withThrowingTaskGroup(of: Void.self) { group in | ||
| // Task to run the test | ||
| group.addTask { | ||
| _ = try await withTimeout(in: .seconds(1), clock: .continuous) { | ||
| // Task that will take longer than the timeout | ||
| try await Task.sleep(for: .seconds(10), clock: .continuous) | ||
| Issue.record("Should not be reached") | ||
| } | ||
| } | ||
|
|
||
| return await group.nextResult() | ||
| } | ||
| #expect(throws: TimeOutError.self) { | ||
| try result?.get() | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| func workThrowsError() async throws { | ||
| struct TestError: Error { | ||
| var message: String | ||
| } | ||
| await #expect(throws: TestError.self) { | ||
| _ = try await withTimeout(in: .seconds(1), clock: .continuous) { | ||
| throw TestError(message: "hi") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| func overallCancelled() async throws { | ||
| // Run a task that will not finish for a long time | ||
| let workTask = Task { | ||
| try await withTimeout(in: .seconds(100), clock: .continuous) { | ||
| try await Task.sleep(for: .seconds(10_000)) | ||
| } | ||
| } | ||
| // Cancel it | ||
| workTask.cancel() | ||
|
|
||
| // It should throw an error | ||
| await #expect(throws: (any Error).self) { | ||
| try await workTask.value | ||
| } | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.