From ab1cea3f45de3ad0f4d70165c723f321a0eae10c Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 3 Aug 2019 14:29:27 -0400 Subject: [PATCH] bt2: remove BT CC entry from global HT in _UserComponentType.__del__() The global `bt_cc_ptr_to_py_cls` hash table maps BT component class pointers to Python component classes (`PyObject *`) for component classes created in Python. The key and value are weak references. When the Python side calls native_bt.bt2_component_class_*_create() with a Python component class, an entry is added to `bt_cc_ptr_to_py_cls` on success. This works most of the time because all Python classes are normally destroyed when the interpreter is finalized, but there could be issues with specific/unusual import patterns. To avoid potential issues, remove an entry from `bt_cc_ptr_to_py_cls` in _UserComponentType.__del__(), where a strong BT component class reference is finally released. Signed-off-by: Philippe Proulx Change-Id: I450a1c6f179f352e758b9e3e5cac8f4711aa3c88 Reviewed-on: https://review.lttng.org/c/babeltrace/+/1816 Tested-by: jenkins Reviewed-by: Simon Marchi --- src/bindings/python/bt2/bt2/component.py | 1 + .../python/bt2/bt2/native_bt_component_class.i | 1 + .../python/bt2/bt2/native_bt_component_class.i.h | 14 ++++++++++++++ 3 files changed, 16 insertions(+) diff --git a/src/bindings/python/bt2/bt2/component.py b/src/bindings/python/bt2/bt2/component.py index a057019a..31f63725 100644 --- a/src/bindings/python/bt2/bt2/component.py +++ b/src/bindings/python/bt2/bt2/component.py @@ -617,6 +617,7 @@ class _UserComponentType(type): if hasattr(cls, '_bt_cc_ptr'): cc_ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr) native_bt.component_class_put_ref(cc_ptr) + native_bt.bt2_unregister_cc_ptr_to_py_cls(cc_ptr) # Subclasses must provide these methods or property: diff --git a/src/bindings/python/bt2/bt2/native_bt_component_class.i b/src/bindings/python/bt2/bt2/native_bt_component_class.i index 25f50df2..8efe8147 100644 --- a/src/bindings/python/bt2/bt2/native_bt_component_class.i +++ b/src/bindings/python/bt2/bt2/native_bt_component_class.i @@ -47,3 +47,4 @@ struct bt_component_class_filter *bt_bt2_component_class_filter_create( struct bt_component_class_sink *bt_bt2_component_class_sink_create( PyObject *py_cls, const char *name, const char *description, const char *help); +void bt_bt2_unregister_cc_ptr_to_py_cls(const bt_component_class *comp_cls); diff --git a/src/bindings/python/bt2/bt2/native_bt_component_class.i.h b/src/bindings/python/bt2/bt2/native_bt_component_class.i.h index 479f9abf..fc10efef 100644 --- a/src/bindings/python/bt2/bt2/native_bt_component_class.i.h +++ b/src/bindings/python/bt2/bt2/native_bt_component_class.i.h @@ -44,6 +44,19 @@ static GHashTable *bt_cc_ptr_to_py_cls; +static +void bt_bt2_unregister_cc_ptr_to_py_cls(const bt_component_class *comp_cls) +{ + gboolean existed; + + if (!bt_cc_ptr_to_py_cls) { + return; + } + + existed = g_hash_table_remove(bt_cc_ptr_to_py_cls, comp_cls); + BT_ASSERT(existed); +} + static void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc, PyObject *py_cls) @@ -85,6 +98,7 @@ void native_comp_class_dtor(void) { if (bt_cc_ptr_to_py_cls) { BT_LOGD_STR("Destroying native component class to Python component class hash table."); g_hash_table_destroy(bt_cc_ptr_to_py_cls); + bt_cc_ptr_to_py_cls = NULL; } } -- 2.34.1