Skip to content

Commit 210b85a

Browse files
Sobes76rusAntondanfimov
authored
fix: sync decorators (#343)
Co-authored-by: Anton <myprojectorterrypratchett@gmail.com> Co-authored-by: Dmitrii Anfimov <anfimov@tochka.com>
1 parent fc33b02 commit 210b85a

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

taskiq/receiver/receiver.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,18 @@ async def run_task( # noqa: C901, PLR0912, PLR0915
251251
if timeout is not None:
252252
if not is_coroutine:
253253
logger.warning("Timeouts for sync tasks don't work in python well.")
254-
target_future = asyncio.wait_for(target_future, float(timeout))
255-
returned = await target_future
254+
255+
with anyio.fail_after(float(timeout)):
256+
target_future = await target_future
257+
if inspect.isawaitable(target_future):
258+
target_future = await target_future
259+
260+
else:
261+
target_future = await target_future
262+
if inspect.isawaitable(target_future):
263+
target_future = await target_future
264+
265+
returned = target_future
256266
except NoResultError as no_res_exc:
257267
found_exception = no_res_exc
258268
logger.warning(

tests/receiver/test_receiver.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import time
55
from collections.abc import Generator
66
from concurrent.futures import ThreadPoolExecutor
7+
from functools import wraps
78
from typing import Any, ClassVar
89

910
import pytest
@@ -516,3 +517,30 @@ async def test_func() -> int:
516517
),
517518
)
518519
assert result.return_value == EXPECTED_CTX_VALUE
520+
521+
522+
async def test_sync_decorator_on_async_function() -> None:
523+
broker = InMemoryBroker()
524+
wrapper_call = False
525+
526+
def wrapper(f: Any) -> Any:
527+
@wraps(f)
528+
def wrapper_impl(*args: Any, **kwargs: Any) -> Any:
529+
nonlocal wrapper_call
530+
531+
wrapper_call = True
532+
return f(*args, **kwargs)
533+
534+
return wrapper_impl
535+
536+
@broker.task
537+
@wrapper
538+
async def task_no_result() -> str:
539+
return "some value"
540+
541+
task = await task_no_result.kiq()
542+
resp = await task.wait_result(timeout=1)
543+
544+
assert resp.return_value == "some value"
545+
assert not broker._running_tasks
546+
assert wrapper_call is True

0 commit comments

Comments
 (0)