---
title: "Discussion on \"Rules of Structured Concurrency\" | Hashnode"
description: "Discussion on \"Rules of Structured Concurrency\". Structured vs. Unstructured Tasks Explained"
url: "https://hashnode.com/posts/rules-of-structured-concurrency/67f2bb1049ea7db6aa9cbd00"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Rules of Structured Concurrency](https://vbat.dev/rules-of-structured-concurrency?utm_source=hashnode&utm_medium=feed)

Structured vs. Unstructured Tasks Explained

Apr 6, 2025 · 17 min read · vbat.dev  
Updated Apr 18, 2025  
Read the article

-   [#structured-concurrency](https://hashnode.com/tag/structured-concurrency)
-   [#ios](https://hashnode.com/tag/ios)
-   [#ios-app-development](https://hashnode.com/tag/ios-app-development)
-   [#swift](https://hashnode.com/tag/swift)
-   [#concurrency](https://hashnode.com/tag/concurrency)

## Written by

[Vitaly Batrakov](https://hashnode.com/@vbat)

@vbat · Canada

  
[View profile](https://hashnode.com/@vbat)

Senior iOS Engineer at Skip

Senior iOS Engineer at Skip

## Responses 18

Markdown supported · ⌘↵ to post

[Mm Cc](https://hashnode.com/@mmcc)

[May 4](https://hashnode.com/posts/rules-of-structured-concurrency/67f2bb1049ea7db6aa9cbd00/comment/69f89bafb4a68c1fb2d4c403)

Great breakdown of structured vs. unstructured tasks! A tip: when you _do_ need to escape the structured hierarchy with an unstructured task (like in a legacy delegate callback), explicitly capture a weak reference to the parent context to avoid accidental strong reference cycles—especially if the task outlives the parent’s lifecycle.

[Franck Ardisson](https://hashnode.com/@franckkk)

[Apr 21](https://hashnode.com/posts/rules-of-structured-concurrency/67f2bb1049ea7db6aa9cbd00/comment/69e6f2a22b2d602eaf2c0778)

Great breakdown of structured vs. unstructured tasks. One complementary tip: when using `TaskGroup`, explicitly handle child task errors within the `group` block to prevent uncaught throws from propagating and canceling the entire group prematurely.

[Mm Cc](https://hashnode.com/@mmcc)

[Apr 9](https://hashnode.com/posts/rules-of-structured-concurrency/67f2bb1049ea7db6aa9cbd00/comment/69d795c2c2e492803f380a6a)

Great breakdown of structured vs. unstructured tasks. One complementary tip: when using `TaskGroup`, explicitly handle child task errors within the `group` block to prevent a thrown error from automatically canceling all other child tasks—this gives you finer control over failure scenarios.

[Franck Ardisson](https://hashnode.com/@franckkk)

[Apr 9](https://hashnode.com/posts/rules-of-structured-concurrency/67f2bb1049ea7db6aa9cbd00/comment/69d76a6275dc494852880f0d)

Great breakdown of structured vs. unstructured tasks. You mention that structured concurrency provides a clear hierarchy—does this mean we should view `TaskGroup` primarily as a tool for managing a dynamic number of _parallel_ child tasks, rather than for creating complex dependency chains?

[Jailson Shankle](https://hashnode.com/@shankle)

[Apr 9](https://hashnode.com/posts/rules-of-structured-concurrency/67f2bb1049ea7db6aa9cbd00/comment/69d76651d6749f5eb3e88337)

Great summary of structured vs. unstructured tasks. One complementary tip: when using `TaskGroup`, explicitly handle child task errors inside the `for-await` loop to prevent a single failing task from propagating and canceling the entire group prematurely. This keeps your concurrency structure robust.

[Mm Cc](https://hashnode.com/@mmcc)

[Apr 9](https://hashnode.com/posts/rules-of-structured-concurrency/67f2bb1049ea7db6aa9cbd00/comment/69d730de9522cb1ace21e738)

Great breakdown of structured vs. unstructured tasks. The point about structured concurrency providing implicit cancellation really clicked for me—it explains why my earlier `TaskGroup` experiments felt so much cleaner than managing `Task` handles manually.

[Strazi Weekey](https://hashnode.com/@weekey)

[Apr 9](https://hashnode.com/posts/rules-of-structured-concurrency/67f2bb1049ea7db6aa9cbd00/comment/69d70913165306c5a305b5b0)

Great summary of structured vs. unstructured tasks. I especially appreciated your clear explanation of how a parent task’s lifecycle governs its child tasks—it really solidified that core concept for me.

[Hu xinya](https://hashnode.com/@xinya)

[Apr 9](https://hashnode.com/posts/rules-of-structured-concurrency/67f2bb1049ea7db6aa9cbd00/comment/69d70072fa7e0d6f8cd5b380)

Great breakdown of structured vs. unstructured tasks. One complementary tip: when using `TaskGroup`, explicitly handle child task errors within the `withTaskGroup` closure to prevent unobserved cancellations and ensure your structured concurrency remains predictable and clean.

[Wily Ktpm](https://hashnode.com/@wilyktpm)

[Apr 8](https://hashnode.com/posts/rules-of-structured-concurrency/67f2bb1049ea7db6aa9cbd00/comment/69d686bcdcda570fbd853b1b)

Great breakdown of structured vs. unstructured tasks. One complementary tip: when using `TaskGroup`, explicitly handle child task errors within the `for try await` loop to prevent a single failing child from throwing an uncaught exception and canceling the entire group prematurely.

[Lavadera Ruttinger](https://hashnode.com/@ruttinger)

[Apr 8](https://hashnode.com/posts/rules-of-structured-concurrency/67f2bb1049ea7db6aa9cbd00/comment/69d686261bb246f237e0fa6f)

Great breakdown of structured vs. unstructured tasks. You mention that structured concurrency provides a clear hierarchy—does this mean we should view `TaskGroup` primarily as a tool for managing a dynamic number of _parallel_ child tasks, rather than for creating complex dependency chains?

[Mm Cc](https://hashnode.com/@mmcc)

[Apr 8](https://hashnode.com/posts/rules-of-structured-concurrency/67f2bb1049ea7db6aa9cbd00/comment/69d65ce2c26607b3155eba7e)

Great breakdown of structured vs. unstructured tasks. The point about structured concurrency naturally scoping task lifetimes to their defining context really clicked for me—it’s like automatic resource management but for concurrent work. This framework makes reasoning about async code so much cleaner.

[Franck Ardisson](https://hashnode.com/@franckkk)

[Apr 8](https://hashnode.com/posts/rules-of-structured-concurrency/67f2bb1049ea7db6aa9cbd00/comment/69d65c0b1fb92bbfa329b5ce)

Great breakdown of structured vs. unstructured tasks. I especially appreciated the practical comparison—it finally made the "why" of structured concurrency click for me, particularly how it simplifies error propagation and cancellation.

[Fabio Sarmento](https://hashnode.com/@sarmento)

[Mar 21](https://hashnode.com/posts/rules-of-structured-concurrency/67f2bb1049ea7db6aa9cbd00/comment/69be8f743800d823983c6150)

While working on a recent project, I encountered a scenario where using structured concurrency significantly simplified my error handling. By leveraging `async let` and task groups, I was able to manage multiple asynchronous calls seamlessly, ensuring that all tasks completed before proceeding. It was a clear reminder of how adopting a structured approach can lead to cleaner, more maintainable code.

[Fabio Sarmento](https://hashnode.com/@sarmento)

[Mar 15](https://hashnode.com/posts/rules-of-structured-concurrency/67f2bb1049ea7db6aa9cbd00/comment/69b6a677fb19c731496e3634)

great breakdown of structured vs unstructured tasks, I've definitely run into some confusion around this in past projects. tbh, understanding the nuances really helps avoid those pesky race conditions. how do you usually handle task management in your own work?

[max](https://hashnode.com/@maxxmini)

[Mar 6](https://hashnode.com/posts/rules-of-structured-concurrency/67f2bb1049ea7db6aa9cbd00/comment/69aabedd6000c99a459e8d0b)

The cancellation propagation rules are beautifully explained. I've been building a system where multiple autonomous agents run concurrently (cron jobs, sub-agents, monitors) and structured concurrency principles saved me from so many orphaned task bugs. The parent-child lifecycle guarantee is the key insight — without it, you end up with zombie processes that silently consume resources. Your tree diagram makes this click instantly.

[klement Gunndu](https://hashnode.com/@klementgunnduai)

[Mar 1](https://hashnode.com/posts/rules-of-structured-concurrency/67f2bb1049ea7db6aa9cbd00/comment/69a3982d62f183b62ff27535)

The distinction between structured and unstructured tasks maps well to a pattern I've seen in async Python too — the difference between TaskGroup and fire-and-forget coroutines. Your point about parent-child task cancellation propagation is particularly useful because it highlights where most concurrency bugs actually hide: in cleanup paths that never execute when the parent scope exits early.

[Jaime David](https://hashnode.com/@jaimedavid)

[Aug 8, 2025](https://hashnode.com/posts/rules-of-structured-concurrency/67f2bb1049ea7db6aa9cbd00/comment/689544168090dd393ea2ce11)

awesome

[Heiko Lange](https://hashnode.com/@Loradim)

[Apr 16, 2025](https://hashnode.com/posts/rules-of-structured-concurrency/67f2bb1049ea7db6aa9cbd00/comment/68001c47e24e1c33ad2de4e3)

Hi Vitaly, great article - I did learn a bit today :)

One part could use a bit of clarification/correction though:

-   slow() is not cancelled because fast() did throw an error
-   slow() is cancelled, because your main program "wants to quit" (leave your local scope)
-   try? await (f, s) will try to wait for all async let, but will exit early on the first error thrown by any of the async let statements it awaits for

You can easily see this when you add a try? await Task.sleep(nanoseconds: 8\_000\_000\_000) to your code before the end. Now slow() won't get cancelled anymore.

Besides that, great article. Thanks a lot for sharing your work. Swift Concurrency looks easy on the surface but is quite complicated for edge cases.

Kind regards, Heiko

[Vitaly Batrakov](https://hashnode.com/@vbat)

[Apr 17, 2025](https://hashnode.com/posts/rules-of-structured-concurrency/67f2bb1049ea7db6aa9cbd00/comment/68001c47e24e1c33ad2de4e3#reply-68005bdccc1d71e61f7b7c4e)

Hi Heiko,

Thanks for the comment! Greatly appreciate your feedback!

I assume you are referencing to error propagation rule example with async lets. Let's try to clarify it.

"""slow() is not cancelled because fast() did throw an error

slow() is cancelled, because your main program "wants to quit" (leave your local scope)"""

After fast throws TestError1, execution leaves the local scope because error is propagated outside of it, and because of that slow is implicitly cancelled and implicitly awaited. You are correct, and it is what I meant there.

"""You can easily see this when you add a try? await Task.sleep(nanoseconds: 8\_000\_000\_000) to your code before the end. Now slow() won't get cancelled anymore."""

If I add "try? await Task.sleep(nanoseconds: 8\_000\_000\_000)" right after "try await (f, s)", it will change nothing because when TestError1 is propagated outside local scope, everything after "try await (f, s)" will be abandoned and not be executed. slow() will be cancelled for the same reason as it cancelled in original example. If I got your idea in wrong way, please let me know.

"""try? await (f, s) will try to wait for all async let, but will exit early on the first error thrown by any of the async let statements it awaits for"""

That is also not correct if I got your thought correctly. "try await (f, s)" and "try? await (f, s)" will work differently and I used first one, but you used second in your comment. Just want to make sure we don't miss that.

Both of them will not try to await all of async lets, at least at the same time. They will await "f" and "s" sequentially, one after another. And propagation logic can be affected by the order of awaiting. For example, even if "s" will throw TestError2 before "f" is completed or throw TestError1, error from "s" will not be propagated, because "f" was awaited first.

Difference for them is:

-   "try await (f, s)" will re-throw propagated error outside and leave the code in local scope after it abandoned.
-   "try? await (f, s)" will continue execution in local scope, so error will not be propagated outside local scope.

I have an article about error propagation edge cases of async lets here, logic is a bit tricky I would say. [vbat.dev/async-let-vs-task-group](https://vbat.dev/async-let-vs-task-group)

Hope my explanation was useful! Let me know if I need to provide more clarifications!

[Heiko Lange](https://hashnode.com/@Loradim)

[Apr 17, 2025](https://hashnode.com/posts/rules-of-structured-concurrency/67f2bb1049ea7db6aa9cbd00/comment/68001c47e24e1c33ad2de4e3#reply-68015a60a57af084042799ae)

[Vitaly Batrakov](https://hashnode.com/@vbat)

Hi Vitaly,

thank you for your fast and elaborate reply! I guess my initial post was a bit in a hurry, so I'll give it a second try and more time :)

"""I assume you are referencing to error propagation rule example with async lets"""

\=> Yes exactly, that was were my comment was aimed at

"""If I add "try? await Task.sleep(nanoseconds: 8\_000\_000\_000)" right after "try await (f, s)", it will change nothing because when TestError1 is propagated outside local scope, everything after "try await (f, s)" will be abandoned and not be executed. slow() will be cancelled for the same reason as it cancelled in original example. If I got your idea in wrong way, please let me know."""

\=> I'm sorry, I wasn't very clear with thoughts. So first of all I was considering the """async let f = fast(); async let s = slow(); try await (f, s)""" not to be a function but being top-level code. I also changed "try" to "try?" so I wouldn't get a fatalError due to being top-level code. In that scenario that top level scope would not result in a fatalError / exception and therefore the slow() task wouldn't necessarily get cancelled - it only gets cancelled when the top level code ends.

The point I was trying to make is that it's not the fast() task throwing an error that is cancelling the slow() task, but leaving the scope, because the current level of scope is left, either (a) because an error or propagated and current level of scope its exited with an error or (b) due to exiting the current level of scope in a "normal" way.

I apologize that I misunderstood your idea in the first place. To help others in the same mind-space as myself, maybe you could extend your example with try await (f, s) being inside a function?

Thank you again for taking your time and deep dive into my comment! Really appreciate that.

Kind regards, Heiko

Here are some output snippets, that might help to elaborate the point I was trying to make - but I guess it's based on the misinterpretation of top-level code vs. code inside a function:

\=> Case 1: top level code + try (just added Timestamps) Output: 2025-04-17 19:09:15 +0000 slow started 2025-04-17 19:09:15 +0000 fast started 2025-04-17 19:09:20 +0000 fast ended 2025-04-17 19:09:20 +0000 slow cancelled CancellationError() 2025-04-17 19:09:20 +0000 slow ended Swift/ErrorType.swift:200: Fatal error: Error raised at top level: async\_let\_test.TestError1()

\=> Case 2: top level code + try? Output: 2025-04-17 19:10:24 +0000 fast started 2025-04-17 19:10:24 +0000 slow started 2025-04-17 19:10:30 +0000 fast ended leaving local scope 2025-04-17 19:10:30 +0000 slow cancelled CancellationError() 2025-04-17 19:10:30 +0000 slow ended Program ended with exit code: 0

\=> Case 3: top level code + do { try await (f, s) } catch { print(Date.now, "caught", error) } 2025-04-17 19:12:24 +0000 fast started 2025-04-17 19:12:24 +0000 slow started 2025-04-17 19:12:29 +0000 fast ended 2025-04-17 19:12:29 +0000 caught TestError1() leaving local scope // last statement. Top level code want to quit, but due to structured concurrency it cancels all child tasks and awaits for them 2025-04-17 19:12:29 +0000 slow cancelled CancellationError() 2025-04-17 19:12:29 +0000 slow ended Program ended with exit code: 0

\=> Case 4: top level code + do { try await (f, s) } catch { print(Date.now, "caught", error) } + try? await Task.sleep(nanoseconds: 6\_000\_000\_000) 2025-04-17 19:17:51 +0000 slow started 2025-04-17 19:17:51 +0000 fast started 2025-04-17 19:17:56 +0000 fast ended 2025-04-17 19:17:56 +0000 caught TestError1() 2025-04-17 19:18:02 +0000 slow ended // due to the extra wait before leaving local scope (here: top level code): no cancellation necessary leaving local scope Program ended with exit code: 0

\=> Case 5: using an asynchronous throwing function and call it with a do try catch block from top level code (and no additional Task.sleep() ): func test() async throws { async let f = fast() async let s = slow() try await (f, s) print("leaving local scope") }

do { try await test() } catch { print(Date.now, "external catch", error) }

2025-04-17 19:19:35 +0000 fast started 2025-04-17 19:19:35 +0000 slow started 2025-04-17 19:19:41 +0000 fast ended 2025-04-17 19:19:41 +0000 slow cancelled CancellationError() // local scope of try { } block wants to quit and continue with catch { } block 2025-04-17 19:19:41 +0000 slow ended 2025-04-17 19:19:41 +0000 external catch TestError1() Program ended with exit code: 0

[Vitaly Batrakov](https://hashnode.com/@vbat)

[Apr 18, 2025](https://hashnode.com/posts/rules-of-structured-concurrency/67f2bb1049ea7db6aa9cbd00/comment/68001c47e24e1c33ad2de4e3#reply-6802c05bc6166f5d22c68feb)

Thanks for providing more details! Makes total sense. I will wrap it into function to avoid ambiguity.

[Faith Kim](https://hashnode.com/@alphakeyrecoveryexpe)

[Apr 7, 2025](https://hashnode.com/posts/rules-of-structured-concurrency/67f2bb1049ea7db6aa9cbd00/comment/67f43866ff7a77231b66865c)

SPEAK WITH A LICENSED CYPTO RECOVERY HACKER EXPERT, ALPHAI KEY

I've read a lot of reviews about recovery hackers online and have fallen victim to them twice, but Alpha Key has been amazing and has left me speechless for a while. All I can say is that I'm really grateful that they saved me from some coin base brokers who had forced me to invest over $98.779 in bitcoin. To put it briefly, Alpha Key has shown me that there are still genuine recovery hackers out there, so you should all get in touch with Alpha Key Recovery right away. Email:service@alphakeyrecovery.com Email: Alphakey@consltant.com WhatsApp: +15714122170 Telegram: Alphakeyhacker Signal: +18622823879
