vert-x3 / vertx-lang-kotlin

Vert.x for Kotlin
Apache License 2.0
293 stars 67 forks source link

ChannelWriteStream has a bug closing underlying stream #256

Closed bbyk closed 11 months ago

bbyk commented 11 months ago

Questions

ChannelWriteStream closes the underlying stream too early

Version

Vert.x 4.4.5

Context

When you call ChannelWriteStream#close the underlying stream gets closed immediately whereas the channel might still have an item or two in it and the job reading from the channel has the opportunity to complete them and in fact is still running and might encounter the closed stream

To illustrate the point, consider the following changes:

  1. ChannelWriteStream#close just needs to close the channel
    override fun close(cause: Throwable?): Boolean {
        return channel.close(cause)
    }
  2. The reading job should close the stream instead when the channel is closed, all the remaining items are received and you exit the loop. The suggested change is to use try-finally over the original code and close the stream in the finally block.
    
        launch {
            try {
                while (true) {
                    val res = channel.receiveCatching()
                    ....
                }
            } finally {
                stream.end()
            }
        }

### Do you have a reproducer?

No, because it's a race condition.
tsegismont commented 11 months ago

@bbyk please take a look at #260 if you get a chance

tsegismont commented 11 months ago

Fixed by cd81bb6

bbyk commented 11 months ago

@tsegismont Perfect. Thanks!