Skip to content

[asyncio.exceptions.CancelledError] error in string await asyncio.sleep(0.1) #5

Description

@kuzyara

code

import asyncio
from opencode_runtime import OpenCodeRuntime


async def main():
    async with OpenCodeRuntime() as r:
        session = await r.session()
        response = await session.ask("hi", model="local/custom_openai//models/DeepSeek-V4.1-Flash")
        print(response.text)


# 3. Run the event loop
if __name__ == "__main__":
    asyncio.run(main())

always an error in __exit__() from OpenCodeRuntime()

  File "E:\git\python\langchain\.venv\Lib\site-packages\opencode_runtime\server.py", line 342, in stop
    await asyncio.sleep(0.1)
  File "C:\Users\Kuznetsov_NA\AppData\Local\Programs\Python\Python314\Lib\asyncio\tasks.py", line 702, in sleep
    return await future
           ^^^^^^^^^^^^
asyncio.exceptions.CancelledError

stacktrace

Traceback (most recent call last):
  File "C:\Users\Kuznetsov_NA\AppData\Local\Programs\Python\Python314\Lib\asyncio\runners.py", line 127, in run
    return self._loop.run_until_complete(task)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "C:\Users\Kuznetsov_NA\AppData\Local\Programs\Python\Python314\Lib\asyncio\base_events.py", line 719, in run_until_complete
    return future.result()
           ~~~~~~~~~~~~~^^
  File "E:\git\python\langchain\opencode.py", line 6, in main
    async with OpenCodeRuntime() as r:
               ~~~~~~~~~~~~~~~^^
  File "E:\git\python\langchain\.venv\Lib\site-packages\opencode_runtime\runtime.py", line 61, in __aexit__
    await self.close()
  File "E:\git\python\langchain\.venv\Lib\site-packages\opencode_runtime\runtime.py", line 72, in close
    await self._server_manager.stop_owned()
  File "E:\git\python\langchain\.venv\Lib\site-packages\opencode_runtime\server.py", line 361, in stop_owned
    await self.stop(key)
  File "E:\git\python\langchain\.venv\Lib\site-packages\opencode_runtime\server.py", line 342, in stop
    await asyncio.sleep(0.1)
  File "C:\Users\Kuznetsov_NA\AppData\Local\Programs\Python\Python314\Lib\asyncio\tasks.py", line 702, in sleep
    return await future
           ^^^^^^^^^^^^
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:\git\python\langchain\opencode.py", line 15, in <module>
    asyncio.run(main())
    ~~~~~~~~~~~^^^^^^^^
  File "C:\Users\Kuznetsov_NA\AppData\Local\Programs\Python\Python314\Lib\asyncio\runners.py", line 204, in run
    return runner.run(main)
           ~~~~~~~~~~^^^^^^
  File "C:\Users\Kuznetsov_NA\AppData\Local\Programs\Python\Python314\Lib\asyncio\runners.py", line 132, in run
    raise KeyboardInterrupt()
KeyboardInterrupt

Process finished with exit code -1073741510 (0xC000013A: interrupted by Ctrl+C)

i dont pressed ctrl+c !

how to fix

The library code at server.py:342 should shield the sleep from cancellation:

# server.py - inside stop()
try:
    await asyncio.sleep(0.1)
except asyncio.CancelledError:
    pass  # ignore cancellation during shutdown sleep

If you maintain this library, make stop() cancellation-safe so __aexit__ can complete even when Ctrl+C is pressed. This is a known pattern — any blocking await inside an __aexit__ / finalizer should tolerate CancelledError.

im noob in asyncio, this is worked llm fix, maybe it can be done better.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions