Skip to content

Commit e2e402a

Browse files
authored
Merge branch 'main' into stepfun-demo
2 parents 2896e5e + 3088758 commit e2e402a

File tree

4 files changed

+162
-32
lines changed

4 files changed

+162
-32
lines changed

core/src/ten_runtime/binding/go/native/ten_env/ten_env_create_instance_done.c

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,52 @@
44
// Licensed under the Apache License, Version 2.0, with certain conditions.
55
// Refer to the "LICENSE" file in the root directory for more information.
66
//
7+
#include "include_internal/ten_runtime/addon/addon_host.h"
8+
#include "include_internal/ten_runtime/app/app.h"
79
#include "include_internal/ten_runtime/binding/go/extension/extension.h"
810
#include "include_internal/ten_runtime/binding/go/ten_env/ten_env.h"
911
#include "include_internal/ten_runtime/binding/go/ten_env/ten_env_internal.h"
1012
#include "ten_runtime/binding/go/interface/ten_runtime/ten_env.h"
1113
#include "ten_runtime/extension/extension.h"
1214
#include "ten_runtime/ten.h"
1315
#include "ten_utils/macro/check.h"
16+
#include "ten_utils/macro/memory.h"
17+
18+
typedef struct ten_go_ten_env_on_create_instance_done_ctx_t {
19+
ten_addon_host_t *addon_host;
20+
void *instance;
21+
void *context;
22+
} ten_go_ten_env_on_create_instance_done_ctx_t;
23+
24+
static void ten_app_addon_host_on_create_instance_done(void *from, void *args) {
25+
ten_app_t *app = (ten_app_t *)from;
26+
TEN_ASSERT(app, "Should not happen.");
27+
TEN_ASSERT(ten_app_check_integrity(app, true), "Should not happen.");
28+
29+
ten_go_ten_env_on_create_instance_done_ctx_t *ctx =
30+
(ten_go_ten_env_on_create_instance_done_ctx_t *)args;
31+
TEN_ASSERT(ctx, "Should not happen.");
32+
33+
ten_addon_host_t *addon_host = ctx->addon_host;
34+
TEN_ASSERT(addon_host, "Should not happen.");
35+
TEN_ASSERT(ten_addon_host_check_integrity(addon_host, true),
36+
"Should not happen.");
37+
38+
ten_error_t err;
39+
TEN_ERROR_INIT(err);
40+
41+
bool rc = ten_env_on_create_instance_done(addon_host->ten_env, ctx->instance,
42+
ctx->context, &err);
43+
if (!rc) {
44+
TEN_LOGE("ten_env.on_create_instance_done() in go binding failed: %s",
45+
ten_error_message(&err));
46+
TEN_ASSERT(0, "Should not happen.");
47+
}
48+
49+
ten_error_deinit(&err);
50+
51+
TEN_FREE(ctx);
52+
}
1453

