Fix: bt2: adjust reference counting of value objects
authorSimon Marchi <simon.marchi@efficios.com>
Tue, 9 Jul 2019 21:11:33 +0000 (17:11 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Wed, 10 Jul 2019 21:40:43 +0000 (17:40 -0400)
commitb5947615bfcd6a309e1945a270f67ccc6cd2cf69
tree228d347fd43e96f395589fcdc13f343e8f4821b4
parent056632bfab2b876e4e6149eaa3a6889ca3d28533
Fix: bt2: adjust reference counting of value objects

This patch fixes some reference counting bugs when handling bt_values in
Python.

The first one is when a component class implemented in Python returns
None to a query.  The _UserComponentType._bt_query_from_native method
must return a bt_value address, and it must be a new reference that it
transfers to its caller.  Currently, it returns a pointer to
bt_value_null, without ever acquiring a new reference to it.

The second bug, in the same area, is when the component class returns
some value.  We currently call bt2.object._release, effectively
transferring the Python object's reference to the value to the caller.
However, the user could have kept a reference to that value, wishing to
keep using it.  We should instead return a new reference to this value.

_UserComponentType._bt_query_from_native is therefore modified to
acquire a new reference to return to its caller, regardless of if the
value is bt_value_null or another value.

The third bug is in bt2.value._create_from_ptr.  This function receives
a pointer to a bt_value, and steals it from its caller.  It creates a
Python object that, when destroy, will put the reference.  However, when
the passed pointer is bt_value_null, it just returns None.  The
reference to bt_value_null that has been passed therefore never gets
put.

Bugs 1 and 3 would cancel each other: when doing a query that returned
bt_value_null from Python to a Python component class, we would miss
getting a ref to bt_value_null (bug 1) but we would also miss putting it
(bug 3).

The patch adds a test where Python code queries a Python component
class, which returns None.  While it might not have caught all the bugs
fixed in this patch, it at least exercises the code paths touched in
this patch, which were not tested before, AFAICT.

Change-Id: I0f7bf1004d9d766db4cc0c83475ed37ce2e3f1ed
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1666
CI-Build: Philippe Proulx <eeppeliteloop@gmail.com>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Tested-by: jenkins <jenkins@lttng.org>
src/bindings/python/bt2/bt2/component.py
src/bindings/python/bt2/bt2/object.py
src/bindings/python/bt2/bt2/value.py
tests/bindings/python/bt2/test_component_class.py
This page took 0.02453 seconds and 4 git commands to generate.