Skip to content

Commit ad68c06

Browse files
author
Sergey Mashkov
committed
IO: eliminate race in close
I knew that I'll hit this bug anyway. Here we MUST flush twice as flush at read suspend is not enough. If a writer closes then the first flush does flush and it closes so all readers will see all pending data. If a foreign thread does close then the first flush is makes no difference however the second is required for proper cleanup and to do tryTerminate
1 parent ccf8dde commit ad68c06

File tree

1 file changed

+1
-0
lines changed

1 file changed

+1
-0
lines changed

core/kotlinx-coroutines-io/src/main/kotlin/kotlinx/coroutines/experimental/io/ByteBufferChannel.kt

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ internal class ByteBufferChannel(
7171
override fun close(cause: Throwable?): Boolean {
7272
if (closed != null) return false
7373
val newClosed = if (cause == null) ClosedElement.EmptyCause else ClosedElement(cause)
74+
state.capacity.flush()
7475
if (!Closed.compareAndSet(this, null, newClosed)) return false
7576
state.capacity.flush()
7677
if (state.capacity.isEmpty() || cause != null) tryTerminate()

0 commit comments

Comments
 (0)