Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: coredump when cmd has not connected #29

Merged
merged 6 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions core/src/ten_runtime/binding/go/interface/ten/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
)

type Extension interface {
OnConfigure(tenEnv TenEnv)
OnInit(
tenEnv TenEnv,
)
Expand All @@ -35,6 +36,10 @@ type DefaultExtension struct{}

var _ Extension = new(DefaultExtension)

func (p *DefaultExtension) OnConfigure(tenEnv TenEnv) {
tenEnv.OnConfigureDone()
}

func (p *DefaultExtension) OnInit(
tenEnv TenEnv,
) {
Expand Down Expand Up @@ -132,8 +137,8 @@ func newExtensionWithBridge(
}

//
//export tenGoExtensionOnInit
func tenGoExtensionOnInit(
//export tenGoExtensionOnConfigure
func tenGoExtensionOnConfigure(
extensionID C.uintptr_t,
tenEnvID C.uintptr_t,
) {
Expand Down Expand Up @@ -165,6 +170,41 @@ func tenGoExtensionOnInit(

tenEnvInstance.attachToExtension(extensionObj)

extensionObj.OnConfigure(tenEnvObj)
}

//
//export tenGoExtensionOnInit
func tenGoExtensionOnInit(
extensionID C.uintptr_t,
tenEnvID C.uintptr_t,
) {
extensionObj, ok := loadImmutableHandle(goHandle(extensionID)).(*extension)
if !ok {
panic(
fmt.Sprintf(
"Failed to get extension from handle map, id: %d.",
uintptr(extensionID),
),
)
}

tenEnvObj, ok := handle(tenEnvID).get().(TenEnv)
if !ok {
panic(
fmt.Sprintf(
"Failed to get ten from handle map, id: %d.",
uintptr(tenEnvID),
),
)
}

tenEnvInstance, ok := tenEnvObj.(*tenEnv)
if tenEnvInstance == nil || !ok {
// Should not happen.
panic("Invalid ten object type.")
}

// As the `extension` struct embeds a user-defined extension instance
// implements the Extension interface, the `OnInit` method can be called
// directly on the `extensionObj` object. The receiver of the `OnInit`
Expand Down
1 change: 1 addition & 0 deletions core/src/ten_runtime/binding/go/interface/ten/ten_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type TenEnv interface {
) error

InitPropertyFromJSONBytes(value []byte) error
InitManifestFromJSONBytes(value []byte) error
leoadonia marked this conversation as resolved.
Show resolved Hide resolved

LogVerbose(msg string)
LogDebug(msg string)
Expand Down
3 changes: 3 additions & 0 deletions core/src/ten_runtime/binding/go/interface/ten/ten_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ ten_go_status_t ten_go_ten_env_set_property_json_bytes(uintptr_t bridge_addr,
ten_go_status_t ten_go_ten_env_init_property_from_json_bytes(
uintptr_t bridge_addr, const void *json_str, int json_str_len);

ten_go_status_t ten_go_ten_env_init_manifest_from_json_bytes(
uintptr_t bridge_addr, const void *json_str, int json_str_len);

void ten_go_ten_env_log(uintptr_t bridge_addr, int level, const void *func_name,
int func_name_len, const void *file_name,
int file_name_len, int line_no, const void *msg,
Expand Down
13 changes: 13 additions & 0 deletions core/src/ten_runtime/binding/go/interface/ten/ten_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,3 +690,16 @@ func (p *tenEnv) InitPropertyFromJSONBytes(value []byte) error {

return err
}

func (p *tenEnv) InitManifestFromJSONBytes(value []byte) error {
defer p.keepAlive()

apiStatus := C.ten_go_ten_env_init_manifest_from_json_bytes(
p.cPtr,
unsafe.Pointer(unsafe.SliceData(value)),
C.int(len(value)),
)
err := withGoStatus(&apiStatus)

return err
}
29 changes: 25 additions & 4 deletions core/src/ten_runtime/binding/go/native/extension/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
#include "ten_utils/lib/string.h"
#include "ten_utils/macro/check.h"

extern void tenGoExtensionOnConfigure(ten_go_handle_t go_extension,
ten_go_handle_t go_ten);

extern void tenGoExtensionOnInit(ten_go_handle_t go_extension,
ten_go_handle_t go_ten);

Expand Down Expand Up @@ -96,7 +99,7 @@ static void ten_go_extension_bridge_destroy(ten_go_extension_t *self) {
TEN_FREE(self);
}

static void proxy_on_init(ten_extension_t *self, ten_env_t *ten_env) {
static void proxy_on_configure(ten_extension_t *self, ten_env_t *ten_env) {
TEN_ASSERT(self && ten_extension_check_integrity(self, true),
"Should not happen.");
TEN_ASSERT(ten_env && ten_env_check_integrity(ten_env, true),
Expand All @@ -111,6 +114,24 @@ static void proxy_on_init(ten_extension_t *self, ten_env_t *ten_env) {
ten_go_ten_env_t *ten_bridge = ten_go_ten_env_wrap(ten_env);
ten_bridge->c_ten_env_proxy = ten_env_proxy_create(ten_env, 1, NULL);

tenGoExtensionOnConfigure(ten_go_extension_go_handle(extension_bridge),
ten_go_ten_env_go_handle(ten_bridge));
}

static void proxy_on_init(ten_extension_t *self, ten_env_t *ten_env) {
TEN_ASSERT(self && ten_extension_check_integrity(self, true),
"Should not happen.");
TEN_ASSERT(ten_env && ten_env_check_integrity(ten_env, true),
"Should not happen.");
TEN_ASSERT(ten_extension_get_ten(self) == ten_env, "Should not happen.");

ten_go_extension_t *extension_bridge =
ten_binding_handle_get_me_in_target_lang((ten_binding_handle_t *)self);
TEN_ASSERT(ten_go_extension_check_integrity(extension_bridge),
"Should not happen.");

ten_go_ten_env_t *ten_bridge = ten_go_ten_env_wrap(ten_env);

tenGoExtensionOnInit(ten_go_extension_go_handle(extension_bridge),
ten_go_ten_env_go_handle(ten_bridge));
}
Expand Down Expand Up @@ -285,9 +306,9 @@ ten_go_extension_t *ten_go_extension_create_internal(
extension_bridge->bridge.sp_ref_by_c = NULL;

extension_bridge->c_extension = ten_extension_create(
name, NULL, proxy_on_init, proxy_on_start, proxy_on_stop, proxy_on_deinit,
proxy_on_cmd, proxy_on_data, proxy_on_audio_frame, proxy_on_video_frame,
NULL);
name, proxy_on_configure, proxy_on_init, proxy_on_start, proxy_on_stop,
proxy_on_deinit, proxy_on_cmd, proxy_on_data, proxy_on_audio_frame,
proxy_on_video_frame, NULL);

ten_binding_handle_set_me_in_target_lang(
&extension_bridge->c_extension->binding_handle, extension_bridge);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "include_internal/ten_runtime/binding/go/internal/common.h"
#include "include_internal/ten_runtime/binding/go/ten_env/ten_env.h"
#include "include_internal/ten_runtime/binding/go/ten_env/ten_env_internal.h"
#include "ten_utils/macro/check.h"
#include "include_internal/ten_runtime/ten_env/metadata.h"
#include "ten_runtime/binding/go/interface/ten/common.h"
#include "ten_runtime/binding/go/interface/ten/ten_env.h"
#include "ten_runtime/common/errno.h"
Expand All @@ -20,6 +20,7 @@

typedef struct ten_notify_set_init_property_info_t {
ten_string_t value;
ten_error_t err;
ten_event_t *completed;
} ten_env_notify_init_property_info_t;

Expand All @@ -30,6 +31,7 @@ ten_env_notify_init_property_info_create(const void *value, int value_len) {
TEN_ASSERT(info, "Failed to allocate memory.");

ten_string_init_formatted(&info->value, "%.*s", value_len, value);
ten_error_init(&info->err);
info->completed = ten_event_create(0, 1);

return info;
Expand All @@ -40,6 +42,7 @@ static void ten_env_notify_init_property_info_destroy(
TEN_ASSERT(self, "Invalid argument.");

ten_string_deinit(&self->value);
ten_error_deinit(&self->err);
ten_event_destroy(self->completed);

TEN_FREE(self);
Expand All @@ -65,6 +68,21 @@ static void ten_env_notify_init_property_from_json(ten_env_t *ten_env,
ten_error_deinit(&err);
}

static void ten_env_notify_init_manifest_from_json(ten_env_t *ten_env,
void *user_data) {
TEN_ASSERT(user_data, "Invalid argument.");
TEN_ASSERT(ten_env && ten_env_check_integrity(ten_env, true),
"Should not happen.");

ten_env_notify_init_property_info_t *info = user_data;
TEN_ASSERT(info, "Should not happen.");

ten_env_init_manifest_from_json(ten_env, ten_string_get_raw_str(&info->value),
&info->err);

ten_event_set(info->completed);
}

ten_go_status_t ten_go_ten_env_init_property_from_json_bytes(
uintptr_t bridge_addr, const void *json_str, int json_str_len) {
ten_go_ten_env_t *self = ten_go_ten_env_reinterpret(bridge_addr);
Expand All @@ -79,24 +97,56 @@ ten_go_status_t ten_go_ten_env_init_property_from_json_bytes(
return status;
});

ten_error_t err;
ten_error_init(&err);
ten_env_notify_init_property_info_t *info =
ten_env_notify_init_property_info_create(json_str, json_str_len);
TEN_ASSERT(info, "Should not happen.");

if (!ten_env_proxy_notify(self->c_ten_env_proxy,
ten_env_notify_init_property_from_json, info, false,
&err)) {
ten_go_status_from_error(&status, &err);
&info->err)) {
goto done;
}

ten_event_wait(info->completed, -1);

done:
ten_go_status_from_error(&status, &info->err);
ten_env_notify_init_property_info_destroy(info);
TEN_GO_TEN_IS_ALIVE_REGION_END(self);

ten_is_close:
return status;
}

ten_go_status_t ten_go_ten_env_init_manifest_from_json_bytes(
uintptr_t bridge_addr, const void *json_str, int json_str_len) {
ten_go_ten_env_t *self = ten_go_ten_env_reinterpret(bridge_addr);
TEN_ASSERT(self && ten_go_ten_env_check_integrity(self),
"Should not happen.");

ten_go_status_t status;
ten_go_status_init_with_errno(&status, TEN_ERRNO_OK);

TEN_GO_TEN_IS_ALIVE_REGION_BEGIN(self, {
ten_go_status_init_with_errno(&status, TEN_ERRNO_TEN_IS_CLOSED);
return status;
});

ten_env_notify_init_property_info_t *info =
ten_env_notify_init_property_info_create(json_str, json_str_len);
TEN_ASSERT(info, "Should not happen.");

if (!ten_env_proxy_notify(self->c_ten_env_proxy,
ten_env_notify_init_manifest_from_json, info, false,
&info->err)) {
goto done;
}

ten_event_wait(info->completed, -1);

done:
ten_go_status_from_error(&status, &info->err);
ten_env_notify_init_property_info_destroy(info);
ten_error_deinit(&err);
TEN_GO_TEN_IS_ALIVE_REGION_END(self);

ten_is_close:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
//
#include "include_internal/ten_runtime/binding/go/ten_env/ten_env.h"
#include "include_internal/ten_runtime/binding/go/ten_env/ten_env_internal.h"
#include "ten_utils/macro/check.h"
#include "ten_runtime/binding/go/interface/ten/ten_env.h"
#include "ten_runtime/ten_env_proxy/ten_env_proxy.h"
#include "ten_utils/lib/alloc.h"
#include "ten_utils/macro/check.h"

typedef struct ten_env_notify_is_cmd_connected_info_t {
bool result;
Expand Down Expand Up @@ -53,7 +53,6 @@ static void ten_notify_is_cmd_connected(ten_env_t *ten_env, void *user_data) {

info->result = ten_env_is_cmd_connected(
ten_env, ten_string_get_raw_str(&info->name), &err);
TEN_ASSERT(info->result, "Should not happen.");

leoadonia marked this conversation as resolved.
Show resolved Hide resolved
ten_event_set(info->completed);

Expand Down Expand Up @@ -82,6 +81,7 @@ bool ten_go_ten_env_is_cmd_connected(uintptr_t bridge_addr, const char *name) {
}

ten_event_wait(info->completed, -1);
result = info->result;

done:
ten_error_deinit(&err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "include_internal/ten_runtime/binding/go/ten_env/ten_env.h"
#include "include_internal/ten_runtime/binding/go/ten_env/ten_env_internal.h"
#include "include_internal/ten_runtime/binding/go/value/value.h"
#include "ten_utils/macro/check.h"
#include "ten_runtime/binding/go/interface/ten/common.h"
#include "ten_runtime/binding/go/interface/ten/ten_env.h"
#include "ten_runtime/binding/go/interface/ten/value.h"
Expand All @@ -22,10 +21,12 @@
#include "ten_utils/lib/event.h"
#include "ten_utils/lib/json.h"
#include "ten_utils/lib/string.h"
#include "ten_utils/macro/check.h"
#include "ten_utils/value/value.h"

typedef struct ten_env_notify_set_property_info_t {
bool result;
ten_error_t err;
ten_string_t path;
ten_value_t *c_value;
ten_event_t *completed;
Expand All @@ -39,6 +40,7 @@ ten_env_notify_set_property_info_create(const void *path, int path_len,
TEN_ASSERT(info, "Failed to allocate memory.");

info->result = true;
ten_error_init(&info->err);
ten_string_init_formatted(&info->path, "%.*s", path_len, path);
info->c_value = value;
info->completed = ten_event_create(0, 1);
Expand All @@ -50,6 +52,7 @@ static void ten_env_notify_set_property_info_destroy(
ten_env_notify_set_property_info_t *self) {
TEN_ASSERT(self, "Invalid argument.");

ten_error_deinit(&self->err);
ten_string_deinit(&self->path);
self->c_value = NULL;
ten_event_destroy(self->completed);
Expand All @@ -65,16 +68,10 @@ static void ten_env_notify_set_property(ten_env_t *ten_env, void *user_data) {
ten_env_notify_set_property_info_t *info = user_data;
TEN_ASSERT(info, "Should not happen.");

ten_error_t err;
ten_error_init(&err);

info->result = ten_env_set_property(
ten_env, ten_string_get_raw_str(&info->path), info->c_value, &err);
TEN_ASSERT(info->result, "Should not happen.");
ten_env, ten_string_get_raw_str(&info->path), info->c_value, &info->err);

ten_event_set(info->completed);

ten_error_deinit(&err);
}

static void ten_go_ten_env_set_property(ten_go_ten_env_t *self,
Expand All @@ -90,25 +87,20 @@ static void ten_go_ten_env_set_property(ten_go_ten_env_t *self,
ten_go_status_set_errno(status, TEN_ERRNO_TEN_IS_CLOSED);
});

ten_error_t err;
ten_error_init(&err);

ten_env_notify_set_property_info_t *info =
ten_env_notify_set_property_info_create(path, path_len, value);

if (!ten_env_proxy_notify(self->c_ten_env_proxy, ten_env_notify_set_property,
info, false, &err)) {
ten_go_status_from_error(status, &err);
info, false, &info->err)) {
goto done;
}

ten_event_wait(info->completed, -1);

done:
ten_go_status_from_error(status, &info->err);
ten_env_notify_set_property_info_destroy(info);

ten_error_deinit(&err);

TEN_GO_TEN_IS_ALIVE_REGION_END(self);

ten_is_close:
Expand Down
Loading
Loading