diff --git a/ably/decorator/sync.py b/ably/decorator/sync.py index ecaca933..543914b2 100644 --- a/ably/decorator/sync.py +++ b/ably/decorator/sync.py @@ -21,16 +21,19 @@ def wrapper(*args, **kwargs): pass ably_eventloop: events = AblyEventLoop.current().loop - # Handle calls from ably_eventloop on the same loop, return awaitable - if caller_eventloop is not None and caller_eventloop == ably_eventloop: - return ably_eventloop.create_task(fn(*args, **kwargs)) - - # Handle calls from external eventloop, post them on ably_eventloop - future = asyncio.run_coroutine_threadsafe(fn(*args, **kwargs), ably_eventloop) - if caller_eventloop is not None and caller_eventloop.is_running(): - return asyncio.wrap_future(future) - - # If called from regular function, return blocking result - return future.result() - - return wrapper \ No newline at end of file + res = fn(*args, **kwargs) + if asyncio.iscoroutine(res): + # Handle calls from ably_eventloop on the same loop, return awaitable + if caller_eventloop is not None and caller_eventloop == ably_eventloop: + return ably_eventloop.create_task(res) + + # Handle calls from external eventloop, post them on ably_eventloop + future = asyncio.run_coroutine_threadsafe(res, ably_eventloop) + if caller_eventloop is not None and caller_eventloop.is_running(): + return asyncio.wrap_future(future) + + # If called from regular function, return blocking result + return future.result() + return res + + return wrapper