Skip to content

Commit 9eae12f

Browse files
authored
Bugfix: fixed the following bugs (#221)
- fixed the bug that the service may fail to respond to the active packet on the server side under fiber - fixed the bug that data loss in LZ4 compression - fixed the bug that LockFreeQueue queue size may overflow - fixed the bug that gdb_fiber_plugin.py script execution failures when encounter illegal characters
1 parent f1473c6 commit 9eae12f

File tree

11 files changed

+44
-21
lines changed

11 files changed

+44
-21
lines changed

trpc/compressor/lz4/lz4_util.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ bool DoCompress(LZ4F_compressionContext_t& ctx, const NoncontiguousBuffer& in, N
147147
TRPC_FMT_ERROR("CompressedToOutputStream error, compressed_size={}", compressed_size);
148148
return false;
149149
}
150-
left_to_copy = -current_size;
150+
left_to_copy -= current_size;
151151
current_pos += current_size;
152152
}
153153
}

trpc/runtime/iomodel/reactor/fiber/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ cc_test(
150150
"//trpc/runtime:fiber_runtime",
151151
"//trpc/runtime/iomodel/reactor/common:default_io_handler",
152152
"//trpc/util:latch",
153+
"//trpc/util:time",
153154
"@com_google_googletest//:gtest",
154155
"@com_google_googletest//:gtest_main",
155156
],

trpc/runtime/iomodel/reactor/fiber/fiber_tcp_connection.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ FiberTcpConnection::~FiberTcpConnection() {
3636
}
3737

