Skip to content

KV Cache Memory Leakage in Prefill Node During Context Transfer Failures #5820

Closed

Description

@Nekofish-L

Environment:
TensorRT-LLM version: v1.0.0rc1
Hardware: H20-96G and IB network

Description:
I conducted experiments with PD deployment using DeepSeek-R1, and observed that the prefill node occasionally hangs during stress testing.

After preliminary investigation, I found that:

  • The prefill node fails to release KV cache space allocated for failed requests
  • This leads to request accumulation in the prefill node
  • Both failed requests and normal in-transit requests are tracked in PyExecutor.ctx_in_transmission_requests
  • The hanging occurs probabilistically during synchronous status checks of these requests via self.kv_cache_transceiver.check_context_transfer_status(1)

Expected Behavior:
The system should properly release KV cache resources for failed requests and maintain stable operation during context transfer status checks.

Reproduction Steps:

  • Configure PD deployment
  • Stress test the service with high traffic (unconstrained concurrency) until:
    • The system exceeds its maximum processing capacity.
    • Requests start queuing due to overload.
  • Abruptly terminate the stress test script (simulate multiple request failures to rapidly exhaust the KV cache in the prefill node).
  • Observe behavior:
    • The prefill node continues processing some remaining requests.
    • Eventually, it hangs and stops accepting new requests.