1554
void ten_go_ten_env_on_create_instance_done(uintptr_t bridge_addr,
1655
uintptr_t instance_bridge_addr,
@@ -19,28 +58,45 @@ void ten_go_ten_env_on_create_instance_done(uintptr_t bridge_addr,
1958
TEN_ASSERT(self && ten_go_ten_env_check_integrity(self), "Invalid argument.");
2059
TEN_ASSERT(context_addr, "Invalid argument.");
2160

22-
ten_extension_t *c_extension_or_extension_group = NULL;
61+
ten_extension_t *c_extension = NULL;
2362
if (instance_bridge_addr) {
2463
ten_go_extension_t *extension_bridge =
2564
ten_go_extension_reinterpret(instance_bridge_addr);
2665
TEN_ASSERT(ten_go_extension_check_integrity(extension_bridge),
2766
"Should not happen.");
28-
c_extension_or_extension_group =
29-
ten_go_extension_c_extension(extension_bridge);
67+
c_extension = ten_go_extension_c_extension(extension_bridge);
3068
}
3169

3270
TEN_GO_TEN_ENV_IS_ALIVE_REGION_BEGIN(self, {});
3371

34-
ten_error_t err;
35-
TEN_ERROR_INIT(err);
72+
ten_env_t *c_ten_env = self->c_ten_env;
73+
TEN_ASSERT(c_ten_env, "Should not happen.");
74+
TEN_ASSERT(ten_env_check_integrity(c_ten_env, false), "Should not happen.");
3675

37-
// TODO(xilin): Switch to the addon_host thread.
38-
bool rc = ten_env_on_create_instance_done(self->c_ten_env,
39-
c_extension_or_extension_group,
40-
(void *)context_addr, &err);
41-
TEN_ASSERT(rc, "Should not happen.");
76+
TEN_ASSERT(c_ten_env->attach_to == TEN_ENV_ATTACH_TO_ADDON,
77+
"Should not happen.");
4278

43-
ten_error_deinit(&err);
79+
ten_addon_host_t *addon_host = ten_env_get_attached_addon(c_ten_env);
80+
TEN_ASSERT(addon_host, "Should not happen.");
81+
TEN_ASSERT(ten_addon_host_check_integrity(addon_host, false),
82+
"Should not happen.");
83+
84+
ten_app_t *app = addon_host->attached_app;
85+
TEN_ASSERT(app, "Should not happen.");
86+
TEN_ASSERT(ten_app_check_integrity(app, false), "Should not happen.");
87+
88+
ten_go_ten_env_on_create_instance_done_ctx_t *ctx =
89+
TEN_MALLOC(sizeof(ten_go_ten_env_on_create_instance_done_ctx_t));
90+
TEN_ASSERT(ctx, "Failed to allocate memory.");
91+
92+
ctx->addon_host = addon_host;
93+
ctx->instance = c_extension;
94+
ctx->context = (void *)context_addr;
95+
96+
int post_task_rc = ten_runloop_post_task_tail(
97+
ten_app_get_attached_runloop(app),
98+
ten_app_addon_host_on_create_instance_done, app, ctx);
99+
TEN_ASSERT(post_task_rc == 0, "Failed to post task.");
44100

45101
TEN_GO_TEN_ENV_IS_ALIVE_REGION_END(self);
46102

core/src/ten_runtime/binding/python/native/ten_env/ten_env_on_create_instance_done.c

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,59 @@
44
// Licensed under the Apache License, Version 2.0, with certain conditions.
55
// Refer to the "LICENSE" file in the root directory for more information.
66
//
7+
#include "include_internal/ten_runtime/addon/addon_host.h"
8+
#include "include_internal/ten_runtime/app/app.h"
79
#include "include_internal/ten_runtime/binding/python/common/error.h"
810
#include "include_internal/ten_runtime/binding/python/extension/extension.h"
911
#include "include_internal/ten_runtime/binding/python/ten_env/ten_env.h"
1012
#include "object.h"
13+
#include "ten_runtime/app/app.h"
1114
#include "ten_runtime/ten_env/internal/on_xxx_done.h"
1215
#include "ten_utils/macro/mark.h"
16+
#include "ten_utils/macro/memory.h"
17+
18+
typedef struct ten_py_ten_env_on_create_instance_done_ctx_t {
19+
ten_addon_host_t *addon_host;
20+
void *instance;
21+
void *context;
22+
} ten_py_ten_env_on_create_instance_done_ctx_t;
23+
24+
static void ten_app_addon_host_on_create_instance_done(void *from, void *args) {
25+
ten_app_t *app = (ten_app_t *)from;
26+
TEN_ASSERT(app, "Should not happen.");
27+
TEN_ASSERT(ten_app_check_integrity(app, true), "Should not happen.");
28+
29+
ten_py_ten_env_on_create_instance_done_ctx_t *ctx =
30+
(ten_py_ten_env_on_create_instance_done_ctx_t *)args;
31+
TEN_ASSERT(ctx, "Should not happen.");
32+
33+
ten_addon_host_t *addon_host = ctx->addon_host;
34+
TEN_ASSERT(addon_host, "Should not happen.");
35+
TEN_ASSERT(ten_addon_host_check_integrity(addon_host, true),
36+
"Should not happen.");
37+
38+
ten_error_t err;
39+
TEN_ERROR_INIT(err);
40+
41+
bool rc = ten_env_on_create_instance_done(addon_host->ten_env, ctx->instance,
42+
ctx->context, &err);
43+
if (!rc) {
44+
TEN_LOGE("ten_env.on_create_instance_done() in python binding failed: %s",
45+
ten_error_message(&err));
46+
TEN_ASSERT(0, "Should not happen.");
47+
}
48+
49+
ten_error_deinit(&err);
50+
51+
TEN_FREE(ctx);
52+
}
1353

1454
PyObject *ten_py_ten_env_on_create_instance_done(PyObject *self,
1555
TEN_UNUSED PyObject *args) {
1656
ten_py_ten_env_t *py_ten_env = (ten_py_ten_env_t *)self;
1757
TEN_ASSERT(py_ten_env && ten_py_ten_env_check_integrity(py_ten_env),
1858
"Invalid argument.");
1959

20-
ten_error_t err;
21-
TEN_ERROR_INIT(err);
22-
2360
ten_py_extension_t *extension = NULL;
2461
void *context = NULL;
2562

@@ -29,17 +66,39 @@ PyObject *ten_py_ten_env_on_create_instance_done(PyObject *self,
2966
"Invalid argument count when ten_env.on_create_instance_done.");
3067
}
3168

32-
// TODO(xilin): Switch to the addon_host thread.
33-
bool rc = ten_env_on_create_instance_done(
34-
py_ten_env->c_ten_env, extension->c_extension, context, &err);
35-
TEN_ASSERT(rc, "Should not happen.");
69+
ten_env_t *c_ten_env = py_ten_env->c_ten_env;
70+
TEN_ASSERT(c_ten_env, "Should not happen.");
71+
TEN_ASSERT(ten_env_check_integrity(c_ten_env, false), "Should not happen.");
72+
73+
TEN_ASSERT(c_ten_env->attach_to == TEN_ENV_ATTACH_TO_ADDON,
74+
"Should not happen.");
75+
76+
ten_addon_host_t *addon_host = ten_env_get_attached_addon(c_ten_env);
77+
TEN_ASSERT(addon_host, "Should not happen.");
78+
TEN_ASSERT(ten_addon_host_check_integrity(addon_host, false),
79+
"Should not happen.");
80+
81+
ten_app_t *app = addon_host->attached_app;
82+
TEN_ASSERT(app, "Should not happen.");
83+
TEN_ASSERT(ten_app_check_integrity(app, false), "Should not happen.");
84+
85+
ten_py_ten_env_on_create_instance_done_ctx_t *ctx =
86+
TEN_MALLOC(sizeof(ten_py_ten_env_on_create_instance_done_ctx_t));
87+
TEN_ASSERT(ctx, "Failed to allocate memory.");
88+
89+
ctx->addon_host = addon_host;
90+
ctx->instance = extension->c_extension;
91+
ctx->context = context;
92+
93+
int post_task_rc = ten_runloop_post_task_tail(
94+
ten_app_get_attached_runloop(app),
95+
ten_app_addon_host_on_create_instance_done, app, ctx);
96+
TEN_ASSERT(post_task_rc == 0, "Failed to post task.");
3697

3798
// It's necessary to keep the reference of the extension object to
3899
// prevent the python object from being destroyed when GC is triggered util
39100
// the addon's 'on_destroy_instance' function is called.
40101
Py_INCREF(extension);
41102

42-
ten_error_deinit(&err);
43-
44103
Py_RETURN_NONE;
45104
}

core/src/ten_rust/src/graph/graph_info.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,16 @@ pub fn load_graph_from_uri(
3333
base_dir: Option<&str>,
3434
new_base_dir: &mut Option<String>,
3535
) -> Result<Graph> {
36-
// Try to parse as URL first
36+
// First check if it's an absolute path - these are not supported
37+
if Path::new(uri).is_absolute() {
38+
return Err(anyhow!(
39+
"Absolute paths are not supported in import_uri: {}. Use file:// \
40+
URI or relative path instead",
41+
uri
42+
));
43+
}
44+
45+
// Try to parse as URL
3746
if let Ok(url) = Url::parse(uri) {
3847
match url.scheme() {
3948
"http" | "https" => {
@@ -52,15 +61,6 @@ pub fn load_graph_from_uri(
5261
}
5362
}
5463

55-
// Handle relative paths only - absolute paths are not supported
56-
if Path::new(uri).is_absolute() {
57-
return Err(anyhow!(
58-
"Absolute paths are not supported in import_uri: {}. Use file:// \
59-
URI or relative path instead",
60-
uri
61-
));
62-
}
63-
6464
// For relative paths, base_dir must not be None
6565
let base_dir = base_dir.ok_or_else(|| {
6666
anyhow!("base_dir cannot be None when uri is a relative path")

tests/ten_runtime/integration/python/call_api_after_closing_python/call_api_after_closing_python_app/ten_packages/extension/default_extension_python/addon.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# Licensed under the Apache License, Version 2.0, with certain conditions.
55
# Refer to the "LICENSE" file in the root directory for more information.
66
#
7+
import time
8+
import threading
79
from ten_runtime import (
810
Addon,
911
register_addon_as_extension,
@@ -14,12 +16,25 @@
1416

1517
@register_addon_as_extension("default_extension_python")
1618
class DefaultExtensionAddon(Addon):
17-
def on_create_instance(self, ten_env: TenEnv, name: str, context) -> None:
18-
ten_env.log_info("on_create_instance" + name)
19+
20+
def thread_on_create_instance_done(
21+
self, ten_env: TenEnv, name: str, context
22+
) -> None:
23+
# sleep 1 second to mock the time-consuming operation.
24+
time.sleep(1)
1925

2026
if name == "server":
2127
ten_env.on_create_instance_done(ServerExtension(name), context)
2228
elif name == "client":
2329
ten_env.on_create_instance_done(ClientExtension(name), context)
2430
else:
2531
assert False
32+
33+
def on_create_instance(self, ten_env: TenEnv, name: str, context) -> None:
34+
ten_env.log_info("on_create_instance" + name)
35+
36+
# Create a new thread to call the on_create_instance_done function.
37+
threading.Thread(
38+
target=self.thread_on_create_instance_done,
39+
args=(ten_env, name, context),
40+
).start()

0 commit comments

Comments
 (0)