{
uint64_t i;
typedef void (*method_t)(void *);
-
- struct bt_component_class *comp_class = NULL;
- method_t method = NULL;
+ bool call_user_finalize = true;
BT_ASSERT(iterator);
switch (iterator->state) {
case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED:
- /* Skip user finalization if user initialization failed */
- BT_LIB_LOGD("Not finalizing non-initialized message iterator: "
- "%!+i", iterator);
- goto end;
+ /*
+ * If this function is called while the iterator is in the
+ * NON_INITIALIZED state, it means the user initialization
+ * method has either not been called, or has failed. We
+ * therefore don't want to call the user finalization method.
+ * However, the initialization method might have created some
+ * upstream message iterators before failing, so we want to
+ * execute the rest of this function, which unlinks the related
+ * iterators.
+ */
+ call_user_finalize = false;
+ break;
case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED:
/* Already finalized */
BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
set_self_comp_port_input_msg_iterator_state(iterator,
BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING);
BT_ASSERT(iterator->upstream_component);
- comp_class = iterator->upstream_component->class;
/* Call user-defined destroy method */
- switch (comp_class->type) {
- case BT_COMPONENT_CLASS_TYPE_SOURCE:
- {
- struct bt_component_class_source *src_comp_cls =
- (void *) comp_class;
+ if (call_user_finalize) {
+ method_t method = NULL;
+ struct bt_component_class *comp_class =
+ iterator->upstream_component->class;
- method = (method_t) src_comp_cls->methods.msg_iter_finalize;
- break;
- }
- case BT_COMPONENT_CLASS_TYPE_FILTER:
- {
- struct bt_component_class_filter *flt_comp_cls =
- (void *) comp_class;
+ switch (comp_class->type) {
+ case BT_COMPONENT_CLASS_TYPE_SOURCE:
+ {
+ struct bt_component_class_source *src_comp_cls =
+ (void *) comp_class;
- method = (method_t) flt_comp_cls->methods.msg_iter_finalize;
- break;
- }
- default:
- /* Unreachable */
- bt_common_abort();
- }
+ method = (method_t) src_comp_cls->methods.msg_iter_finalize;
+ break;
+ }
+ case BT_COMPONENT_CLASS_TYPE_FILTER:
+ {
+ struct bt_component_class_filter *flt_comp_cls =
+ (void *) comp_class;
- if (method) {
- const bt_error *saved_error;
+ method = (method_t) flt_comp_cls->methods.msg_iter_finalize;
+ break;
+ }
+ default:
+ /* Unreachable */
+ bt_common_abort();
+ }
- saved_error = bt_current_thread_take_error();
+ if (method) {
+ const bt_error *saved_error;
- BT_LIB_LOGD("Calling user's finalization method: %!+i",
- iterator);
- method(iterator);
+ saved_error = bt_current_thread_take_error();
- if (saved_error) {
- BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error);
+ BT_LIB_LOGD("Calling user's finalization method: %!+i",
+ iterator);
+ method(iterator);
+
+ if (saved_error) {
+ BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error);
+ }
}
}
with self.assertRaises(bt2.TryAgain):
next(it)
+ def test_error_in_iterator_with_cycle_after_having_created_upstream_iterator(self):
+ # Test a failure that triggered an abort in libbabeltrace2, in this situation:
+ #
+ # - The filter iterator creates an upstream iterator.
+ # - The filter iterator creates a reference cycle, including itself.
+ # - An exception is raised, causing the filter iterator's
+ # initialization method to fail.
+ class MySourceIter(bt2._UserMessageIterator):
+ pass
+
+ class MySource(bt2._UserSourceComponent, message_iterator_class=MySourceIter):
+ def __init__(self, config, params, obj):
+ self._add_output_port('out')
+
+ class MyFilterIter(bt2._UserMessageIterator):
+ def __init__(self, config, port):
+ # First, create an upstream iterator.
+ self._upstream_iter = self._create_input_port_message_iterator(
+ self._component._input_ports['in']
+ )
+
+ # Then, voluntarily make a reference cycle that will keep this
+ # Python object alive, which will keep the upstream iterator
+ # Babeltrace object alive.
+ self._self = self
+
+ # Finally, raise an exception to make __init__ fail.
+ raise ValueError('woops')
+
+ class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyFilterIter):
+ def __init__(self, config, params, obj):
+ self._in = self._add_input_port('in')
+ self._out = self._add_output_port('out')
+
+ class MySink(bt2._UserSinkComponent):
+ def __init__(self, config, params, obj):
+ self._input_port = self._add_input_port('in')
+
+ def _user_graph_is_configured(self):
+ self._upstream_iter = self._create_input_port_message_iterator(
+ self._input_port
+ )
+
+ def _user_consume(self):
+ # We should not reach this.
+ assert False
+
+ g = bt2.Graph()
+ src = g.add_component(MySource, 'src')
+ flt = g.add_component(MyFilter, 'flt')
+ snk = g.add_component(MySink, 'snk')
+ g.connect_ports(src.output_ports['out'], flt.input_ports['in'])
+ g.connect_ports(flt.output_ports['out'], snk.input_ports['in'])
+
+ with self.assertRaisesRegex(bt2._Error, 'ValueError: woops'):
+ g.run()
+
def _setup_seek_test(
sink_cls,