Skip to content

Instantly share code, notes, and snippets.

@ole
Last active March 22, 2023 16:27
Show Gist options
  • Save ole/c68d1bef13131cad2fd2b3eb71127881 to your computer and use it in GitHub Desktop.
Save ole/c68d1bef13131cad2fd2b3eb71127881 to your computer and use it in GitHub Desktop.
Swift TaskGroup swallows errors if you use it for fire-and-forget tasks (i.e. you never await the child tasks)
struct MyError: Error {}
func fireAndForget() async {
await withThrowingTaskGroup(of: Void.self) { group in
group.addTask {
print("child task start")
print("child task throws")
throw MyError()
}
// Notice that we're not awaiting the child task.
// The task group swallows the child task's error
// without propagating it up.
}
}
await fireAndForget()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment