@@ -135,7 +135,8 @@ internal class ByteBufferChannel(
135
135
}
136
136
137
137
if (avr >= minReadSize) resumeReadOp()
138
- if (avw >= minWriteSize) resumeWriteOp()
138
+ val joining = joining
139
+ if (avw >= minWriteSize && (joining == null || state == = ReadWriteBufferState .Terminated )) resumeWriteOp()
139
140
}
140
141
141
142
override fun flush () {
@@ -1215,7 +1216,7 @@ internal class ByteBufferChannel(
1215
1216
1216
1217
var partSize = 0
1217
1218
1218
- val rc = src.reading { srcState ->
1219
+ src.reading { srcState ->
1219
1220
val srcBuffer = this
1220
1221
1221
1222
val rem = minOf(srcBuffer.remaining().toLong(), dstBuffer.remaining().toLong(), limit - copied).toInt()
@@ -1236,7 +1237,7 @@ internal class ByteBufferChannel(
1236
1237
true
1237
1238
}
1238
1239
1239
- if (rc ) {
1240
+ if (partSize > 0 ) {
1240
1241
dstBuffer.bytesWritten(state, partSize)
1241
1242
copied + = partSize
1242
1243
@@ -1266,7 +1267,7 @@ internal class ByteBufferChannel(
1266
1267
}
1267
1268
1268
1269
if (joining != null ) {
1269
- yield ( )
1270
+ tryWriteSuspend( 1 )
1270
1271
}
1271
1272
}
1272
1273
@@ -2038,16 +2039,17 @@ internal class ByteBufferChannel(
2038
2039
}
2039
2040
2040
2041
private fun resumeWriteOp () {
2041
- WriteOp .getAndSet(this , null )?.apply {
2042
+ while (true ) {
2043
+ val writeOp = writeOp ? : return
2042
2044
val closed = closed
2043
- if (closed == null ) resume(Unit ) else resumeWithException(closed.sendException)
2045
+ if (closed == null && joining != null && state != = ReadWriteBufferState .Terminated ) return
2046
+ if (WriteOp .compareAndSet(this , writeOp, null )) {
2047
+ if (closed == null ) writeOp.resume(Unit ) else writeOp.resumeWithException(closed.sendException)
2048
+ return
2049
+ }
2044
2050
}
2045
2051
}
2046
2052
2047
- private fun resumeWriteOp (cause : Throwable ) {
2048
- WriteOp .getAndSet(this , null )?.resumeWithException(cause)
2049
- }
2050
-
2051
2053
private fun resumeClosed (cause : Throwable ? ) {
2052
2054
ReadOp .getAndSet(this , null )?.let { c ->
2053
2055
if (cause != null )
0 commit comments