Skip to content

Tidy: Session refactors - #100

Merged
pniedzielski merged 4 commits into
bloomberg:mainfrom
pniedzielski:authn/tidy
Sep 18, 2026
Merged

pniedzielski merged 4 commits into
bloomberg:mainfrom
pniedzielski:authn/tidy

Conversation

@pniedzielski

Copy link
Copy Markdown
Contributor

This pull request contains four Tidy-First refactors that make working with Sessions at the Python, Cython, and C++ layers simpler.

@pniedzielski
pniedzielski requested a review from a team as a code owner September 4, 2026 16:31
@pniedzielski pniedzielski added the skip news No news entry is required label Sep 4, 2026
`SessionEventHandler` hand-rolls reference counting for its three
Python callbacks: three `Py_INCREF`s under a `GilAcquireGuard` in the
constructor, and three matching `Py_DECREF`s in the destructor.  As we
intend to add an additional callback to provide authentication
credentials, we will have to repeat this same structure wherever that
is held and make sure to get the GIL right.

This patch adds a new C++ class `pybmq::PyRef`, an owning reference
type that acquires the GIL whenever it adjusts a reference count.
Because copying and destroying it are GIL-safe, it can be held by
objects the SDK copies on its own IO threads.  By doing this, we no
longer need to manually maintain the reference count with `Py_INCREF`
and `Py_DECREF`s under the GIL.

This patch also ports the callbacks held by `SessionEventHandler` to
use it.  These changes do result in a few more locks and unlocks of
the GIL, but this only happens once while constructing and once while
destructing a `Session`, so the downside is small.

Signed-off-by: Patrick M. Niedzielski <pniedzielski@bloomberg.net>
This patch collapses two branches in `with_options`, which differed
only in whether `DEFAULT_TIMEOUT` or `session_options.timeouts` was
passed to a function call, and duplicated the other eleven.

Signed-off-by: Patrick M. Niedzielski <pniedzielski@bloomberg.net>
`with_options` passed eleven arguments positionally, so inserting a
parameter anywhere in `Session.__init__` would silently rebind every
argument after it rather than failing.  This patch changes the call to
pass all optional arguments by keyword.

Signed-off-by: Patrick M. Niedzielski <pniedzielski@bloomberg.net>
The constructor to `pybmq::Session` has been getting a little
unwieldy, growing to 21 parameters.  Originally, passing parameters
directly to `pybmq::Session` like this resulted in the simplest and
most transparent code in the Cython layer (which is the layer that is
hardest to debug) and meant we didn’t need to worry too much about the
reference counting of Python objects being marshalled through Cython.
But, with so many parameters now, this is more of a liability than a
benefit.

This patch adds a new struct `pybmq::SessionConfig`, which holds the
fourteen plain option values.  The Python callbacks, host health
monitor and exception types stay as constructor parameters, so no
Python object lives in the struct for reference counting ease.  Adding
this struct allows us to simplify the constructor for `pybmq::Session`
down from taking 21 arguments to taking only 8.

Signed-off-by: Patrick M. Niedzielski <pniedzielski@bloomberg.net>
@pniedzielski
pniedzielski merged commit 770d776 into bloomberg:main Sep 18, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip news No news entry is required

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants