Skip to content

Commit d7bf7cd

Browse files
RovicnowYoyipopoaichuiniu
authored andcommitted
[BugFix]Add try catch to deal with worker->join() crash
1. add try-catch for thread->join() 2. unify kInvalidSocket.
1 parent 0e915c2 commit d7bf7cd

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

debug_router/native/base/socket_guard.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,37 @@
99
#include <winsock2.h>
1010
#include <ws2tcpip.h>
1111
#define CLOSESOCKET closesocket
12+
typedef SOCKET SocketType;
13+
constexpr SocketType kInvalidSocket = INVALID_SOCKET;
1214
#else
1315
#include <netdb.h>
1416
#include <sys/socket.h>
1517
#include <sys/types.h>
1618
#include <unistd.h>
17-
#define SOCKET int
1819
#define CLOSESOCKET close
20+
typedef int SocketType;
21+
constexpr SocketType kInvalidSocket = -1;
1922
#endif
2023

21-
constexpr SOCKET kInvalidSocket = 0;
22-
2324
namespace debugrouter {
2425
namespace base {
2526

2627
class SocketGuard {
2728
public:
28-
SOCKET Get() const { return sock_; }
29+
SocketType Get() const { return sock_; }
2930

30-
explicit SocketGuard(SOCKET sock) : sock_(sock) {}
31+
explicit SocketGuard(SocketType sock) : sock_(sock) {}
3132

3233
~SocketGuard() {
33-
if (sock_ != 0) {
34+
if (sock_ != kInvalidSocket) {
3435
CLOSESOCKET(sock_);
3536
}
3637
}
3738
SocketGuard(const SocketGuard&) = delete;
3839
SocketGuard& operator=(const SocketGuard&) = delete;
3940

4041
private:
41-
SOCKET sock_;
42+
SocketType sock_;
4243
};
4344

4445
} // namespace base

debug_router/native/net/websocket_task.cc

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,18 @@ void WebSocketTask::SendInternal(const std::string &data) {
125125
*reinterpret_cast<uint32_t *>(prefix + prefix_len) = 0;
126126
prefix_len += 4;
127127

128-
send(socket_guard_->Get(), (char *)prefix, prefix_len, 0);
129-
send(socket_guard_->Get(), buf, payloadLen, 0);
128+
if (!socket_guard_) {
129+
onFailure();
130+
return;
131+
}
132+
if (send(socket_guard_->Get(), (char *)prefix, prefix_len, 0) == -1) {
133+
onFailure();
134+
return;
135+
}
136+
if (send(socket_guard_->Get(), buf, payloadLen, 0) == -1) {
137+
onFailure();
138+
return;
139+
}
130140
}
131141

132142
void WebSocketTask::start() {
@@ -230,6 +240,11 @@ bool WebSocketTask::do_read(std::string &msg) {
230240
uint8_t mask_payload_len;
231241
} head;
232242

243+
if (!socket_guard_) {
244+
onFailure();
245+
return false;
246+
}
247+
233248
if (recv(socket_guard_->Get(), (char *)&head, sizeof(head), 0) !=
234249
sizeof(head)) {
235250
LOGE("failed to read websocket message");

debug_router/native/socket/work_thread_executor.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "debug_router/native/socket/work_thread_executor.h"
66

7+
#include "debug_router/native/log/logging.h"
8+
79
namespace debugrouter {
810
namespace base {
911

@@ -38,7 +40,12 @@ void WorkThreadExecutor::shutdown() {
3840
cond.notify_all();
3941

4042
if (worker && worker->joinable()) {
41-
worker->join();
43+
try {
44+
worker->join();
45+
} catch (const std::exception& e) {
46+
LOGE("WorkThreadExecutor::shutdown worker->detach() failed, "
47+
<< e.what());
48+
}
4249
}
4350
worker.reset();
4451
}

0 commit comments

Comments
 (0)