mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2026-11-25 13:11:36 +00:00
fix(zeromq,shmem): stop socket monitor before closing sockets
- Stop the zmq monitor and drain pending events before closing the monitored socket and the reading monitor socket - Events emitted while the monitored socket closes were dropped by libzmq, leaking their endpoint frames (zmq_msg_init_size) - Apply to both the zeromq and shmem transport sockets - LeakSanitizer no longer reports leaks in the test suite Fixes: #463 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -320,17 +320,14 @@ class Socket final : public fair::mq::Socket
|
||||
{
|
||||
// LOG(debug) << "Closing socket " << fId;
|
||||
|
||||
zmq::closeMonitorSocket(fSocket, fMonitorSocket);
|
||||
fMonitorSocket = nullptr;
|
||||
|
||||
if (fSocket && zmq_close(fSocket) != 0) {
|
||||
LOG(error) << "Failed closing data socket " << fId
|
||||
<< ", reason: " << zmq_strerror(errno);
|
||||
}
|
||||
fSocket = nullptr;
|
||||
|
||||
if (fMonitorSocket && zmq_close(fMonitorSocket) != 0) {
|
||||
LOG(error) << "Failed closing monitor socket " << fId
|
||||
<< ", reason: " << zmq_strerror(errno);
|
||||
}
|
||||
fMonitorSocket = nullptr;
|
||||
}
|
||||
|
||||
void SetOption(const std::string& option, const void* value, size_t valueSize) override
|
||||
|
||||
@@ -170,6 +170,31 @@ inline auto makeMonitorSocket(void* zmqCtx, void* socketToMonitor, std::string_v
|
||||
return mon;
|
||||
}
|
||||
|
||||
/// Stop monitoring, discard all pending events and close the reading monitor socket
|
||||
///
|
||||
/// Stopping the monitor first closes the writing end synchronously. Otherwise, events emitted
|
||||
/// while the monitored socket is closing can be dropped by libzmq after the reading end is gone,
|
||||
/// which leaks their (heap allocated) endpoint frames.
|
||||
inline auto closeMonitorSocket(void* socketToMonitor, void* monitorSocket) -> void
|
||||
{
|
||||
if (monitorSocket == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (socketToMonitor != nullptr) {
|
||||
[[maybe_unused]] auto const rc = zmq_socket_monitor(socketToMonitor, nullptr, 0);
|
||||
}
|
||||
|
||||
zmq_msg_t msg;
|
||||
zmq_msg_init(&msg);
|
||||
while (zmq_msg_recv(&msg, monitorSocket, ZMQ_DONTWAIT) != -1) {}
|
||||
zmq_msg_close(&msg);
|
||||
|
||||
if (zmq_close(monitorSocket) != 0) {
|
||||
LOG(error) << "Failed closing monitor socket, reason: " << zmq_strerror(errno);
|
||||
}
|
||||
}
|
||||
|
||||
/// Read pending zmq monitor event in a non-blocking fashion.
|
||||
/// @return event id or -1 for no event pending
|
||||
inline auto getMonitorEvent(void* monitorSocket) -> int
|
||||
|
||||
@@ -260,17 +260,14 @@ class Socket final : public fair::mq::Socket
|
||||
{
|
||||
// LOG(debug) << "Closing socket " << fId;
|
||||
|
||||
closeMonitorSocket(fSocket, fMonitorSocket);
|
||||
fMonitorSocket = nullptr;
|
||||
|
||||
if (fSocket && zmq_close(fSocket) != 0) {
|
||||
LOG(error) << "Failed closing data socket " << fId
|
||||
<< ", reason: " << zmq_strerror(errno);
|
||||
}
|
||||
fSocket = nullptr;
|
||||
|
||||
if (fMonitorSocket && zmq_close(fMonitorSocket) != 0) {
|
||||
LOG(error) << "Failed closing monitor socket " << fId
|
||||
<< ", reason: " << zmq_strerror(errno);
|
||||
}
|
||||
fMonitorSocket = nullptr;
|
||||
}
|
||||
|
||||
void SetOption(const std::string& option, const void* value, size_t valueSize) override
|
||||
|
||||
Reference in New Issue
Block a user