Repository navigation
fix: Hold pybmq::Session by shared_ptr in _ext.Session - #112
Merged
Merged
Conversation
`Session.stop()` stops the underlying `pybmq::Session` but does not destroy it. Destruction is left to `_ext.Session.__dealloc__`, which runs whenever the garbage collector gets to it, on whichever thread triggers the collection. Destroying a `pybmq::Session` blocks until the libbmq FSM thread finishes stopping it, and the FSM thread logs through the Python `logging` module before it can finish. If the collection runs on a thread that holds a `logging.Handler` lock, the destructor waits for the FSM thread and the FSM thread waits for the lock, and the process deadlocks. Any reference cycle hands a stopped `Session` to the collector; a traceback retained after an exception is a common one. This patch holds the `pybmq::Session` by `shared_ptr` and releases it in `stop()`, so a `Session` used as a context manager is destroyed when the `with` block exits. Each method takes its own copy of the pointer before calling into C++, so a concurrent `stop()` cannot destroy the session while it is in use. Calling a method after `stop()` now raises `Error`. Signed-off-by: Chris A. Evans <thecityofguanyu@outlook.com>
pniedzielski
approved these changes
Sep 28, 2026
pniedzielski
left a comment
Contributor
There was a problem hiding this comment.
Looks good, thanks for this!
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Session.stop()stops the underlyingpybmq::Sessionbut does not destroy it. Destruction is left to_ext.Session.__dealloc__, which runs whenever the garbage collector gets to it, on whichever thread triggers the collection.Destroying a
pybmq::Sessionblocks until the libbmq FSM thread finishes stopping it, and the FSM thread logs through the Pythonloggingmodule before it can finish.If the collection runs on a thread that holds a
logging.Handlerlock, the destructor waits for the FSM thread and the FSM thread waits for the lock, and the process deadlocks. Any reference cycle hands a stoppedSessionto the collector. A traceback retained after an exception is a common one.stop().Proposed Changes
Hold the
pybmq::Sessionviashared_ptrand release it instop(), so aSessionused as a context manager is destroyed when thewithblock exits.Each method takes its own copy of the pointer before calling into C++, so a concurrent
stop()cannot destroy the session while it is in use. A method called afterstop()still raises the sameErroras before.Unit tests have been added to validate this behavior.