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

GrpcWorkerAgentRuntimeHost fails to start due to "no running event loop" error #5124

Open
linznin opened this issue Jan 21, 2025 · 3 comments

Comments

@linznin
Copy link

linznin commented Jan 21, 2025

What happened?

While using Distributed Agent Runtime to start GrpcWorkerAgentRuntimeHost, an error occurs. Below are the steps to reproduce the issue and the code snippet.

Steps to Reproduce:

  1. Use the following code to start GrpcWorkerAgentRuntimeHost:
  def main():
      host = GrpcWorkerAgentRuntimeHost(address="0.0.0.0:50051")
      host.start()  # Start a host service in the background.
  
  if __name__ == "__main__":
  1. Run the script, and the following error appears:
   Traceback (most recent call last):
  File "d:\Users\10609302\Documents\workspace\ai-agent\ag_event_host.py", line 96, in <module>
    main()
  File "d:\Users\10609302\Documents\workspace\ai-agent\ag_event_host.py", line 93, in main
    host.start()  # Start a host service in the background.
    ^^^^^^^^^^^^
  File "C:\Users\10609302\AppData\Local\pypoetry\Cache\virtualenvs\ai-agent-avTRqWVV-py3.11\Lib\site-packages\autogen_ext\runtimes\grpc\_worker_runtime_host.py", line 37, in start
    self._serve_task = asyncio.create_task(self._serve())
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Program Files\Python311\Lib\asyncio\tasks.py", line 381, in create_task
    loop = events.get_running_loop()
           ^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: no running event loop
sys:1: RuntimeWarning: coroutine 'GrpcWorkerAgentRuntimeHost._serve' was never awaited

Expected Behavior:
GrpcWorkerAgentRuntimeHost should successfully start the background service without throwing a RuntimeError.

What did you expect to happen?

  1. Use the following code to start GrpcWorkerAgentRuntimeHost
  def main():
      host = GrpcWorkerAgentRuntimeHost(address="0.0.0.0:50051")
      host.start()  # Start a host service in the background.
  
  if __name__ == "__main__":
      main()

How can we reproduce it (as minimally and precisely as possible)?

The error message suggests that an asyncio event loop is not properly initialized, which causes asyncio.create_task to fail.

AutoGen version

0.4.2

Which package was this bug in

Extensions

Model used

No response

Python version

3.11.6

Operating system

Windows10

Any additional info you think would be helpful for fixing this bug

No response

@ekzhu
Copy link
Collaborator

ekzhu commented Jan 21, 2025

It's not a bug, you need to run this inside an async function.

  async def main():
      host = GrpcWorkerAgentRuntimeHost(address="0.0.0.0:50051")
      host.start()  # Start a host service in the background.
      await service.stop_when_signal()
  
  if __name__ == "__main__":
      asyncio.run(main())

See sample: https://github.com/microsoft/autogen/blob/main/python/samples/core_grpc_worker_runtime/run_host.py

@linznin
Copy link
Author

linznin commented Jan 22, 2025

I followed the sample to wrap the GrpcWorkerAgentRuntimeHost usage inside an async function as shown below

async def main() -> None:
    service = GrpcWorkerAgentRuntimeHost(address="localhost:50051")
    service.start()
    await service.stop_when_signal()

if __name__ == "__main__":
    asyncio.run(main())

However, I still encountered the following error when running the code:

Traceback (most recent call last):
  File "d:\Users\10609302\Documents\workspace\ai-agent\ag_event_host.py", line 121, in <module>
    asyncio.run(main())
  File "D:\Program Files\Python311\Lib\asyncio\runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "D:\Program Files\Python311\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Program Files\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "d:\Users\10609302\Documents\workspace\ai-agent\ag_event_host.py", line 104, in main
    await service.stop_when_signal()
  File "C:\Users\10609302\AppData\Local\pypoetry\Cache\virtualenvs\ai-agent-avTRqWVV-py3.11\Lib\site-packages\autogen_ext\runtimes\grpc\_worker_runtime_host.py", line 67, in stop_when_signal    
    loop.add_signal_handler(sig, signal_handler)
  File "D:\Program Files\Python311\Lib\asyncio\events.py", line 574, in add_signal_handler
    raise NotImplementedError
NotImplementedError
Exception ignored in: <function Server.__del__ at 0x000001F9C20BEE80>
Traceback (most recent call last):
  File "C:\Users\10609302\AppData\Local\pypoetry\Cache\virtualenvs\ai-agent-avTRqWVV-py3.11\Lib\site-packages\grpc\aio\_server.py", line 185, in __del__
  File "src\python\grpcio\grpc\_cython\_cygrpc/aio/common.pyx.pxi", line 120, in grpc._cython.cygrpc.schedule_coro_threadsafe
  File "src\python\grpcio\grpc\_cython\_cygrpc/aio/common.pyx.pxi", line 112, in grpc._cython.cygrpc.schedule_coro_threadsafe
  File "D:\Program Files\Python311\Lib\asyncio\base_events.py", line 434, in create_task
  File "D:\Program Files\Python311\Lib\asyncio\base_events.py", line 519, in _check_closed
RuntimeError: Event loop is closed
sys:1: RuntimeWarning: coroutine 'AioServer.shutdown' was never awaited

I found workaround by chatgpt:

async def main():
    host = GrpcWorkerAgentRuntimeHost(address="0.0.0.0:50051")
    host.start()  # Start a host service in the background.

    try:
        print("Service is running. Press Ctrl+C to stop.")
        # Wait indefinitely (or you can use another condition to stop the service).
        await asyncio.Event().wait()
    except KeyboardInterrupt:
        print("Stopping service...")
    finally:
        await host.stop()
        print("Service stopped.")

@ekzhu
Copy link
Collaborator

ekzhu commented Jan 22, 2025

Would you like to submit a PR to update the sample code. It's very helpful :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants