Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: potential missing on_closed message on client-side #2525

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/brpc/policy/baidu_rpc_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "butil/iobuf.h" // butil::IOBuf
#include "butil/raw_pack.h" // RawPacker RawUnpacker
#include "brpc/controller.h" // Controller
#include "brpc/errno.pb.h"
#include "brpc/socket.h" // Socket
#include "brpc/server.h" // Server
#include "brpc/span.h"
Expand Down Expand Up @@ -216,7 +217,15 @@ void SendRpcResponse(int64_t correlation_id,
if (Socket::Address(response_stream_id, &stream_ptr) == 0) {
Stream* s = (Stream*)stream_ptr->conn();
s->FillSettings(meta.mutable_stream_settings());
s->SetHostSocket(sock);
// If failed to set host socket here,
// s->SetConnected will fail at CHECK(_host_socket != NULL)
if (s->SetHostSocket(sock) != 0) {
LOG(WARNING) << "Failed to set host socket " << *sock;
cntl->SetFailed(EFAILEDSOCKET, "Fail to set host socket %s",
sock->description().c_str());
((Stream *)stream_ptr->conn())->Close();
return;
}
} else {
LOG(WARNING) << "Stream=" << response_stream_id
<< " was closed before sending response";
Expand Down Expand Up @@ -247,6 +256,14 @@ void SendRpcResponse(int64_t correlation_id,
// Send rpc response over stream even if server side failed to create
// stream for some reason.
if(cntl->has_remote_stream()){
// If we don't set connected here before send the response,
// client-side may close the stream before server-side set connected.
// This will cause missing on_closed message on the client-side.
if (stream_ptr) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

能不能加一个注释说明下为什么SetConnected一定要在发消息之前设置吗?
如果这里有race的话,后面需要加一个barrier不?

Copy link
Author

@kaijchen kaijchen Feb 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加了,我们在单机测试下,出现了客户端收不到 on_closed 的问题:

  1. 服务端 send response 先发了包,但还未设置 connected。
  2. 客户端收到 open 成功,处理完后续逻辑,执行 stream close。
  3. 服务端收到 on_closed 时候,set connected 还未执行,导致 close frame 没发出去。

img_v3_027h_ef451b0b-127e-421e-aa49-7a069148d34g

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码和注释更新了,请再看一下。

// Now it's ok the mark this server-side stream as connectted as all the
// written user data would follower the RPC response.
((Stream*)stream_ptr->conn())->SetConnected();
}
// Send the response over stream to notify that this stream connection
// is successfully built.
// Response_stream can be INVALID_STREAM_ID when error occurs.
Expand All @@ -262,12 +279,6 @@ void SendRpcResponse(int64_t correlation_id,
}
return;
}

if(stream_ptr) {
// Now it's ok the mark this server-side stream as connected as all the
// written user data would follower the RPC response.
((Stream*)stream_ptr->conn())->SetConnected();
}
} else{
// Have the risk of unlimited pending responses, in which case, tell
// users to set max_concurrency.
Expand Down
Loading