lib: do not finalize a non-initialized notification iterator
[babeltrace.git] / lib / graph / connection.c
index 17cefd9c2956d5e48e04cc86190e740778d2cb68..f3fbe7c0526997f2eeee5e7c4fcd70dca0d076c6 100644 (file)
@@ -47,7 +47,6 @@ void bt_connection_destroy(struct bt_object *obj)
 {
        struct bt_connection *connection = container_of(obj,
                        struct bt_connection, base);
-       size_t i;
 
        BT_LOGD("Destroying connection: addr=%p", connection);
 
@@ -60,32 +59,18 @@ void bt_connection_destroy(struct bt_object *obj)
         * Because connections are destroyed before components within a
         * graph, this ensures that notification iterators are always
         * finalized before their upstream component.
+        *
+        * Ending the connection does exactly this. We pass `false` to
+        * bt_connection_end() here to avoid removing this connection
+        * from the graph: if we're here, we're already in the graph's
+        * destructor.
         */
-       if (connection->iterators) {
-               for (i = 0; i < connection->iterators->len; i++) {
-                       struct bt_notification_iterator *iterator =
-                               g_ptr_array_index(connection->iterators, i);
-
-                       BT_LOGD("Finalizing notification iterator created by this connection: "
-                               "iter-addr=%p", iterator);
-                       bt_notification_iterator_finalize(iterator);
-
-                       /*
-                        * Make sure this iterator does not try to
-                        * remove itself from this connection's
-                        * iterators on destruction because this
-                        * connection won't exist anymore.
-                        */
-                       bt_notification_iterator_set_connection(iterator,
-                               NULL);
-               }
-
-               g_ptr_array_free(connection->iterators, TRUE);
-       }
+       bt_connection_end(connection, false);
+       g_ptr_array_free(connection->iterators, TRUE);
 
        /*
-        * No bt_put on ports as a connection only holds _weak_ references
-        * to them.
+        * No bt_put on ports as a connection only holds _weak_
+        * references to them.
         */
        g_free(connection);
 }
@@ -196,22 +181,32 @@ end:
 }
 
 BT_HIDDEN
-void bt_connection_disconnect_ports(struct bt_connection *conn)
+void bt_connection_end(struct bt_connection *conn,
+               bool try_remove_from_graph)
 {
        struct bt_component *downstream_comp = NULL;
        struct bt_component *upstream_comp = NULL;
        struct bt_port *downstream_port = conn->downstream_port;
        struct bt_port *upstream_port = conn->upstream_port;
-       struct bt_graph *graph = (void *) bt_object_borrow_parent(conn);
+       struct bt_graph *graph = bt_connection_borrow_graph(conn);
        size_t i;
 
+       BT_LOGD("Ending connection: conn-addr=%p, try-remove-from-graph=%d",
+               conn, try_remove_from_graph);
+
        if (downstream_port) {
+               BT_LOGD("Disconnecting connection's downstream port: "
+                       "port-addr=%p, port-name=\"%s\"",
+                       downstream_port, bt_port_get_name(downstream_port));
                downstream_comp = bt_port_get_component(downstream_port);
                bt_port_set_connection(downstream_port, NULL);
                conn->downstream_port = NULL;
        }
 
        if (upstream_port) {
+               BT_LOGD("Disconnecting connection's upstream port: "
+                       "port-addr=%p, port-name=\"%s\"",
+                       upstream_port, bt_port_get_name(upstream_port));
                upstream_comp = bt_port_get_component(upstream_port);
                bt_port_set_connection(upstream_port, NULL);
                conn->upstream_port = NULL;
@@ -257,7 +252,10 @@ void bt_connection_disconnect_ports(struct bt_connection *conn)
        }
 
        g_ptr_array_set_size(conn->iterators, 0);
-       bt_connection_try_remove_from_graph(conn);
+
+       if (try_remove_from_graph) {
+               bt_connection_try_remove_from_graph(conn);
+       }
 }
 
 struct bt_port *bt_connection_get_upstream_port(
@@ -303,12 +301,23 @@ bt_private_connection_create_notification_iterator(
                goto end;
        }
 
-       if (!notification_types) {
-               BT_LOGD_STR("No notification types: subscribing to all notifications.");
-               notification_types = all_notif_types;
+       connection = bt_connection_from_private(private_connection);
+
+       if (bt_graph_is_canceled(bt_connection_borrow_graph(connection))) {
+               BT_LOGW("Cannot create notification iterator from connection: "
+                       "connection's graph is canceled: "
+                       "conn-addr=%p, upstream-port-addr=%p, "
+                       "upstream-port-name=\"%s\", upstream-comp-addr=%p, "
+                       "upstream-comp-name=\"%s\", graph-addr=%p",
+                       connection, connection->upstream_port,
+                       bt_port_get_name(connection->upstream_port),
+                       upstream_component,
+                       bt_component_get_name(upstream_component),
+                       bt_connection_borrow_graph(connection));
+               status = BT_CONNECTION_STATUS_GRAPH_IS_CANCELED;
+               goto end;
        }
 
-       connection = bt_connection_from_private(private_connection);
        if (bt_connection_is_ended(connection)) {
                BT_LOGW("Invalid parameter: connection is ended: "
                        "conn-addr=%p", connection);
@@ -316,6 +325,11 @@ bt_private_connection_create_notification_iterator(
                goto end;
        }
 
+       if (!notification_types) {
+               BT_LOGD_STR("No notification types: subscribing to all notifications.");
+               notification_types = all_notif_types;
+       }
+
        upstream_port = connection->upstream_port;
        assert(upstream_port);
        upstream_component = bt_port_get_component(upstream_port);
@@ -381,6 +395,7 @@ bt_private_connection_create_notification_iterator(
                }
        }
 
+       iterator->state = BT_NOTIFICATION_ITERATOR_STATE_ACTIVE;
        g_ptr_array_add(connection->iterators, iterator);
        BT_LOGD("Created notification iterator from connection: "
                "conn-addr=%p, upstream-port-addr=%p, "
This page took 0.029084 seconds and 4 git commands to generate.