-
Notifications
You must be signed in to change notification settings - Fork 796
Fix Groot2Publisher destructor infinite loop #1100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
ZMQ zmq::multipart_t::send throw exception ETERM shutdown pattern💡 Result:
zmq::multipart_t::send()ultimately callszmq_send()on the underlying socket. If your process is shutting down and the context is shut down / terminated while another thread is sending, libzmq makes (almost) all further socket operations fail withETERM, whichcppzmqsurfaces aszmq::error_t. This is expected behavior: context shutdown/termination forces any blocking (and many non-blocking) socket ops to return immediately withETERM. [1], [2]Recommended shutdown pattern (avoid “spurious” exceptions, exit cleanly)
recv()/send()by callingctx.shutdown()(maps tozmq_ctx_shutdown()), which causes blocked operations to return withETERM. [1]zmq::error_tand treatETERMas a normal exit condition.linger = 0first so close/term doesn’t hang waiting for pending sends). [3]ctx.close()/zmq_ctx_term()), after sockets are closed. [2]Minimal C++ example (cppzmq)
std::atomic_bool stopping{false}; void worker(zmq::context_t& ctx, zmq::socket_t& s) { s.set(zmq::sockopt::linger, 0); // optional but common for fast shutdown [3] while (!stopping.load(std::memory_order_relaxed)) { try { zmq::multipart_t mp; // ... fill mp ... mp.send(s); // may throw zmq::error_t(ETERM) during shutdown } catch (const zmq::error_t& e) { if (e.num() == ETERM) break; // context shutdown/term in progress -> exit loop [1][2] throw; } } s.close(); } // elsewhere (shutdown path): stopping = true; ctx.shutdown(); // interrupts blocked ops with ETERM [1] // join worker threads ctx.close(); // terminates context; requires sockets closed; may block per linger [2][3]Key point: if you call
ctx.shutdown()/ctx.close()before (or concurrently with) stopping your senders, you should expectmultipart_t::send()to throwETERM—handle it as a normal shutdown condition rather than an error. [1], [4]Sources:
[1]
zmq_ctx_shutdown(3)behavior andETERMsemantics https://libzmq.readthedocs.io/en/latest/zmq_ctx_shutdown.html[2]
zmq_ctx_term(3)termination steps and blocking/linger considerations https://libzmq.readthedocs.io/en/latest/zmq_ctx_term.html[3]
ZMQ_LINGERoption and effect on close/termination https://api.zeromq.org/4-2:zmq-setsockopt[4] Catching
ETERMduring shutdown (discussion/example) https://stackoverflow.com/questions/8022391/zeromq-socket-recv-throws-context-was-terminated-exception-why-and-how-to🏁 Script executed:
Repository: BehaviorTree/BehaviorTree.CPP
Length of output: 109
🏁 Script executed:
sed -n '260,295p' src/loggers/groot2_publisher.cppRepository: BehaviorTree/BehaviorTree.CPP
Length of output: 965
🏁 Script executed:
sed -n '500,530p' src/loggers/groot2_publisher.cppRepository: BehaviorTree/BehaviorTree.CPP
Length of output: 792
🏁 Script executed:
sed -n '250,270p' src/loggers/groot2_publisher.cppRepository: BehaviorTree/BehaviorTree.CPP
Length of output: 566
🏁 Script executed:
sed -n '240,265p' src/loggers/groot2_publisher.cppRepository: BehaviorTree/BehaviorTree.CPP
Length of output: 588
🏁 Script executed:
sed -n '255,265p' src/loggers/groot2_publisher.cppRepository: BehaviorTree/BehaviorTree.CPP
Length of output: 353
Guard sendErrorReply against ZMQ shutdown exceptions.
The
recv()andreply_msg.send()calls in the main loop (lines 272–283 and 510–518) are wrapped in try/catch forzmq::error_t, but thesendErrorReplylambda (lines 256–262) sends without exception handling. During context shutdown,zmq::multipart_t::send()throwszmq::error_t(withETERM), which will terminate the thread if uncaught. Wrap the error reply send similarly to maintain consistent shutdown behavior:Suggested fix
auto sendErrorReply = [&socket](const std::string& msg) { zmq::multipart_t error_msg; error_msg.addstr("error"); error_msg.addstr(msg); + try + { error_msg.send(socket); + } + catch(const zmq::error_t&) + { + // Ignore errors during shutdown (e.g., ETERM) + } };Also applies to: 510–518
🤖 Prompt for AI Agents