Skip to content

Introducing empty onCompletion operator with callbackFlow changes result #3459

Open
@SanjayVas

Description

@SanjayVas

I've got some code using callbackFlow with buffer(Channel.RENDEZVOUS). There's a deadlock elsewhere in my code that I believe is triggered by something in my awaitClose block not completing. Introducing an onCompletion operator, even an empty one, before the buffer call avoids the deadlock. Not having it, or introducing it after buffer, results in the deadlock.

If I'm understanding how the operators are supposed to work, I don't think the presence/placement of an empty onCompletion operator should affect the result.

This is on Kotlin compiler 1.6.21 with kotlinx.coroutines.core 1.6.2 and language level 1.5.

Code sample:

    callbackFlow<Struct> {
      // Some callback code that calls send/close.

      awaitClose {
        if (!coroutineContext.isActive) {
          // Cancel the operation that calls the callback.
        }

        // Clean up the object that calls the callback.
      }
    }
    .onCompletion {
      // For some reason, having an onCompletion specified here is significant.
    }
    .buffer(Channel.RENDEZVOUS)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions