Skip to content

Commit c898bd7

Browse files
RovicnowYoyipopoaichuiniu
authored andcommitted
[BugFix]Fix all the crash that caused by thread processing
1. fix do_read crash caused by multithreaded operation of the socket_guard_. 2. fix crash that run() thread can not be released by the destructor when work_thread_executor destructor. 3. add worker->get_id() to determine whether the current waiting thread is this thread, if so, detach.
1 parent d7bf7cd commit c898bd7

File tree

7 files changed

+21
-3
lines changed

7 files changed

+21
-3
lines changed

debug_router/native/net/websocket_task.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,14 @@ std::string decodeURIComponent(std::string url) {
9191
WebSocketTask::WebSocketTask(
9292
std::shared_ptr<core::MessageTransceiver> transceiver,
9393
const std::string &url)
94-
: transceiver_(transceiver), url_(url) {
94+
: transceiver_(transceiver),
95+
url_(url),
96+
socket_guard_(std::make_unique<base::SocketGuard>(kInvalidSocket)) {
9597
submit([this]() { start(); });
9698
}
9799

100+
WebSocketTask::~WebSocketTask() { shutdown(); }
101+
98102
void WebSocketTask::SendInternal(const std::string &data) {
99103
const char *buf = data.data();
100104
size_t payloadLen = data.size();

debug_router/native/net/websocket_task.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class WebSocketTask : public base::WorkThreadExecutor {
1616
public:
1717
WebSocketTask(std::shared_ptr<core::MessageTransceiver> transceiver,
1818
const std::string &url);
19+
virtual ~WebSocketTask() override;
1920

2021
void SendInternal(const std::string &data);
2122

debug_router/native/socket/socket_server_api.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ void SocketServer::HandleOnOpenStatus(std::shared_ptr<UsbClient> client,
4040
thread::DebugRouterExecutor::GetInstance().Post([=]() {
4141
std::shared_ptr<UsbClient> old_client_ = usb_client_;
4242
LOGI("SocketServerApi OnOpen: replace old client.");
43+
if (old_client_) {
44+
old_client_->Stop();
45+
}
4346
usb_client_ = client;
4447
if (auto listener = listener_.lock()) {
4548
listener->OnStatusChanged(kConnected, code, reason);
@@ -68,6 +71,7 @@ void SocketServer::HandleOnCloseStatus(std::shared_ptr<UsbClient> client,
6871
LOGI("SocketServerApi OnMessage: client is null or not match.");
6972
return;
7073
}
74+
usb_client_->Stop();
7175
usb_client_ = nullptr;
7276
if (auto listener = listener_.lock()) {
7377
listener->OnStatusChanged(status, code, reason);
@@ -83,6 +87,7 @@ void SocketServer::HandleOnErrorStatus(std::shared_ptr<UsbClient> client,
8387
LOGI("SocketServerApi OnMessage: client is null or not match.");
8488
return;
8589
}
90+
usb_client_->Stop();
8691
usb_client_ = nullptr;
8792
if (auto listener = listener_.lock()) {
8893
listener->OnStatusChanged(status, code, reason);

debug_router/native/socket/usb_client.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ bool UsbClient::Send(const std::string &message) {
367367
return true;
368368
}
369369

370+
void UsbClient::Stop() { work_thread_.shutdown(); }
371+
370372
void UsbClient::SendInternal(const std::string &message) {
371373
LOGI("UsbClient: SendInternal.");
372374
if (connect_status_ != USBConnectStatus::CONNECTED) {

debug_router/native/socket/usb_client.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class UsbClient : public std::enable_shared_from_this<UsbClient> {
2828
// true means the message are added to message queue
2929
bool Send(const std::string &message);
3030

31+
void Stop();
32+
3133
explicit UsbClient(SocketType socket_fd);
3234
~UsbClient();
3335

debug_router/native/socket/work_thread_executor.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ void WorkThreadExecutor::shutdown() {
4141

4242
if (worker && worker->joinable()) {
4343
try {
44-
worker->join();
44+
if (worker->get_id() != std::this_thread::get_id()) {
45+
worker->join();
46+
} else {
47+
worker->detach();
48+
}
4549
} catch (const std::exception& e) {
4650
LOGE("WorkThreadExecutor::shutdown worker->detach() failed, "
4751
<< e.what());

debug_router/native/socket/work_thread_executor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace base {
1717
class WorkThreadExecutor {
1818
public:
1919
WorkThreadExecutor();
20-
~WorkThreadExecutor();
20+
virtual ~WorkThreadExecutor();
2121

2222
void submit(std::function<void()> task);
2323
void shutdown();

0 commit comments

Comments
 (0)