Skip to content

Commit

Permalink
fix: invalid graph (#249)
Browse files Browse the repository at this point in the history
Co-authored-by: Hu Yueh-Wei <[email protected]>
  • Loading branch information
leoadonia and halajohn authored Nov 11, 2024
1 parent 20e8275 commit 0188c96
Show file tree
Hide file tree
Showing 32 changed files with 449 additions and 212 deletions.
118 changes: 61 additions & 57 deletions core/include/ten_runtime/binding/cpp/internal/addon.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,63 +219,67 @@ class extension_addon_t : public addon_t {

} // namespace ten

#define TEN_CPP_REGISTER_ADDON_AS_EXTENSION_GROUP(NAME, CLASS) \
class NAME##_default_extension_group_addon_t \
: public ten::extension_group_addon_t { \
public: \
void on_create_instance(ten::ten_env_t &ten_env, const char *name, \
void *context) override { \
auto *instance = new CLASS(name); \
ten_env.on_create_instance_done(instance, context); \
} \
void on_destroy_instance(ten::ten_env_t &ten_env, void *instance, \
void *context) override { \
delete static_cast<CLASS *>(instance); \
ten_env.on_destroy_instance_done(context); \
} \
}; \
static ten::addon_t *g_##NAME##_default_extension_group_addon = nullptr; \
TEN_CONSTRUCTOR(____ctor_ten_declare_##NAME##_extension_group_addon____) { \
g_##NAME##_default_extension_group_addon = \
new NAME##_default_extension_group_addon_t(); \
ten_string_t *base_dir = ten_path_get_module_path( \
(void *)____ctor_ten_declare_##NAME##_extension_group_addon____); \
ten_addon_register_extension_group( \
#NAME, ten_string_get_raw_str(base_dir), \
g_##NAME##_default_extension_group_addon->get_c_addon()); \
ten_string_destroy(base_dir); \
} \
TEN_DESTRUCTOR(____dtor_ten_declare_##NAME##_##TYPE##_addon____) { \
ten_addon_unregister_extension_group(#NAME); \
delete g_##NAME##_default_extension_group_addon; \
#define TEN_CPP_REGISTER_ADDON_AS_EXTENSION_GROUP(NAME, CLASS) \
class NAME##_default_extension_group_addon_t \
: public ten::extension_group_addon_t { \
public: \
void on_create_instance(ten::ten_env_t &ten_env, const char *name, \
void *context) override { \
auto *instance = new CLASS(name); \
ten_env.on_create_instance_done(instance, context); \
} \
void on_destroy_instance(ten::ten_env_t &ten_env, void *instance, \
void *context) override { \
delete static_cast<CLASS *>(instance); \
ten_env.on_destroy_instance_done(context); \
} \
}; \
static ten::addon_t *g_##NAME##_default_extension_group_addon = nullptr; \
TEN_CONSTRUCTOR(____ctor_ten_declare_##NAME##_extension_group_addon____) { \
g_##NAME##_default_extension_group_addon = \
new NAME##_default_extension_group_addon_t(); \
ten_string_t *base_dir = \
ten_path_get_module_path(/* NOLINTNEXTLINE */ \
(void *) \
____ctor_ten_declare_##NAME##_extension_group_addon____); \
ten_addon_register_extension_group( \
#NAME, ten_string_get_raw_str(base_dir), \
g_##NAME##_default_extension_group_addon->get_c_addon()); \
ten_string_destroy(base_dir); \
} \
TEN_DESTRUCTOR(____dtor_ten_declare_##NAME##_##TYPE##_addon____) { \
ten_addon_unregister_extension_group(#NAME); \
delete g_##NAME##_default_extension_group_addon; \
}

#define TEN_CPP_REGISTER_ADDON_AS_EXTENSION(NAME, CLASS) \
class NAME##_default_extension_addon_t : public ten::extension_addon_t { \
public: \
void on_create_instance(ten::ten_env_t &ten_env, const char *name, \
void *context) override { \
auto *instance = new CLASS(name); \
ten_env.on_create_instance_done(instance, context); \
} \
void on_destroy_instance(ten::ten_env_t &ten_env, void *instance, \
void *context) override { \
delete static_cast<CLASS *>(instance); \
ten_env.on_destroy_instance_done(context); \
} \
}; \
static ten::addon_t *g_##NAME##_default_extension_addon = nullptr; \
TEN_CONSTRUCTOR(____ctor_ten_declare_##NAME##_extension_addon____) { \
g_##NAME##_default_extension_addon = \
new NAME##_default_extension_addon_t(); \
ten_string_t *base_dir = ten_path_get_module_path( \
(void *)____ctor_ten_declare_##NAME##_extension_addon____); \
ten_addon_register_extension( \
#NAME, ten_string_get_raw_str(base_dir), \
g_##NAME##_default_extension_addon->get_c_addon()); \
ten_string_destroy(base_dir); \
} \
TEN_DESTRUCTOR(____dtor_ten_declare_##NAME##_##TYPE##_addon____) { \
ten_addon_unregister_extension(#NAME); \
delete g_##NAME##_default_extension_addon; \
#define TEN_CPP_REGISTER_ADDON_AS_EXTENSION(NAME, CLASS) \
class NAME##_default_extension_addon_t : public ten::extension_addon_t { \
public: \
void on_create_instance(ten::ten_env_t &ten_env, const char *name, \
void *context) override { \
auto *instance = new CLASS(name); \
ten_env.on_create_instance_done(instance, context); \
} \
void on_destroy_instance(ten::ten_env_t &ten_env, void *instance, \
void *context) override { \
delete static_cast<CLASS *>(instance); \
ten_env.on_destroy_instance_done(context); \
} \
}; \
static ten::addon_t *g_##NAME##_default_extension_addon = nullptr; \
TEN_CONSTRUCTOR(____ctor_ten_declare_##NAME##_extension_addon____) { \
g_##NAME##_default_extension_addon = \
new NAME##_default_extension_addon_t(); \
ten_string_t *base_dir = \
ten_path_get_module_path(/* NOLINTNEXTLINE */ \
(void *) \
____ctor_ten_declare_##NAME##_extension_addon____); \
ten_addon_register_extension( \
#NAME, ten_string_get_raw_str(base_dir), \
g_##NAME##_default_extension_addon->get_c_addon()); \
ten_string_destroy(base_dir); \
} \
TEN_DESTRUCTOR(____dtor_ten_declare_##NAME##_##TYPE##_addon____) { \
ten_addon_unregister_extension(#NAME); \
delete g_##NAME##_default_extension_addon; \
}
3 changes: 3 additions & 0 deletions core/include_internal/ten_runtime/app/endpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ TEN_RUNTIME_PRIVATE_API bool ten_app_is_endpoint_closed(ten_app_t *self);

TEN_RUNTIME_PRIVATE_API void ten_app_create_protocol_context_store(
ten_app_t *self);

TEN_RUNTIME_PRIVATE_API bool ten_app_is_protocol_context_store_closed(
ten_app_t *self);
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ typedef struct ten_extension_info_t {

TEN_RUNTIME_PRIVATE_API ten_extension_info_t *ten_extension_info_create(void);

TEN_RUNTIME_PRIVATE_API ten_shared_ptr_t *ten_extension_info_clone(
ten_extension_info_t *self, ten_list_t *extensions_info, ten_error_t *err);
TEN_RUNTIME_PRIVATE_API bool ten_extensions_info_clone(ten_list_t *from,
ten_list_t *to,
ten_error_t *err);

TEN_RUNTIME_PRIVATE_API bool ten_extension_info_check_integrity(
ten_extension_info_t *self, bool check_thread);
Expand All @@ -58,8 +59,7 @@ TEN_RUNTIME_PRIVATE_API bool ten_extension_info_is_desired_extension_group(
TEN_RUNTIME_PRIVATE_API ten_shared_ptr_t *get_extension_info_in_extensions_info(
ten_list_t *extensions_info, const char *app_uri, const char *graph_id,
const char *extension_group_name, const char *extension_addon_name,
const char *extension_instance_name, bool *new_one_created,
ten_error_t *err);
const char *extension_instance_name, bool should_exist, ten_error_t *err);

TEN_RUNTIME_PRIVATE_API ten_extension_info_t *ten_extension_info_from_smart_ptr(
ten_smart_ptr_t *smart_ptr);
Expand Down
7 changes: 5 additions & 2 deletions core/src/ten_runtime/app/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ ten_app_t *ten_app_create(ten_app_on_configure_func_t on_configure,
self->state = TEN_APP_STATE_INIT;

self->endpoint_protocol = NULL;
self->protocol_context_store = NULL;

ten_list_init(&self->engines);
ten_list_init(&self->orphan_connections);
Expand Down Expand Up @@ -157,8 +158,10 @@ void ten_app_destroy(ten_app_t *self) {
ten_ref_dec_ref(&self->endpoint_protocol->ref);
}

ten_protocol_context_store_destroy(self->protocol_context_store);
self->protocol_context_store = NULL;
if (self->protocol_context_store) {
ten_protocol_context_store_destroy(self->protocol_context_store);
self->protocol_context_store = NULL;
}

ten_value_deinit(&self->manifest);
ten_value_deinit(&self->property);
Expand Down
9 changes: 8 additions & 1 deletion core/src/ten_runtime/app/close.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static bool ten_app_could_be_close(ten_app_t *self) {
TEN_ASSERT(self && ten_app_check_integrity(self, true), "Should not happen.");

if (ten_app_has_no_work(self) && ten_app_is_endpoint_closed(self) &&
ten_protocol_context_store_is_closed(self->protocol_context_store)) {
ten_app_is_protocol_context_store_closed(self)) {
return true;
}

Expand Down Expand Up @@ -86,6 +86,13 @@ static void ten_app_close_task(void *app_, TEN_UNUSED void *arg) {
ten_app_t *app = (ten_app_t *)app_;
TEN_ASSERT(app_ && ten_app_check_integrity(app_, true), "Should not happen.");

// The app might be closed due to the problems during creation, ex: some
// property is invalid. And all resources have not been created yet.
if (ten_app_could_be_close(app)) {
ten_app_proceed_to_close(app);
return;
}

ten_app_close_sync(app);
}

Expand Down
11 changes: 11 additions & 0 deletions core/src/ten_runtime/app/endpoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,14 @@ void ten_app_create_protocol_context_store(ten_app_t *self) {
self->protocol_context_store, ten_app_on_protocol_context_store_closed,
self);
}

bool ten_app_is_protocol_context_store_closed(ten_app_t *self) {
TEN_ASSERT(self && ten_app_check_integrity(self, true),
"Access across threads.");

if (!self->protocol_context_store) {
return true;
}

return ten_protocol_context_store_is_closed(self->protocol_context_store);
}
54 changes: 36 additions & 18 deletions core/src/ten_runtime/app/predefined_graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,9 @@ bool ten_app_get_predefined_graph_extensions_and_groups_info_by_name(
return false;
}

ten_list_foreach (&predefined_graph_info->extensions_info, iter) {
ten_extension_info_t *extension_info =
ten_shared_ptr_get_data(ten_smart_ptr_listnode_get(iter.node));
if (!ten_extension_info_clone(extension_info, extensions_info, err)) {
return false;
}
if (!ten_extensions_info_clone(
extensions_info, &predefined_graph_info->extensions_info, err)) {
return false;
}

ten_list_foreach (&predefined_graph_info->extension_groups_info, iter) {
Expand Down Expand Up @@ -316,8 +313,11 @@ bool ten_app_get_predefined_graphs_from_property(ten_app_t *self) {
goto done;
}

int graph_idx = -1;
ten_list_foreach (ten_value_peek_array(predefined_graphs),
predefined_graphs_iter) {
graph_idx++;

ten_value_t *predefined_graph_info_value =
ten_ptr_listnode_get(predefined_graphs_iter.node);
TEN_ASSERT(predefined_graph_info_value &&
Expand Down Expand Up @@ -361,6 +361,7 @@ bool ten_app_get_predefined_graphs_from_property(ten_app_t *self) {
ten_value_get_bool(predefined_graph_info_singleton_value, &err);
}

// Parse 'nodes'.
ten_value_t *predefined_graph_info_nodes_value =
ten_value_object_peek(predefined_graph_info_value, TEN_STR_NODES);
if (predefined_graph_info_nodes_value &&
Expand Down Expand Up @@ -390,18 +391,26 @@ bool ten_app_get_predefined_graphs_from_property(ten_app_t *self) {
}

const char *type = ten_value_peek_raw_str(type_value);
if (!strcmp(type, TEN_STR_EXTENSION)) {
ten_extension_info_node_from_value(

// Only the extension node is preferred.
result = ten_c_string_is_equal(type, TEN_STR_EXTENSION);
if (result) {
ten_shared_ptr_t *extension_info = ten_extension_info_node_from_value(
predefined_graph_info_node_item_value,
&predefined_graph_info->extensions_info, NULL);
} else {
&predefined_graph_info->extensions_info, &err);
if (!extension_info) {
result = false;
}
}

if (!result) {
ten_predefined_graph_info_destroy(predefined_graph_info);
result = false;
goto done;
}
}
}

// Parse 'connections'.
ten_value_t *predefined_graph_info_connections_value =
ten_value_object_peek(predefined_graph_info_value, TEN_STR_CONNECTIONS);
if (predefined_graph_info_connections_value &&
Expand All @@ -415,16 +424,23 @@ bool ten_app_get_predefined_graphs_from_property(ten_app_t *self) {
predefined_graph_info_connection_item_value),
"Invalid argument.");

if (!predefined_graph_info_connection_item_value ||
!ten_value_is_object(predefined_graph_info_connection_item_value)) {
result =
predefined_graph_info_connection_item_value &&
ten_value_is_object(predefined_graph_info_connection_item_value);
if (result) {
ten_shared_ptr_t *src_extension_in_connection =
ten_extension_info_parse_connection_src_part_from_value(
predefined_graph_info_connection_item_value,
&predefined_graph_info->extensions_info, &err);
if (!src_extension_in_connection) {
result = false;
}
}

if (!result) {
ten_predefined_graph_info_destroy(predefined_graph_info);
result = false;
goto done;
}

ten_extension_info_parse_connection_src_part_from_value(
predefined_graph_info_connection_item_value,
&predefined_graph_info->extensions_info, NULL);
}
}

Expand All @@ -449,6 +465,8 @@ bool ten_app_get_predefined_graphs_from_property(ten_app_t *self) {
done:
if (result == false) {
ten_list_clear(&self->predefined_graph_infos);
TEN_LOGE("[%s] Failed to parse predefined_graphs[%d], %s",
ten_app_get_uri(self), graph_idx, ten_error_errmsg(&err));
}

ten_error_deinit(&err);
Expand Down
Loading

0 comments on commit 0188c96

Please sign in to comment.