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

feat: print log with exception info in async extension and abort #276

Merged
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
# Refer to the "LICENSE" file in the root directory for more information.
#
import asyncio
import os
import threading
import traceback
from typing import final
from libten_runtime_python import _Extension
from .video_frame import VideoFrame
Expand All @@ -31,13 +33,13 @@ async def _thread_routine(self, ten_env: TenEnv):
ten_env, self._ten_loop, self._ten_thread
)

await self.on_configure(self._async_ten_env)
await self._wrapper_on_config(self._async_ten_env)
ten_env.on_configure_done()

# Suspend the thread until stopEvent is set.
await self._ten_stop_event.wait()

await self.on_deinit(self._async_ten_env)
await self._wrapper_on_deinit(self._async_ten_env)
self._async_ten_env._deinit()

async def _stop_thread(self):
Expand All @@ -64,7 +66,7 @@ def _proxy_on_init(self, ten_env: TenEnv) -> None:

@final
async def _proxy_async_on_init(self, ten_env: TenEnv):
await self.on_init(self._async_ten_env)
await self._wrapper_on_init(self._async_ten_env)
ten_env.on_init_done()

@final
Expand All @@ -75,7 +77,7 @@ def _proxy_on_start(self, ten_env: TenEnv) -> None:

@final
async def _proxy_async_on_start(self, ten_env: TenEnv):
await self.on_start(self._async_ten_env)
await self._wrapper_on_start(self._async_ten_env)
ten_env.on_start_done()

@final
Expand All @@ -86,7 +88,7 @@ def _proxy_on_stop(self, ten_env: TenEnv) -> None:

@final
async def _proxy_async_on_stop(self, ten_env: TenEnv):
await self.on_stop(self._async_ten_env)
await self._wrapper_on_stop(self._async_ten_env)
ten_env.on_stop_done()

@final
Expand All @@ -96,21 +98,21 @@ def _proxy_on_deinit(self, ten_env: TenEnv) -> None:
@final
def _proxy_on_cmd(self, ten_env: TenEnv, cmd: Cmd) -> None:
asyncio.run_coroutine_threadsafe(
self.on_cmd(self._async_ten_env, cmd), self._ten_loop
self._wrapper_on_cmd(self._async_ten_env, cmd), self._ten_loop
)

@final
def _proxy_on_data(self, ten_env: TenEnv, data: Data) -> None:
asyncio.run_coroutine_threadsafe(
self.on_data(self._async_ten_env, data), self._ten_loop
self._wrapper_on_data(self._async_ten_env, data), self._ten_loop
)

@final
def _proxy_on_video_frame(
self, ten_env: TenEnv, video_frame: VideoFrame
) -> None:
asyncio.run_coroutine_threadsafe(
self.on_video_frame(self._async_ten_env, video_frame),
self._wrapper_on_video_frame(self._async_ten_env, video_frame),
self._ten_loop,
)

Expand All @@ -119,10 +121,77 @@ def _proxy_on_audio_frame(
self, ten_env: TenEnv, audio_frame: AudioFrame
) -> None:
asyncio.run_coroutine_threadsafe(
self.on_audio_frame(self._async_ten_env, audio_frame),
self._wrapper_on_audio_frame(self._async_ten_env, audio_frame),
self._ten_loop,
)

# Wrapper methods for handling exceptions in User-defined methods

async def _wrapper_on_config(self, async_ten_env: AsyncTenEnv):
try:
await self.on_configure(async_ten_env)
except Exception as e:
_exit_on_exception(async_ten_env, e)

async def _wrapper_on_init(self, async_ten_env: AsyncTenEnv):
try:
await self.on_init(async_ten_env)
except Exception as e:
_exit_on_exception(async_ten_env, e)

async def _wrapper_on_start(self, async_ten_env: AsyncTenEnv):
try:
await self.on_start(async_ten_env)
except Exception as e:
_exit_on_exception(async_ten_env, e)

async def _wrapper_on_stop(self, async_ten_env: AsyncTenEnv):
try:
await self.on_stop(async_ten_env)
except Exception as e:
_exit_on_exception(async_ten_env, e)

async def _wrapper_on_deinit(self, async_ten_env: AsyncTenEnv):
try:
await self.on_deinit(async_ten_env)
except Exception as e:
_exit_on_exception(async_ten_env, e)

async def _wrapper_on_cmd(self, async_ten_env: AsyncTenEnv, cmd: Cmd):
try:
await self.on_cmd(async_ten_env, cmd)
except Exception as e:
_exit_on_exception(async_ten_env, e)

async def _wrapper_on_data(self, async_ten_env: AsyncTenEnv, data: Data):
try:
await self.on_data(async_ten_env, data)
except Exception as e:
_exit_on_exception(async_ten_env, e)

async def _wrapper_on_video_frame(
self, async_ten_env: AsyncTenEnv, video_frame: VideoFrame
):
try:
await self.on_video_frame(async_ten_env, video_frame)
except Exception as e:
_exit_on_exception(async_ten_env, e)

async def _wrapper_on_audio_frame(
self, async_ten_env: AsyncTenEnv, audio_frame: AudioFrame
):
try:
await self.on_audio_frame(async_ten_env, audio_frame)
except Exception as e:
_exit_on_exception(async_ten_env, e)

def _exit_on_exception(self, async_ten_env: AsyncTenEnv, e: Exception):
traceback_info = traceback.format_exc()
async_ten_env.log_fatal(
f"Uncaught exception: {e} \ntraceback: {traceback_info}"
)
os._exit(1)

# Override these methods in your extension

async def on_configure(self, async_ten_env: AsyncTenEnv) -> None:
Expand Down
Loading