Commit 1329c39b authored by Andrii Grynenko's avatar Andrii Grynenko Committed by Facebook Github Bot

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
parent f3957689
......@@ -2747,13 +2747,17 @@ void AsyncSocket::failWrite(
VLOG(4) << "AsyncSocket(this=" << this << ", fd=" << fd_
<< ", state=" << state_ << " host=" << addr_.describe()
<< "): failed while writing in " << fn << "(): " << ex.what();
if (closeOnFailedWrite_) {
startFail();
}
if (callback != nullptr) {
callback->writeErr(bytesWritten, ex);
}
if (closeOnFailedWrite_) {
finishFail();
}
}
void AsyncSocket::failAllWrites(const AsyncSocketException& ex) {
......
......@@ -905,6 +905,13 @@ class AsyncSocket : virtual public AsyncTransportWrapper {
return selfCertData_.get();
}
/**
* Whether socket should be closed on write failure (true by default).
*/
void setCloseOnFailedWrite(bool closeOnFailedWrite) {
closeOnFailedWrite_ = closeOnFailedWrite;
}
/**
* writeReturn is the total number of bytes written, or WRITE_ERROR on error.
* If no data has been written, 0 is returned.
......@@ -1349,6 +1356,8 @@ class AsyncSocket : virtual public AsyncTransportWrapper {
nullptr};
mutable std::unique_ptr<const AsyncTransportCertificate> selfCertData_{
nullptr};
bool closeOnFailedWrite_{true};
};
#ifdef _MSC_VER
#pragma vtordisp(pop)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment