Chapter 4: Why do we cancel the parent task if the nested tasks will not be canceled?

In chapter 4 of the book, the observeAppStatus() method creates 2 nested tasks and you try to cancel them via canceling the parent task

let notifications = Task {
	await observeAppStatus()
}
defer {
	notifications.cancel()
}

source

Why do I need to cancel the parent task if the nested tasks will not be canceled? Do I understand correctly that nested tasks are new “top-level tasks” and will not be canceled cascaded?


Additionally, I’m wondering if such code could cause self to leak, given that nested tasks will not be canceled, and internally we are implicitly strongly capturing self and accessing the username property

func observeAppStatus() async {
	Task {
        ...
			try? await say("\(username) went away", isSystemMessage: true) // self capture
		...
	}

	Task {
        ...
			try? await say("\(username) came back", isSystemMessage: true) // self capture
		...
	}
}

source