Skip to content

Commit e67aba9

Browse files
authored
Merge pull request #327 from surban/flush-complete
encoder: ensure that flush is finished
2 parents 04141de + c6d9531 commit e67aba9

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/tokio/write/generic/encoder.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use tokio::io::{AsyncBufRead, AsyncRead, AsyncWrite, ReadBuf};
1616
#[derive(Debug)]
1717
enum State {
1818
Encoding,
19+
Flushing,
1920
Finishing,
2021
Done,
2122
}
@@ -80,6 +81,12 @@ impl<W: AsyncWrite, E: Encode> Encoder<W, E> {
8081
State::Encoding
8182
}
8283

84+
// Once a flush has been started, it must be completed.
85+
State::Flushing => match this.encoder.flush(&mut output)? {
86+
true => State::Encoding,
87+
false => State::Flushing,
88+
},
89+
8390
State::Finishing | State::Done => {
8491
return Poll::Ready(Err(io::Error::new(
8592
io::ErrorKind::Other,
@@ -105,7 +112,7 @@ impl<W: AsyncWrite, E: Encode> Encoder<W, E> {
105112
let mut output = PartialBuffer::new(output);
106113

107114
let done = match this.state {
108-
State::Encoding => this.encoder.flush(&mut output)?,
115+
State::Encoding | State::Flushing => this.encoder.flush(&mut output)?,
109116

110117
State::Finishing | State::Done => {
111118
return Poll::Ready(Err(io::Error::new(
@@ -114,11 +121,13 @@ impl<W: AsyncWrite, E: Encode> Encoder<W, E> {
114121
)))
115122
}
116123
};
124+
*this.state = State::Flushing;
117125

118126
let produced = output.written().len();
119127
this.writer.as_mut().produce(produced);
120128

121129
if done {
130+
*this.state = State::Encoding;
122131
return Poll::Ready(Ok(()));
123132
}
124133
}
@@ -140,6 +149,12 @@ impl<W: AsyncWrite, E: Encode> Encoder<W, E> {
140149
}
141150
}
142151

152+
// Once a flush has been started, it must be completed.
153+
State::Flushing => match this.encoder.flush(&mut output)? {
154+
true => State::Finishing,
155+
false => State::Flushing,
156+
},
157+
143158
State::Done => State::Done,
144159
};
145160

0 commit comments

Comments
 (0)