Creating child task after a task has been cancelled

What is the expected behavior here:

    func testChildTaskCancellation() async throws {
        let t = Task {
            await Task.yield()
            let child = Task {
                await Task.yield()
                XCTAssertTrue(Task.isCancelled) // Failure here
                try Task.checkCancellation()
            print("-- child.isCancelled: \(child.isCancelled)") // This prints "true"
            XCTAssertTrue(child.isCancelled) // This assert passes
            _ = try await child.value
        try? await t.value

A child task is created after the parent is cancelled. I had expected the child task to be created but in a cancelled state. The expectation and failure is noted in the code. Is this a bug or are my expectations incorrect? The fact that the child reports isCancelled as true leads me to believe my expectations are correct.

@ktoso any thoughts?

This is not a child task, so no.

For better or worse that's the spellings we arrived at; so no, unstructured tasks are not created cancelled.

Child tasks are:

async let ... = { child task is here }
withTaskGroup ... { group in group.addTask { child task is here } } 

Unstructured tasks are:

Task {} 
Task.detached {} 

This is the bug we fixed in your previous thread -- this is incorrectly checking Task.isCancelled (on the current task. and not on "child")

Ah, I see. For some reason I thought the only unstructured task was Task.detached {}, but now I recall. Thank you, I should've probably double checked that detail.

1 Like
Terms of Service

Privacy Policy

Cookie Policy