Skip to content

Commit 1329c39

Browse files
Andrii Grynenkofacebook-github-bot
authored andcommitted
Make client process all buffered reads on write failure
Summary: When connection is reset by server it's possible for a write to fail before the read callback is called. Make sure we still try to process all the data that was received before closing the socket. Reviewed By: rhodo Differential Revision: D19851139 fbshipit-source-id: af50b939ccd7f25384e48bcf29b4892a23c2ef83
1 parent f395768 commit 1329c39

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

folly/io/async/AsyncSocket.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2747,13 +2747,17 @@ void AsyncSocket::failWrite(
27472747
VLOG(4) << "AsyncSocket(this=" << this << ", fd=" << fd_
27482748
<< ", state=" << state_ << " host=" << addr_.describe()
27492749
<< "): failed while writing in " << fn << "(): " << ex.what();
2750-
startFail();
2750+
if (closeOnFailedWrite_) {
2751+
startFail();
2752+
}
27512753

27522754
if (callback != nullptr) {
27532755
callback->writeErr(bytesWritten, ex);
27542756
}
27552757

2756-
finishFail();
2758+
if (closeOnFailedWrite_) {
2759+
finishFail();
2760+
}
27572761
}
27582762

27592763
void AsyncSocket::failAllWrites(const AsyncSocketException& ex) {

folly/io/async/AsyncSocket.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,13 @@ class AsyncSocket : virtual public AsyncTransportWrapper {
905905
return selfCertData_.get();
906906
}
907907

908+
/**
909+
* Whether socket should be closed on write failure (true by default).
910+
*/
911+
void setCloseOnFailedWrite(bool closeOnFailedWrite) {
912+
closeOnFailedWrite_ = closeOnFailedWrite;
913+
}
914+
908915
/**
909916
* writeReturn is the total number of bytes written, or WRITE_ERROR on error.
910917
* If no data has been written, 0 is returned.
@@ -1349,6 +1356,8 @@ class AsyncSocket : virtual public AsyncTransportWrapper {
13491356
nullptr};
13501357
mutable std::unique_ptr<const AsyncTransportCertificate> selfCertData_{
13511358
nullptr};
1359+
1360+
bool closeOnFailedWrite_{true};
13521361
};
13531362
#ifdef _MSC_VER
13541363
#pragma vtordisp(pop)

0 commit comments

Comments
 (0)