swift: Crash in Swift 6.1 when using async let inside a do {} block (Xcode 16.3+)
Description
In Swift 6.1 (Xcode 16.3 and 16.4), placing async let bindings inside a do block causes a runtime crash—even when those values are properly awaited and the code is enclosed in do-catch. The crash does not occur in Xcode 16.2 or earlier.
This behavior seems to be related to how task deallocation is handled for async let when declared inside lexical scopes.
Thanks to @IrixVol for sharing the reproducible project: LetAsyncCrash
Reproduction
func crashingAsyncLetUsage() async {
do {
async let value1 = fetchValue1()
async let value2 = fetchValue2()
let result1 = try await value1()
let result2 = try await value2()
let model = buildModel(result1, result2)
await consume(model)
} catch {
print("Caught error: \(error)")
}
}
func safeAsyncLetUsage() async {
async let value1 = fetchValue1()
async let value2 = fetchValue2()
do {
let result1 = try await value1()
let result2 = try await value2()
let model = buildModel(result1, result2)
await consume(model)
} catch {
print("Caught error: \(error)")
}
}
Stack dump
Thread 12 Crashed:
0 libsystem_kernel.dylib 0x102628874 __pthread_kill + 8
1 libsystem_pthread.dylib 0x1015be2ec pthread_kill + 264
2 libsystem_c.dylib 0x180170568 __abort + 112
3 libsystem_c.dylib 0x1801704f8 abort + 116
4 libswift_Concurrency.dylib 0x2493a346c swift::swift_Concurrency_fatalErrorv(unsigned int, char const*, char*) + 28
5 libswift_Concurrency.dylib 0x2493a3488 swift::swift_Concurrency_fatalError(unsigned int, char const*, ...) + 28
6 libswift_Concurrency.dylib 0x2493a6718 swift_task_dealloc + 124
7 libswift_Concurrency.dylib 0x2493a27a4 asyncLet_finish_after_task_completion(swift::AsyncContext*, swift::AsyncLet*, void (swift::AsyncContext* swift_async_context) swiftasynccall*, swift::AsyncContext*, void*) + 72
8 Dev.debug.dylib 0x113f12529 implicit closure #6 in implicit closure #5 in implicit closure #4 in FooViewModel.sendAction<A>(_:) + 1
9 Dev.debug.dylib 0x113f1cddd partial apply for implicit closure #6 in implicit closure #5 in implicit closure #4 in FooViewModel.sendAction<A>(_:) + 1
10 Dev.debug.dylib 0x113ec8d6d thunk for @escaping @callee_guaranteed @async () -> () + 1
11 Dev.debug.dylib 0x113f1ccf9 partial apply for thunk for @escaping @callee_guaranteed @async () -> () + 1
12 Dev.debug.dylib 0x113f0f959 FooViewModel.sendAction<A>(_:) + 1 (FooViewModel.swift:)
13 Dev.debug.dylib 0x113f1b5c1 protocol witness for ActionCaster.sendAction<A>(_:) in conformance FooViewModel + 1
14 Dev.debug.dylib 0x113f0b10d closure #1 in FooViewModel.viewWillAppear(_:) + 1 (FooViewModel.swift:)
15 Dev.debug.dylib 0x113f0b2c5 partial apply for closure #1 in FooViewController.viewWillAppear(_:) + 1
16 Dev.debug.dylib 0x113e74cbd thunk for @escaping @isolated(any) @callee_guaranteed @async () -> (@out A) + 1
17 Dev.debug.dylib 0x113e74e29 partial apply for thunk for @escaping @isolated(any) @callee_guaranteed @async () -> (@out A) + 1
18 libswift_Concurrency.dylib 0x2493a5829 completeTaskWithClosure(swift::AsyncContext*, swift::SwiftError*) + 1
Expected behavior
async let declared inside a do block should behave the same as when declared outside it, as long as all values are awaited. It should not result in a crash.
Environment
swift-driver version: 1.120.5 Apple Swift version 6.1.2 (swiftlang-6.1.2.1.2 clang-1700.0.13.5) Target: arm64-apple-macosx15.0
Additional information
This may be a regression introduced by recent changes to task deallocation logic in Swift 6.1. The crash appears related to how async let scopes are finalized when declared in lexical blocks like do {}.
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 4
- Comments: 17 (8 by maintainers)
Hi folks, chiming in to say we’re experiencing this in our iOS app as well, it appears to be impacting Swift 6.1 and 6.2 so far, preventing us to update past Xcode 16.2 (confirmed crashing with Xcode 16.3, 16.4 and the two Xcode 26 betas so far).
Given that this issue breaks the memory safety expectations of the Swift language, I hope it’s being treated with the upmost urgency.
If anyone discovers a workaround please share.
Nice, that crashes out of the box for me. Thank you!
Swift Concurrency Bug
I prepared small one-screen app, where this error stably appears. To call crash run this app on Xcode 16.2/16.4 simulator iPhone 11 iOS 18.3.1. Example of stable crash with using let async freed pointer was not the last allocation Swift:
https://github.com/IrixVol/LetAsyncCrash
Please note that the error will only occur when the minimum target is less than or equal to 15.
@jpsim I found that wrapping the crashing async let / await code inside an await { }() block seems to prevent the crash. Hope this helps anyone else running into the same issue!
I noticed, this bug appears and disappears with random code changes around async let. For example, in my app bug disappears if change/remove some fields in Result structure. Also, bug disappear if upgrade minimal target to iOS16.
It seems for me like a memory layout optimization which works differently in different iOS versions, but some functions don’t take this into account.
CC: @nate-chandler
@IrixVol Of course! I’ve added your link to the description — really appreciate the time you spent isolating the issue 🙏
Could you add my link to your description 🙏 please?! I spent a lot of time to localize the problem.
@IrixVol I tested it on Xcode 16.4 and was able to reproduce the crash. Thanks for sharing the example!
Understood, often these things can’t be shared. Hopefully we can track it down one way or another. I appreciate all the info so far.
Yeah, if it’s what I suspect, then the issue is that the compiler-emitted calls should be:
But are actually:
Sanitizers won’t help with that, since everything is fine in terms of basic memory access and thread safety.
Are you able to provide the full project? A non-minimal reproducer would be helpful too. Otherwise, I’ll keep an eye on this and see if I can figure anything out.
The good news is that this should be consistent in any given binary. If you run down this particular code path and it doesn’t encounter this error once, then you know it’s doing things in the right order and is solid, so you can have confidence in your workarounds at least.
Are you able to put together a complete example? (Reduced would be nice, but a large project that replicates the issue would be great too, if possible.) I tried adapting your code above but they don’t crash when I add some stubs to make them build.
The basic issue here is that the generated code allocates memory on the async task stack, then deallocates that memory out of order. This can be caused by issues in the code such as throwing ObjC exceptions through async Swift code, but in this case it looks likely to be a compiler bug. The changes you mention that fix the issue probably convince the compiler to deallocate the memory in the proper order.