Skip to content

Commit 546b4c9

Browse files
author
Karl Herbig
committed
[async] fix race condition if destroy server object while call async receive
1 parent 73b8b27 commit 546b4c9

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

src/server.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
# include <fcntl.h>
2828
# include <unistd.h>
2929
#endif
30+
#include <mutex>
3031
#include "server_p.h"
3132
#include "config.h"
3233

@@ -314,6 +315,7 @@ namespace Modbus {
314315

315316
// ---------------------------------------------------------------------------
316317
void Server::Private::close() {
318+
std::lock_guard<std::mutex> lg (d_guard);
317319

318320
if (backend->net() == Tcp) {
319321

@@ -404,6 +406,7 @@ namespace Modbus {
404406
// ---------------------------------------------------------------------------
405407
// static
406408
int Server::Private::receive (Private * d) {
409+
std::lock_guard<std::mutex> lg (d->d_guard);
407410
int rc;
408411
if ( (d->backend->net() == Tcp) && !d->isConnected()) {
409412

src/server_p.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ namespace Modbus {
5050
std::promise<void> stopDaemon;
5151
Message::Callback messageCB;
5252

53+
std::mutex d_guard;
54+
5355
PIMP_DECLARE_PUBLIC (Server)
5456
};
5557
}

0 commit comments

Comments
 (0)