Skip to content

CancellableContinuation.invokeOnCancellation cause is inconsistent and breaks its own contract #2089

Open
@qwwdfsad

Description

@qwwdfsad

Steps to reproduce:

  1. This snippet prints null
suspendCancellableCoroutine<Unit> { c ->
    c.invokeOnCancellation {
        println(it) 
    }
    c.cancel()
}
  1. Semantically the same snippet prints CancellationException:
suspendCancellableCoroutine<Unit> { c ->
    c.cancel()
    c.invokeOnCancellation {
        println(it) 
    }
}

CompletionHandler contract:

 * The meaning of `cause` that is passed to the handler:
 * * Cause is `null` when the job has completed normally.
 * * Cause is an instance of [CancellationException] when the job was cancelled _normally_.
 *   **It should not be treated as an error**. In particular, it should not be reported to error logs.
 * * Otherwise, the job had _failed_.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions