Skip to content

Commit

Permalink
fix: coredump when cmd has not connected (#29)
Browse files Browse the repository at this point in the history
* fix: coredump when cmd has not connected

* fix: return value

* feat: add configure api

* fix: remove unexported api

---------

Co-authored-by: Hu Yueh-Wei <[email protected]>
  • Loading branch information
leoadonia and halajohn authored Sep 20, 2024
1 parent 0885c49 commit aacfb6c
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 29 deletions.
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
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,6 @@
#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 "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 +19,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 +30,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 +41,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 Down Expand Up @@ -79,24 +81,21 @@ 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_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.");

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
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ func (p *extensionB) OnCmd(
go func() {
fmt.Println("extensionB OnCmd")

connected, err := tenEnv.IsCmdConnected("cmd_not_exist")
if err != nil || connected {
panic("Should not happen.")
}

cmdName, _ := cmd.GetName()
if cmdName == "B" {
var count uint32 = 0
Expand Down

0 comments on commit aacfb6c

Please sign in to comment.