https://prefect.io logo
Title
m

Marcos

08/08/2022, 10:35 PM
Hi! I think I found a bug when mixing sync and async tasks in an async flow, for example, when running the next example:
import asyncio

from prefect import task, get_run_logger, flow


@task
async def task1():
    await asyncio.sleep(1)


@task
def task2():
    logger = get_run_logger()
    <http://logger.info|logger.info>("hi")


@flow
async def test_flow():
    await task1.submit()
    task2.submit()
The following exception is raised:
Encountered exception during execution:
Traceback (most recent call last):
  File "/opt/conda/lib/python3.10/site-packages/prefect/engine.py", line 550, in orchestrate_flow_run
    result = await flow_call()
  File "/app/src/streamcast/agent/flows/test.py", line 22, in test_flow
    task2.submit()
  File "/opt/conda/lib/python3.10/site-packages/prefect/tasks.py", line 491, in submit
    return enter_task_run_engine(
  File "/opt/conda/lib/python3.10/site-packages/prefect/engine.py", line 691, in enter_task_run_engine
    return flow_run_context.sync_portal.call(begin_run)
  File "/opt/conda/lib/python3.10/site-packages/anyio/from_thread.py", line 283, in call
    return cast(T_Retval, self.start_task_soon(func, *args).result())
  File "/opt/conda/lib/python3.10/concurrent/futures/_base.py", line 439, in result
    return self.__get_result()
  File "/opt/conda/lib/python3.10/concurrent/futures/_base.py", line 391, in __get_result
    raise self._exception
  File "/opt/conda/lib/python3.10/site-packages/anyio/from_thread.py", line 219, in _call_func
    retval = await retval
  File "/opt/conda/lib/python3.10/site-packages/prefect/engine.py", line 783, in create_task_run_then_submit
    task_run = await create_task_run(
  File "/opt/conda/lib/python3.10/site-packages/prefect/engine.py", line 823, in create_task_run
    task_run = await flow_run_context.client.create_task_run(
  File "/opt/conda/lib/python3.10/site-packages/prefect/client.py", line 1635, in create_task_run
    response = await <http://self._client.post|self._client.post>(
  File "/opt/conda/lib/python3.10/site-packages/httpx/_client.py", line 1842, in post
    return await self.request(
  File "/opt/conda/lib/python3.10/site-packages/httpx/_client.py", line 1527, in request
    return await self.send(request, auth=auth, follow_redirects=follow_redirects)
  File "/opt/conda/lib/python3.10/site-packages/prefect/client.py", line 258, in send
    await super().send(*args, **kwargs)
  File "/opt/conda/lib/python3.10/site-packages/httpx/_client.py", line 1614, in send
    response = await self._send_handling_auth(
  File "/opt/conda/lib/python3.10/site-packages/httpx/_client.py", line 1642, in _send_handling_auth
    response = await self._send_handling_redirects(
  File "/opt/conda/lib/python3.10/site-packages/httpx/_client.py", line 1679, in _send_handling_redirects
    response = await self._send_single_request(request)
  File "/opt/conda/lib/python3.10/site-packages/httpx/_client.py", line 1716, in _send_single_request
    response = await transport.handle_async_request(request)
  File "/opt/conda/lib/python3.10/site-packages/httpx/_transports/default.py", line 353, in handle_async_request
    resp = await self._pool.handle_async_request(req)
  File "/opt/conda/lib/python3.10/site-packages/httpcore/_async/connection_pool.py", line 253, in handle_async_request
    raise exc
  File "/opt/conda/lib/python3.10/site-packages/httpcore/_async/connection_pool.py", line 237, in handle_async_request
    response = await connection.handle_async_request(request)
  File "/opt/conda/lib/python3.10/site-packages/httpcore/_async/connection.py", line 90, in handle_async_request
    return await self._connection.handle_async_request(request)
  File "/opt/conda/lib/python3.10/site-packages/httpcore/_async/http11.py", line 105, in handle_async_request
    raise exc
  File "/opt/conda/lib/python3.10/site-packages/httpcore/_async/http11.py", line 84, in handle_async_request
    ) = await self._receive_response_headers(**kwargs)
  File "/opt/conda/lib/python3.10/site-packages/httpcore/_async/http11.py", line 148, in _receive_response_headers
    event = await self._receive_event(timeout=timeout)
  File "/opt/conda/lib/python3.10/site-packages/httpcore/_async/http11.py", line 177, in _receive_event
    data = await self._network_stream.read(
  File "/opt/conda/lib/python3.10/site-packages/httpcore/backends/asyncio.py", line 33, in read
    return await self._stream.receive(max_bytes=max_bytes)
  File "/opt/conda/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 1265, in receive
    await self._protocol.read_event.wait()
  File "/opt/conda/lib/python3.10/asyncio/locks.py", line 211, in wait
    fut = self._get_loop().create_future()
  File "/opt/conda/lib/python3.10/asyncio/mixins.py", line 30, in _get_loop
    raise RuntimeError(f'{self!r} is bound to a different event loop')
RuntimeError: <asyncio.locks.Event object at 0x7fd237ee4310 [unset]> is bound to a different event loop
j

Jeff Hale

08/09/2022, 1:42 AM
Hi Marcos. Hmm. Looks like it might be a bug. Are you able to open an issue?
m

Marcos

08/09/2022, 2:13 AM
b

Brad

08/15/2022, 10:30 AM
+1 also hitting this