Skip to content

Commit 3135c62

Browse files
committed
fix: refine codes
1 parent 2d6be6c commit 3135c62

File tree

25 files changed

+131
-350
lines changed

25 files changed

+131
-350
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
"request": "launch",
138138
"program": "${workspaceFolder}/out/linux/x64/tests/standalone/ten_runtime_smoke_test",
139139
"args": [
140-
"--gtest_filter=ExtensionTest.FailedToConnectToRemote2"
140+
"--gtest_filter=ExtensionTest.CommandInvalidExtensionGroup"
141141
],
142142
"cwd": "${workspaceFolder}/out/linux/x64/tests/standalone/",
143143
"env": {

core/include_internal/ten_runtime/extension_group/extension_group.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "ten_utils/container/list.h"
1717
#include "ten_utils/io/runloop.h"
1818
#include "ten_utils/lib/signature.h"
19-
#include "ten_utils/lib/smart_ptr.h"
2019
#include "ten_utils/sanitizer/thread_check.h"
2120

2221
#define TEN_EXTENSION_GROUP_SIGNATURE 0x94F72EDA6137DF04U
@@ -124,10 +123,6 @@ TEN_RUNTIME_PRIVATE_API void ten_extension_group_destroy_extensions(
124123
TEN_RUNTIME_PRIVATE_API void ten_extension_group_set_addon(
125124
ten_extension_group_t *self, ten_addon_host_t *addon_host);
126125

127-
TEN_RUNTIME_PRIVATE_API ten_shared_ptr_t *
128-
ten_extension_group_create_cmd_result_for_invalid_dest(
129-
ten_shared_ptr_t *origin_cmd, ten_string_t *target_group_name);
130-
131126
TEN_RUNTIME_PRIVATE_API ten_runloop_t *ten_extension_group_get_attached_runloop(
132127
ten_extension_group_t *self);
133128

core/include_internal/ten_runtime/extension_group/msg_interface/common.h

Lines changed: 0 additions & 19 deletions
This file was deleted.

core/src/ten_runtime/app/msg_interface/common.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ void ten_app_do_connection_migration_or_push_to_engine_queue(
5252
// TEN_NOLINTNEXTLINE(thread-check)
5353
// thread-check: We are in the app thread, and all the uses of the engine in
5454
// this function would not cause thread safety issues.
55-
TEN_ASSERT(engine && ten_engine_check_integrity(engine, false),
56-
"This function is called in the app thread.");
55+
TEN_ASSERT(engine, "Should not happen.");
56+
TEN_ASSERT(ten_engine_check_integrity(engine, false), "Should not happen.");
5757

5858
if (connection && ten_connection_needs_to_migrate(connection, engine)) {
5959
ten_connection_migrate(connection, engine, msg);
@@ -317,8 +317,8 @@ static bool ten_app_handle_stop_graph_cmd(ten_app_t *self,
317317
// to the engine.
318318
ten_list_foreach (ten_msg_get_dest(cmd), iter) {
319319
ten_loc_t *dest_loc = ten_ptr_listnode_get(iter.node);
320-
TEN_ASSERT(dest_loc && ten_loc_check_integrity(dest_loc),
321-
"Should not happen.");
320+
TEN_ASSERT(dest_loc, "Should not happen.");
321+
TEN_ASSERT(ten_loc_check_integrity(dest_loc), "Should not happen.");
322322

323323
ten_string_set_formatted(&dest_loc->graph_id, "%s",
324324
ten_string_get_raw_str(&dest_engine->graph_id));
@@ -354,8 +354,8 @@ static bool ten_app_handle_cmd_result(ten_app_t *self,
354354

355355
#if defined(_DEBUG)
356356
ten_loc_t *dest_loc = ten_msg_get_first_dest_loc(cmd_result);
357-
TEN_ASSERT(dest_loc && ten_loc_check_integrity(dest_loc),
358-
"Should not happen.");
357+
TEN_ASSERT(dest_loc, "Should not happen.");
358+
TEN_ASSERT(ten_loc_check_integrity(dest_loc), "Should not happen.");
359359

360360
ten_string_t loc_str;
361361
TEN_STRING_INIT(loc_str);
@@ -405,9 +405,9 @@ static bool ten_app_handle_cmd_result(ten_app_t *self,
405405
bool ten_app_dispatch_msg(ten_app_t *self, ten_shared_ptr_t *msg,
406406
ten_error_t *err) {
407407
ten_loc_t *dest_loc = ten_msg_get_first_dest_loc(msg);
408-
TEN_ASSERT(dest_loc && ten_loc_check_integrity(dest_loc) &&
409-
ten_msg_get_dest_cnt(msg) == 1,
410-
"Should not happen.");
408+
TEN_ASSERT(dest_loc, "Should not happen.");
409+
TEN_ASSERT(ten_loc_check_integrity(dest_loc), "Should not happen.");
410+
TEN_ASSERT(ten_msg_get_dest_cnt(msg) == 1, "Should not happen.");
411411
TEN_ASSERT(!ten_string_is_empty(&dest_loc->app_uri),
412412
"App URI should not be empty.");
413413

core/src/ten_runtime/connection/migration.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,9 @@ void ten_connection_upgrade_migration_state_to_done(ten_connection_t *self,
134134
if (engine) {
135135
// The message is sent to the app, not an engine.
136136

137-
TEN_ASSERT(engine && ten_engine_check_integrity(engine, true),
138-
"Access across threads.");
137+
TEN_ASSERT(engine, "Invalid argument.");
138+
TEN_ASSERT(ten_engine_check_integrity(engine, true),
139+
"Invalid use of engine %p.", engine);
139140

140141
// @{
141142
// Attach to engine.

core/src/ten_runtime/engine/engine.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ static void ten_engine_set_graph_id(ten_engine_t *self, ten_shared_ptr_t *cmd) {
139139
// Set the newly created graph_id to the 'start_graph' command.
140140
ten_list_foreach (ten_msg_get_dest(cmd), iter) {
141141
ten_loc_t *dest_loc = ten_ptr_listnode_get(iter.node);
142-
TEN_ASSERT(dest_loc && ten_loc_check_integrity(dest_loc),
143-
"Should not happen.");
142+
TEN_ASSERT(dest_loc, "Should not happen.");
143+
TEN_ASSERT(ten_loc_check_integrity(dest_loc), "Should not happen.");
144144

145145
ten_string_set_formatted(&dest_loc->graph_id, "%s",
146146
ten_string_get_raw_str(&graph_id_str));

core/src/ten_runtime/engine/internal/extension_interface.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ static void ten_engine_on_all_extension_threads_are_ready(
181181

182182
if (error_occurred) {
183183
ten_app_t *app = self->app;
184-
TEN_ASSERT(app && ten_app_check_integrity(app, false),
185-
"Invalid argument.");
184+
TEN_ASSERT(app, "Should not happen.");
185+
TEN_ASSERT(ten_app_check_integrity(app, false), "Should not happen.");
186186

187187
// This graph/engine will not be functioning properly, so it will be shut
188188
// down directly.

core/src/ten_runtime/engine/msg_interface/common.c

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "include_internal/ten_runtime/msg/msg.h"
2525
#include "include_internal/ten_runtime/msg/msg_info.h"
2626
#include "include_internal/ten_runtime/remote/remote.h"
27+
#include "include_internal/ten_utils/value/value.h"
2728
#include "ten_runtime/app/app.h"
2829
#include "ten_runtime/msg/cmd_result/cmd_result.h"
2930
#include "ten_runtime/msg/msg.h"
@@ -363,18 +364,37 @@ static void ten_engine_post_msg_to_extension_thread(
363364
}
364365
}
365366

367+
ten_shared_ptr_t *ten_engine_create_cmd_result_for_invalid_dest(
368+
ten_shared_ptr_t *origin_cmd) {
369+
TEN_ASSERT(origin_cmd, "Should not happen.");
370+
371+
if (!ten_msg_is_cmd_and_result(origin_cmd)) {
372+
ten_msg_dump(origin_cmd, NULL, "Unexpected message: ^m");
373+
TEN_ASSERT(0, "Should not happen.");
374+
}
375+
376+
ten_shared_ptr_t *cmd_result =
377+
ten_cmd_result_create_from_cmd(TEN_STATUS_CODE_ERROR, origin_cmd);
378+
ten_msg_set_property(cmd_result, TEN_STR_DETAIL,
379+
ten_value_create_vstring("Failed to find destination."),
380+
NULL);
381+
382+
return cmd_result;
383+
}
384+
366385
bool ten_engine_dispatch_msg(ten_engine_t *self, ten_shared_ptr_t *msg) {
367386
TEN_ASSERT(self, "Should not happen.");
368387
TEN_ASSERT(ten_engine_check_integrity(self, true), "Should not happen.");
388+
369389
TEN_ASSERT(msg, "Should not happen.");
370390
TEN_ASSERT(ten_msg_check_integrity(msg), "Should not happen.");
371391
TEN_ASSERT(ten_msg_get_dest_cnt(msg) == 1,
372392
"When this function is executed, there should be only one "
373393
"destination remaining in the message's dest.");
374394

375395
ten_loc_t *dest_loc = ten_msg_get_first_dest_loc(msg);
376-
TEN_ASSERT(dest_loc && ten_loc_check_integrity(dest_loc),
377-
"Should not happen.");
396+
TEN_ASSERT(dest_loc, "Should not happen.");
397+
TEN_ASSERT(ten_loc_check_integrity(dest_loc), "Should not happen.");
378398

379399
ten_app_t *app = self->app;
380400
TEN_ASSERT(app, "Invalid argument.");
@@ -426,34 +446,38 @@ bool ten_engine_dispatch_msg(ten_engine_t *self, ten_shared_ptr_t *msg) {
426446
ten_string_get_raw_str(&dest_loc->graph_id),
427447
ten_string_get_raw_str(&dest_loc->extension_name));
428448

429-
ten_list_foreach (&self->extension_context->extension_threads, iter) {
430-
ten_extension_thread_t *extension_thread =
431-
ten_ptr_listnode_get(iter.node);
432-
TEN_ASSERT(
433-
extension_thread &&
434-
// TEN_NOLINTNEXTLINE(thread-check)
435-
// thread-check: We are in the engine thread, _not_ in the
436-
// extension thread. However, before the engine is closed,
437-
// the pointer of the extension group and the pointer of the
438-
// extension thread will not be changed, and the closing of
439-
// the entire engine must start from the engine, so the
440-
// execution to this position means that the engine has not
441-
// been closed, so there will be no thread safety issue.
442-
ten_extension_thread_check_integrity(extension_thread,
443-
false),
444-
"Should not happen.");
445-
446-
ten_extension_group_t *extension_group =
447-
extension_thread->extension_group;
448-
449-
if (ten_string_is_equal_c_str(&extension_group->name,
450-
extension_group_name)) {
451-
// Find the correct extension thread, ask it to handle the
452-
// message.
453-
found = true;
454-
ten_engine_post_msg_to_extension_thread(self, extension_thread,
455-
msg);
456-
break;
449+
if (extension_group_name) {
450+
ten_list_foreach (&self->extension_context->extension_threads,
451+
iter) {
452+
ten_extension_thread_t *extension_thread =
453+
ten_ptr_listnode_get(iter.node);
454+
TEN_ASSERT(
455+
extension_thread &&
456+
// TEN_NOLINTNEXTLINE(thread-check)
457+
// thread-check: We are in the engine thread, _not_ in the
458+
// extension thread. However, before the engine is closed,
459+
// the pointer of the extension group and the pointer of
460+
// the extension thread will not be changed, and the
461+
// closing of the entire engine must start from the
462+
// engine, so the execution to this position means that
463+
// the engine has not been closed, so there will be no
464+
// thread safety issue.
465+
ten_extension_thread_check_integrity(extension_thread,
466+
false),
467+
"Should not happen.");
468+
469+
ten_extension_group_t *extension_group =
470+
extension_thread->extension_group;
471+
472+
if (ten_string_is_equal_c_str(&extension_group->name,
473+
extension_group_name)) {
474+
// Find the correct extension thread, ask it to handle the
475+
// message.
476+
found = true;
477+
ten_engine_post_msg_to_extension_thread(self, extension_thread,
478+
msg);
479+
break;
480+
}
457481
}
458482
}
459483

@@ -464,8 +488,7 @@ bool ten_engine_dispatch_msg(ten_engine_t *self, ten_shared_ptr_t *msg) {
464488

465489
if (ten_msg_is_cmd(msg)) {
466490
ten_shared_ptr_t *cmd_result =
467-
ten_extension_group_create_cmd_result_for_invalid_dest(
468-
msg, &dest_loc->extension_group_name);
491+
ten_engine_create_cmd_result_for_invalid_dest(msg);
469492

470493
ten_engine_dispatch_msg(self, cmd_result);
471494

core/src/ten_runtime/extension/extension.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@ static void ten_extension_determine_out_msg_dest_from_msg(
289289

290290
ten_list_foreach (&dests, iter) {
291291
ten_loc_t *dest_loc = ten_ptr_listnode_get(iter.node);
292-
TEN_ASSERT(dest_loc && ten_loc_check_integrity(dest_loc),
293-
"Should not happen.");
292+
TEN_ASSERT(dest_loc, "Should not happen.");
293+
TEN_ASSERT(ten_loc_check_integrity(dest_loc), "Should not happen.");
294294

295295
bool need_to_clone_msg = need_to_clone_msg_when_sending(msg, iter.index);
296296

core/src/ten_runtime/extension_context/extension_context.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,9 @@ const char *ten_extension_context_get_extension_group_name(
528528
ten_extension_info_t *extension_info =
529529
ten_extension_context_get_extension_info_by_name(self, app_uri, graph_id,
530530
extension_name);
531-
TEN_ASSERT(extension_info, "Should not happen.");
531+
if (!extension_info) {
532+
return NULL;
533+
}
532534

533535
return ten_string_get_raw_str(&extension_info->loc.extension_group_name);
534536
}

core/src/ten_runtime/extension_group/BUILD.gn

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ ten_runtime_glob("extension_group") {
1212
"builtin",
1313
"extension_group_info",
1414
"internal",
15-
"msg_interface",
1615
"ten_env",
1716
]
1817
}

core/src/ten_runtime/extension_group/extension_group.c

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,16 @@
1010
#include <time.h>
1111

1212
#include "include_internal/ten_runtime/addon/addon_host.h"
13-
#include "include_internal/ten_runtime/common/constant_str.h"
1413
#include "include_internal/ten_runtime/extension/extension.h"
1514
#include "include_internal/ten_runtime/extension_thread/extension_thread.h"
1615
#include "include_internal/ten_runtime/msg/msg.h"
1716
#include "include_internal/ten_runtime/ten_env/ten_env.h"
18-
#include "include_internal/ten_utils/log/log.h"
19-
#include "include_internal/ten_utils/value/value.h"
2017
#include "ten_runtime/binding/common.h"
21-
#include "ten_runtime/msg/cmd_result/cmd_result.h"
2218
#include "ten_runtime/ten_env/ten_env.h"
2319
#include "ten_utils/container/list_node.h"
2420
#include "ten_utils/lib/alloc.h"
2521
#include "ten_utils/lib/error.h"
2622
#include "ten_utils/lib/ref.h"
27-
#include "ten_utils/lib/smart_ptr.h"
2823
#include "ten_utils/macro/check.h"
2924
#include "ten_utils/sanitizer/thread_check.h"
3025
#include "ten_utils/value/value.h"
@@ -226,27 +221,6 @@ void ten_extension_group_set_addon(ten_extension_group_t *self,
226221
ten_ref_inc_ref(&addon_host->ref);
227222
}
228223

229-
ten_shared_ptr_t *ten_extension_group_create_cmd_result_for_invalid_dest(
230-
ten_shared_ptr_t *origin_cmd, ten_string_t *target_group_name) {
231-
TEN_ASSERT(origin_cmd, "Should not happen.");
232-
TEN_ASSERT(target_group_name, "Should not happen.");
233-
234-
if (!ten_msg_is_cmd_and_result(origin_cmd)) {
235-
ten_msg_dump(origin_cmd, NULL, "Unexpected message: ^m");
236-
TEN_ASSERT(0, "Should not happen.");
237-
}
238-
239-
ten_shared_ptr_t *cmd_result =
240-
ten_cmd_result_create_from_cmd(TEN_STATUS_CODE_ERROR, origin_cmd);
241-
ten_msg_set_property(
242-
cmd_result, TEN_STR_DETAIL,
243-
ten_value_create_vstring("The extension group[%s] is invalid.",
244-
ten_string_get_raw_str(target_group_name)),
245-
NULL);
246-
247-
return cmd_result;
248-
}
249-
250224
ten_runloop_t *ten_extension_group_get_attached_runloop(
251225
ten_extension_group_t *self) {
252226
TEN_ASSERT(self, "Should not happen.");

core/src/ten_runtime/extension_group/msg_interface/BUILD.gn

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)