Fix: bt2: fix reference counting of messages returned by Python components
authorSimon Marchi <simon.marchi@efficios.com>
Thu, 27 Jun 2019 19:56:28 +0000 (15:56 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 28 Jun 2019 20:08:01 +0000 (16:08 -0400)
commitd79a835343447b4e7f62af721cfb2a05fa9f450a
tree577ade22c8889b73b0eabaf097b57b2d218a2475
parente74dbb3322844980db73bcd994891c48c68be862
Fix: bt2: fix reference counting of messages returned by Python components

The refcount of messages returned by components implemented in Python
(in _UserMessageIterator._next_from_native) is not right.

When the C iterator method "next" puts bt_message pointers in the output
array, it gives its reference to the bt_messages to the array.  If it
wishes to keep owning a copy of the message, it needs to acquire a new
reference.

For this reason, we currently make the Message Python object release its
reference to the bt_message in _UserMessageIterator.__next__ (otherwise
there would be a double free).  Releasing the reference has the effect
of setting the _ptr property to None, without changing the refcount.

However, it's possible for the user to keep a reference to that Python
object and re-use it.  This is a problem, since the Message Python
object now unexpectedly no longer wraps a valid bt_message.  Trying to
return that message in the following call to __next__, for example, will
fail.

To fix this, this patch makes _next_from_native acquire a new reference
before returning the bt_message.  If the user didn't keep a reference to
the Python object, the Python object will die and put the bt_message.
The message array will therefore be the sole owner of the message.  If
the user did keep a reference to the Python object, then both the Python
object and the message array will now own a reference to the message.

Change-Id: I6f7c472fddeea35509ee669e11d913bec7091f40
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1559
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
src/bindings/python/bt2/bt2/message_iterator.py
tests/bindings/python/bt2/test_message_iterator.py
This page took 0.024772 seconds and 4 git commands to generate.