Skip to content

Commit d6f5d45

Browse files
Merge pull request #281 from CBenoit/fix-poll-flush
Fix poll_flush on closed connection
2 parents f31c425 + 2c3c641 commit d6f5d45

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/lib.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ where
328328
fn start_send(mut self: Pin<&mut Self>, item: Message) -> Result<(), Self::Error> {
329329
match (*self).with_context(None, |s| s.write_message(item)) {
330330
Ok(()) => Ok(()),
331-
Err(::tungstenite::Error::Io(err)) if err.kind() == std::io::ErrorKind::WouldBlock => {
331+
Err(WsError::Io(err)) if err.kind() == std::io::ErrorKind::WouldBlock => {
332332
// the message was accepted and queued
333333
// isn't an error.
334334
Ok(())
@@ -341,7 +341,19 @@ where
341341
}
342342

343343
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
344-
(*self).with_context(Some((ContextWaker::Write, cx)), |s| cvt(s.write_pending()))
344+
match (*self).with_context(Some((ContextWaker::Write, cx)), |s| s.write_pending()) {
345+
Ok(()) => Poll::Ready(Ok(())),
346+
Err(WsError::ConnectionClosed) => {
347+
// WebSocket is closing and there is nothing to send anymore.
348+
// Not an failure, the flush operation is a success.
349+
Poll::Ready(Ok(()))
350+
}
351+
Err(WsError::Io(ref e)) if e.kind() == std::io::ErrorKind::WouldBlock => {
352+
trace!("WouldBlock");
353+
Poll::Pending
354+
}
355+
Err(e) => Poll::Ready(Err(e)),
356+
}
345357
}
346358

347359
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
@@ -354,8 +366,8 @@ where
354366

355367
match res {
356368
Ok(()) => Poll::Ready(Ok(())),
357-
Err(::tungstenite::Error::ConnectionClosed) => Poll::Ready(Ok(())),
358-
Err(::tungstenite::Error::Io(err)) if err.kind() == std::io::ErrorKind::WouldBlock => {
369+
Err(WsError::ConnectionClosed) => Poll::Ready(Ok(())),
370+
Err(WsError::Io(err)) if err.kind() == std::io::ErrorKind::WouldBlock => {
359371
trace!("WouldBlock");
360372
self.closing = true;
361373
Poll::Pending

0 commit comments

Comments
 (0)