Skip to content

Commit 1202824

Browse files
committed
[BugFix][android][ios][common] Fix crash when joining worker thread during WorkThreadExecutor shutdown
A UsbClient must call stop() before destruction to prevent threads from accessing its resources after it's destroyed. issue: #42
1 parent cd4fedd commit 1202824

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

debug_router/native/socket/posix/socket_server_posix.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,14 @@ void SocketServerPosix::Start() {
9494
NotifyInit(GetErrorMessage(), "accept socket error");
9595
return;
9696
}
97-
auto temp_usb_client = std::make_shared<UsbClient>(accept_socket_fd);
97+
if (temp_usb_client_) {
98+
temp_usb_client_->Stop();
99+
}
100+
temp_usb_client_ = std::make_shared<UsbClient>(accept_socket_fd);
98101
std::shared_ptr<ClientListener> listener =
99102
std::make_shared<ClientListener>(shared_from_this());
100-
temp_usb_client->Init();
101-
temp_usb_client->StartUp(listener);
103+
temp_usb_client_->Init();
104+
temp_usb_client_->StartUp(listener);
102105
}
103106

104107
void SocketServerPosix::CloseSocket(int socket_fd) {

debug_router/native/socket/socket_server_api.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ void SocketServer::HandleOnCloseStatus(std::shared_ptr<UsbClient> client,
6969
thread::DebugRouterExecutor::GetInstance().Post([=]() {
7070
if (!usb_client_ || usb_client_ != client) {
7171
LOGI("SocketServerApi OnMessage: client is null or not match.");
72+
client->Stop();
7273
return;
7374
}
7475
usb_client_->Stop();
@@ -85,6 +86,7 @@ void SocketServer::HandleOnErrorStatus(std::shared_ptr<UsbClient> client,
8586
thread::DebugRouterExecutor::GetInstance().Post([=]() {
8687
if (!usb_client_ || usb_client_ != client) {
8788
LOGI("SocketServerApi OnMessage: client is null or not match.");
89+
client->Stop();
8890
return;
8991
}
9092
usb_client_->Stop();
@@ -127,6 +129,7 @@ void SocketServer::Close() {
127129
void SocketServer::Disconnect() {
128130
thread::DebugRouterExecutor::GetInstance().Post([=]() {
129131
if (!usb_client_) {
132+
usb_client_->Stop();
130133
usb_client_ = nullptr;
131134
}
132135
});
@@ -136,6 +139,9 @@ SocketServer::~SocketServer() {
136139
if (!usb_client_) {
137140
usb_client_->Stop();
138141
}
142+
if (!temp_usb_client_) {
143+
temp_usb_client_->Stop();
144+
}
139145
Close();
140146
}
141147

debug_router/native/socket/socket_server_api.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class SocketServer : public std::enable_shared_from_this<SocketServer> {
6565
std::unique_ptr<CountDownLatch> latch_;
6666
std::mutex queue_lock_;
6767
std::shared_ptr<UsbClient> usb_client_;
68+
std::shared_ptr<UsbClient> temp_usb_client_;
6869

6970
volatile SocketType socket_fd_ = kInvalidSocket;
7071
};

0 commit comments

Comments
 (0)