3838
void FiberTcpConnection::Established() {
39-
SetEstablishTimestamp(trpc::time::GetMilliSeconds());
40-
SetConnectionState(ConnectionState::kConnected);
41-
SetConnActiveTime(trpc::time::GetMilliSeconds());
39+
if (IsClient()) {
40+
// The server's connection status are set separately before calling Established
41+
auto now_ms = trpc::time::GetMilliSeconds();
42+
SetEstablishTimestamp(now_ms);
43+
SetConnectionState(ConnectionState::kConnected);
44+
SetConnActiveTime(now_ms);
45+
}
4246

4347
TRPC_ASSERT(GetIoHandler());
4448
TRPC_ASSERT(GetConnectionHandler());

trpc/runtime/iomodel/reactor/fiber/fiber_tcp_connection_test.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "trpc/runtime/iomodel/reactor/fiber/fiber_reactor.h"
2929
#include "trpc/util/latch.h"
3030
#include "trpc/util/net_util.h"
31+
#include "trpc/util/time.h"
3132

3233
namespace trpc {
3334

@@ -105,6 +106,10 @@ class FiberTcpConnectionTestImpl {
105106
auto io_handle = std::make_unique<DefaultIoHandler>(server_conn_.Get());
106107
server_conn_->SetIoHandler(std::move(io_handle));
107108

109+
auto now_ms = trpc::time::GetMilliSeconds();
110+
server_conn_->SetEstablishTimestamp(now_ms);
111+
server_conn_->SetConnectionState(ConnectionState::kConnected);
112+
server_conn_->SetConnActiveTime(now_ms);
108113
server_conn_->Established();
109114
server_conn_->StartHandshaking();
110115

trpc/tools/gdb_plugin/gdb_fiber_plugin.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,13 @@ def _get_memory_maps_in_core_dump():
287287
break # TODO(luobogao): Parse executable sections.
288288

289289
splited = seg.split()
290-
start = int(splited[0], 16)
291-
end = int(splited[2], 16)
290+
try:
291+
start = int(splited[0], 16)
292+
end = int(splited[2], 16)
293+
except ValueError:
294+
# May encounter data like: While running this, GDB does not access memory
295+
#skip them
296+
break
292297
objfile = splited[-1] if not splited[-1].startswith('load') else ''
293298
segs.append(MemorySegment(start, end, objfile))
294299
return segs

trpc/transport/server/fiber/fiber_bind_adapter.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ uint64_t FiberBindAdapter::GenConnectionId() {
304304
return (static_cast<uint64_t>(scheduling_group_index_) << 32) | ++conn_id;
305305
}
306306

307-
void FiberBindAdapter::AddConnection(RefPtr<FiberTcpConnection>&& conn) {
308-
connection_manager_.Add(conn->GetConnId(), std::move(conn));
307+
void FiberBindAdapter::AddConnection(const RefPtr<FiberTcpConnection>& conn) {
308+
connection_manager_.Add(conn->GetConnId(), conn);
309309
}
310310

311311
RefPtr<FiberTcpConnection> FiberBindAdapter::GetConnection(uint64_t conn_id) {
@@ -410,7 +410,7 @@ void FiberBindAdapter::DoClose(const CloseConnectionInfo& close_connection_info)
410410
", conn.ip:" << fiber_conn->GetPeerIp() << ", info.ip:" << close_connection_info.client_ip <<
411411
", conn.port:" << fiber_conn->GetPeerPort() << ", info.port:" << close_connection_info.client_port <<
412412
", conn.fd:" << fiber_conn->GetFd() << ", info.fd:" << close_connection_info.fd);
413-
connection_manager_.Add(fiber_conn->GetConnId(), std::move(fiber_conn));
413+
connection_manager_.Add(fiber_conn->GetConnId(), fiber_conn);
414414
}
415415
}
416416

trpc/transport/server/fiber/fiber_bind_adapter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class FiberBindAdapter : public RefCounted<FiberBindAdapter> {
5050

5151
uint64_t GenConnectionId();
5252

53-
void AddConnection(RefPtr<FiberTcpConnection>&& conn);
53+
void AddConnection(const RefPtr<FiberTcpConnection>& conn);
5454

5555
void UpdateConnection(Connection* conn) {}
5656

trpc/transport/server/fiber/fiber_connection_manager.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ FiberConnectionManager::~FiberConnectionManager() {
3737
}
3838
}
3939

40-
void FiberConnectionManager::Add(uint64_t conn_id, RefPtr<FiberTcpConnection>&& conn) {
40+
void FiberConnectionManager::Add(uint64_t conn_id, const RefPtr<FiberTcpConnection>& conn) {
4141
auto&& shard = conn_shards_[GetHashIndex(conn_id, kShards)];
4242

4343
std::scoped_lock _(shard.lock);
44-
auto&& [it, inserted] = shard.map.emplace(conn_id, std::move(conn));
44+
auto&& [it, inserted] = shard.map.insert(std::make_pair(conn_id, conn));
4545
(void)it; // Suppresses compilation warnings.
4646

4747
TRPC_ASSERT(inserted && "insert FiberConnectionManager with Duplicate conn_id");

trpc/transport/server/fiber/fiber_connection_manager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class FiberConnectionManager {
3030

3131
~FiberConnectionManager();
3232

33-
void Add(uint64_t conn_id, RefPtr<FiberTcpConnection>&& conn);
33+
void Add(uint64_t conn_id, const RefPtr<FiberTcpConnection>& conn);
3434

3535
RefPtr<FiberTcpConnection> Del(uint64_t conn_id);
3636

trpc/transport/server/fiber/fiber_server_transport_impl.cc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "trpc/transport/server/fiber/fiber_server_connection_handler_factory.h"
2828
#include "trpc/util/log/logging.h"
2929
#include "trpc/util/random.h"
30+
#include "trpc/util/time.h"
3031

3132
namespace trpc {
3233

@@ -159,12 +160,18 @@ bool FiberServerTransportImpl::AcceptConnection(AcceptConnectionInfo& connection
159160
conn_handler->Init();
160161

161162
conn->SetConnectionHandler(std::move(conn_handler));
163+
// The connection active time and status must be updated before executing AddConnection to avoid
164+
// the newly created connection being cleared as an idle connection, causing the assertion to fail.
165+
auto now_ms = trpc::time::GetMilliSeconds();
166+
conn->SetEstablishTimestamp(now_ms);
167+
conn->SetConnectionState(ConnectionState::kConnected);
168+
conn->SetConnActiveTime(now_ms);
169+
170+
bind_adapters_[scheduling_group_index]->AddConnection(conn);
162171

163172
conn->Established();
164173
conn->StartHandshaking();
165174

166-
bind_adapters_[scheduling_group_index]->AddConnection(std::move(conn));
167-
168175
FrameStats::GetInstance()->GetServerStats().AddConnCount(1);
169176

170177
return true;

0 commit comments

Comments
 (0)