Activity

  1. Nekofish-L commented on Jul 8, 2025

    @Nekofish-L
    ContributorAuthor

    Additional Evidence from Log Analysis:
    Early requests (e.g., ID 1467) remain in ctx_in_transmission_requests despite subsequent requests (higher IDs) being processed. (both request_id(first list in log) and client_id(second list in log) follow temporal increment patterns)
    Image

  2. Nekofish-L commented on Jul 8, 2025

    @Nekofish-L
    ContributorAuthor

    Additional:
    In the trtllm-serve disaggregated, when a request fails after prefill completion but before decoding, the error is not properly propagated back to the prefill node, causing KV cache leakage. Then prefill node will hang forever during kv_cache_transceiver.check_context_transfer_status(1).

    Here is the error log in trtllm-serve disaggregated:

    ERROR:    Exception in ASGI application
      + Exception Group Traceback (most recent call last):
      |   File "/usr/local/lib/python3.12/dist-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi
      |     result = await app(  # type: ignore[func-returns-value]
      |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      |   File "/usr/local/lib/python3.12/dist-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
      |     return await self.app(scope, receive, send)
      |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      |   File "/usr/local/lib/python3.12/dist-packages/fastapi/applications.py", line 1054, in __call__
      |     await super().__call__(scope, receive, send)
      |   File "/usr/local/lib/python3.12/dist-packages/starlette/applications.py", line 113, in __call__
      |     await self.middleware_stack(scope, receive, send)
      |   File "/usr/local/lib/python3.12/dist-packages/starlette/middleware/errors.py", line 187, in __call__
      |     raise exc
      |   File "/usr/local/lib/python3.12/dist-packages/starlette/middleware/errors.py", line 165, in __call__
      |     await self.app(scope, receive, _send)
      |   File "/usr/local/lib/python3.12/dist-packages/starlette/middleware/exceptions.py", line 62, in __call__
      |     await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
      |   File "/usr/local/lib/python3.12/dist-packages/starlette/_exception_handler.py", line 53, in wrapped_app
      |     raise exc
      |   File "/usr/local/lib/python3.12/dist-packages/starlette/_exception_handler.py", line 42, in wrapped_app
      |     await app(scope, receive, sender)
      |   File "/usr/local/lib/python3.12/dist-packages/starlette/routing.py", line 715, in __call__
      |     await self.middleware_stack(scope, receive, send)
      |   File "/usr/local/lib/python3.12/dist-packages/starlette/routing.py", line 735, in app
      |     await route.handle(scope, receive, send)
      |   File "/usr/local/lib/python3.12/dist-packages/starlette/routing.py", line 288, in handle
      |     await self.app(scope, receive, send)
      |   File "/usr/local/lib/python3.12/dist-packages/starlette/routing.py", line 76, in app
      |     await wrap_app_handling_exceptions(app, request)(scope, receive, send)
      |   File "/usr/local/lib/python3.12/dist-packages/starlette/_exception_handler.py", line 53, in wrapped_app
      |     raise exc
      |   File "/usr/local/lib/python3.12/dist-packages/starlette/_exception_handler.py", line 42, in wrapped_app
      |     await app(scope, receive, sender)
      |   File "/usr/local/lib/python3.12/dist-packages/starlette/routing.py", line 74, in app
      |     await response(scope, receive, send)
      |   File "/usr/local/lib/python3.12/dist-packages/starlette/responses.py", line 252, in __call__
      |     async with anyio.create_task_group() as task_group:
      |   File "/usr/local/lib/python3.12/dist-packages/anyio/_backends/_asyncio.py", line 772, in __aexit__
      |     raise BaseExceptionGroup(
      | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
      +-+---------------- 1 ----------------
        | Traceback (most recent call last):
        |   File "/usr/lib/python3.12/asyncio/selector_events.py", line 1013, in _read_ready__data_received
        |     data = self._sock.recv(self.max_size)
        |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        | ConnectionResetError: [Errno 104] Connection reset by peer
        | 
        | The above exception was the direct cause of the following exception:
        | 
        | Traceback (most recent call last):
        |   File "/usr/local/lib/python3.12/dist-packages/starlette/responses.py", line 255, in wrap
        |     await func()
        |   File "/usr/local/lib/python3.12/dist-packages/starlette/responses.py", line 244, in stream_response
        |     async for chunk in self.body_iterator:
        |   File "/usr/local/lib/python3.12/dist-packages/tensorrt_llm/serve/openai_disagg_server.py", line 143, in merge_streaming_responses
        |     async for chunk in gen_response.body_iterator:
        |   File "/usr/local/lib/python3.12/dist-packages/tensorrt_llm/serve/openai_disagg_server.py", line 315, in create_completion_generator
        |     async for chunk in self.create_generator(url, request, "/v1/completions"):
        |   File "/usr/local/lib/python3.12/dist-packages/tensorrt_llm/serve/openai_disagg_server.py", line 299, in create_generator
        |     async with self.session.post(url + end_point, json=request.model_dump(exclude_unset=True)) as response:
        |   File "/usr/local/lib/python3.12/dist-packages/aiohttp/client.py", line 1425, in __aenter__
        |     self._resp: _RetType = await self._coro
        |                            ^^^^^^^^^^^^^^^^
        |   File "/usr/local/lib/python3.12/dist-packages/aiohttp/client.py", line 730, in _request
        |     await resp.start(conn)
        |   File "/usr/local/lib/python3.12/dist-packages/aiohttp/client_reqrep.py", line 1059, in start
        |     message, payload = await protocol.read()  # type: ignore[union-attr]
        |                        ^^^^^^^^^^^^^^^^^^^^^
        |   File "/usr/local/lib/python3.12/dist-packages/aiohttp/streams.py", line 672, in read
        |     await self._waiter
        | aiohttp.client_exceptions.ClientOSError: [Errno 104] Connection reset by peer
        +------------------------------------
    
  3. SimengLiu-nv commented on Jul 8, 2025

    @SimengLiu-nv
    Collaborator

    @pcastonguay Would you take a look at this issue and tag related persons? Thanks!

  4. Nekofish-L commented on Jul 9, 2025

    @Nekofish-L
    ContributorAuthor

    PR #5815 enhanced the stability of trtllm-serve disaggregated during high-throughput stress tests, significantly reducing connection-related interruptions that caused this issue.

    However, when client connections are interrupted, requests that have been completed prefilled but not yet transferred to decode nodes still accumulate in prefill nodes. This accumulation can lead to system hang during synchronous status checks.

  5. SimengLiu-nv commented on Jul 9, 2025

    @SimengLiu-nv
    Collaborator

    CC @Tabrizian for this issue.

  6. RobertLou commented on Aug 14, 2025

    @RobertLou

    Have plan to fix this?

  7. troycheng commented on Aug 14, 2025

    @troycheng

    Have plan to fix this?

    We(with Nekofish-L) finally using Dynamo + trtllm instead

  8. troycheng commented on Nov 7, 2025

    @troycheng

    For reference, this PR (#8114) closes the issue.

  9. amos-llm commented on Nov 13, 2025

    @amos-llm

    @pcastonguay Could you please take a look? The memory leak still exists in v1.2.0rc2.
    The unit test in #8114 is too simple to expose the issue.
    When I run a stress test and then stop it, the prefill node hangs and cannot process new requests.

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

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions