Skip to content

Commit afd67a0

Browse files
committed
Fix DataBufferUtils::write AsynchronousFileChannel race condition
See gh-29943
1 parent dcadddd commit afd67a0

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferUtils.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,23 +1159,22 @@ protected void hookOnComplete() {
11591159

11601160
@Override
11611161
public void completed(Integer written, Attachment attachment) {
1162-
this.writing.set(false);
1163-
attachment.iterator().close();
1162+
DataBuffer.ByteBufferIterator iterator = attachment.iterator();
1163+
iterator.close();
11641164

11651165
long pos = this.position.addAndGet(written);
11661166
ByteBuffer byteBuffer = attachment.byteBuffer();
1167-
DataBuffer.ByteBufferIterator iterator = attachment.iterator();
1167+
11681168
if (byteBuffer.hasRemaining()) {
1169-
this.writing.set(true);
11701169
this.channel.write(byteBuffer, pos, attachment, this);
11711170
}
11721171
else if (iterator.hasNext()) {
11731172
ByteBuffer next = iterator.next();
1174-
this.writing.set(true);
11751173
this.channel.write(next, pos, attachment, this);
11761174
}
11771175
else {
1178-
sinkDataBuffer(attachment.dataBuffer());
1176+
this.sink.next(attachment.dataBuffer());
1177+
this.writing.set(false);
11791178

11801179
Throwable throwable = this.error.get();
11811180
if (throwable != null) {
@@ -1192,15 +1191,12 @@ else if (this.completed.get()) {
11921191

11931192
@Override
11941193
public void failed(Throwable exc, Attachment attachment) {
1195-
this.writing.set(false);
11961194
attachment.iterator().close();
11971195

1198-
sinkDataBuffer(attachment.dataBuffer());
1199-
this.sink.error(exc);
1200-
}
1196+
this.sink.next(attachment.dataBuffer());
1197+
this.writing.set(false);
12011198

1202-
private void sinkDataBuffer(DataBuffer dataBuffer) {
1203-
this.sink.next(dataBuffer);
1199+
this.sink.error(exc);
12041200
}
12051201

12061202
@Override

0 commit comments

Comments
 (0)