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

feat: add request cancellation and cleanup #167

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

dsp-ant
Copy link
Member

@dsp-ant dsp-ant commented Jan 23, 2025

Summary

  • Add support for request cancellation via notifications
  • Implement periodic cleanup of in-flight requests
  • Track request state throughout lifecycle
  • Fixes Random error thrown on response #88

Test plan

  • Test request cancellation
  • Verify cleanup of completed requests
  • Test concurrent request handling

Depends on #166

@dsp-ant dsp-ant linked an issue Jan 23, 2025 that may be closed by this pull request
Base automatically changed from davidsp/refactor to main January 24, 2025 09:54
Comment on lines +228 to +238
async def _cleanup_loop(self) -> None:
"""Periodically clean up completed and cancelled requests."""
while True:
with anyio.move_on_after(self._cleanup_interval):
# Clean up completed requests
self._in_flight = {
req_id: responder
for req_id, responder in self._in_flight.items()
if responder.in_flight
}
await anyio.sleep(self._cleanup_interval)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this done on completion or cancellation instead? The idea of using a timer for this seems weird to me, and leaves a window (the length of the interval) of possibly unbounded memory growth.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +249 to +254
# Clean up completed requests
self._in_flight = {
req_id: responder
for req_id, responder in self._in_flight.items()
if responder.in_flight
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OTOH, this seems excessive to do on every new incoming message. Why can't we tie it to completion and cancellation?

@EduPonz
Copy link

EduPonz commented Jan 29, 2025

This doesn't seem to solve #88 for me. I can reproduce triggering a call tool from claude with a call tool handle that simply waits for 200 s and returns a valid message. Claude receives the response but the server crashes

@server.call_tool()
async def handle_call_tool(
    name: str, arguments: dict | None,
) -> list[types.TextContent | types.ImageContent | types.EmbeddedResource]:
    await asyncio.sleep(200)
    response = {"msg": "success"}
    return [types.TextContent(type="text", text=f"{response}")]

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

Successfully merging this pull request may close these issues.

Random error thrown on response
3 participants