Skip to content

Commit 891adf5

Browse files
author
Leonardo Parente
authored
Proper lock the callbacks due to diffrent threads calls (#580)
1 parent 8f8e30a commit 891adf5

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

src/inputs/netprobe/PingProbe.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,10 @@ bool PingProbe::start(std::shared_ptr<uvw::Loop> io_loop)
171171
}
172172
});
173173

174-
_recv_connection = PingReceiver::recv_signal.connect([this](pcpp::Packet &packet, timespec stamp) { _recv(packet, TestType::Ping, _name, stamp); });
174+
_recv_connection = PingReceiver::recv_signal.connect([this](pcpp::Packet &packet, timespec stamp) {
175+
std::unique_lock<std::mutex> lock(_mutex);
176+
_recv(packet, TestType::Ping, _name, stamp);
177+
});
175178

176179
(_sequence == UCHAR_MAX) ? _sequence = 0 : _sequence++;
177180
_send_icmp_v4(_internal_sequence);
@@ -296,6 +299,7 @@ void PingProbe::_send_icmp_v4(uint8_t sequence)
296299
if (rc != SOCKET_ERROR) {
297300
pcpp::Packet packet;
298301
packet.addLayer(&icmp);
302+
std::unique_lock<std::mutex> lock(_mutex);
299303
_send(packet, TestType::Ping, _name, stamp);
300304
}
301305
}

src/inputs/netprobe/PingProbe.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class PingProbe final : public NetProbe
9090
sockaddr_in _sa;
9191
sockaddr_in6 _sa6;
9292
sigslot::connection _recv_connection;
93+
std::mutex _mutex;
9394

9495
void _send_icmp_v4(uint8_t sequence);
9596
std::optional<ErrorType> _get_addr();

0 commit comments

Comments
 (0)