Skip to content

Commit 4c0ece9

Browse files
committed
Fix race condition in PartGenerator
This commit fixes a race condition in PartGenerator, used by DefaultPartHttpMessageReader. The condition can occur when a completion signal comes in, and the state is changed to IdleFileState at the same time. Closes gh-28963
1 parent 2c75eb8 commit 4c0ece9

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

spring-web/src/main/java/org/springframework/http/codec/multipart/PartGenerator.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -746,8 +746,15 @@ public void body(DataBuffer dataBuffer) {
746746

747747
@Override
748748
public void partComplete(boolean finalPart) {
749-
this.completed = true;
750-
this.finalPart = finalPart;
749+
State state = PartGenerator.this.state.get();
750+
// writeComplete might have changed our state to IdleFileState
751+
if (state != this) {
752+
state.partComplete(finalPart);
753+
}
754+
else {
755+
this.completed = true;
756+
this.finalPart = finalPart;
757+
}
751758
}
752759

753760
public void writeBuffer(DataBuffer dataBuffer) {
@@ -771,14 +778,16 @@ public void writeBuffers(Iterable<DataBuffer> dataBuffers) {
771778

772779
private void writeComplete() {
773780
IdleFileState newState = new IdleFileState(this);
774-
if (this.completed) {
775-
newState.partComplete(this.finalPart);
776-
}
777-
else if (this.disposed) {
781+
if (this.disposed) {
778782
newState.dispose();
779783
}
780784
else if (changeState(this, newState)) {
781-
requestToken();
785+
if (this.completed) {
786+
newState.partComplete(this.finalPart);
787+
}
788+
else {
789+
requestToken();
790+
}
782791
}
783792
else {
784793
MultipartUtils.closeChannel(this.channel);

0 commit comments

Comments
 (0)