Skip to content

Commit 4290c15

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: lynx-family#42
1 parent cd4fedd commit 4290c15

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ void SocketServer::Close() {
127127
void SocketServer::Disconnect() {
128128
thread::DebugRouterExecutor::GetInstance().Post([=]() {
129129
if (!usb_client_) {
130+
usb_client_->Stop();
130131
usb_client_ = nullptr;
131132
}
132133
});
@@ -136,6 +137,9 @@ SocketServer::~SocketServer() {
136137
if (!usb_client_) {
137138
usb_client_->Stop();
138139
}
140+
if (!temp_usb_client_) {
141+
temp_usb_client_->Stop();
142+
}
139143
Close();
140144
}
141145

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)