Skip to content

Concurrency: Don't rely on future wait context in asyncLet_finish. #38911

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 1 commit into from
Aug 18, 2021
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
10 changes: 7 additions & 3 deletions stdlib/public/Concurrency/AsyncLet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,13 @@ static void swift_asyncLet_getImpl(SWIFT_ASYNC_CONTEXT AsyncContext *callerConte
}

struct AsyncLetContinuationContext: AsyncContext {
AsyncLet *alet;
AsyncLet *alet;
OpaqueValue *resultBuffer;
};

static_assert(sizeof(AsyncLetContinuationContext) <= sizeof(TaskFutureWaitAsyncContext),
"compiler provides the same amount of context space to each");

SWIFT_CC(swiftasync)
static void _asyncLet_get_throwing_continuation(
SWIFT_ASYNC_CONTEXT AsyncContext *callContext,
Expand Down Expand Up @@ -369,8 +373,7 @@ static void _asyncLet_finish_continuation(
auto continuationContext
= reinterpret_cast<AsyncLetContinuationContext*>(callContext);
auto alet = continuationContext->alet;
auto resultBuffer = asImpl(alet)->getFutureContext()
->successResultPointer;
auto resultBuffer = continuationContext->resultBuffer;

// Destroy the error, or the result that was stored to the buffer.
if (error) {
Expand Down Expand Up @@ -415,6 +418,7 @@ static void swift_asyncLet_finishImpl(SWIFT_ASYNC_CONTEXT AsyncContext *callerCo
aletContext->Parent = callerContext;
aletContext->ResumeParent = resumeFunction;
aletContext->alet = alet;
aletContext->resultBuffer = reinterpret_cast<OpaqueValue*>(resultBuffer);
auto futureContext = asImpl(alet)->getFutureContext();

// TODO: It would be nice if we could await the future without having to
Expand Down
28 changes: 28 additions & 0 deletions test/Concurrency/Runtime/async_let_throw_completion_order.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// rdar://81481317
// RUN: %target-run-simple-swift(-Xfrontend -disable-availability-checking %import-libdispatch -parse-as-library) | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: concurrency
// REQUIRES: libdispatch
struct Bad: Error {}

class Foo { init() async throws {}; deinit { print("Foo down") } }
class Bar { init() async throws { throw Bad() }; deinit { print("Bar down") } }
class Baz { init() async throws {}; deinit { print("Baz down") } }

func zim(y: Bar, x: Foo, z: Baz) { print("hooray") }

@main struct Butt {

static func main() async {
do {
async let x = Foo()
async let y = Bar()
async let z = Baz()

return try await zim(y: y, x: x, z: z)
} catch {
// CHECK: oopsie woopsie
print("oopsie woopsie")
}
}
}