TensorRT-LLM icon indicating copy to clipboard operation
TensorRT-LLM copied to clipboard

TemporaryCudaStream leaks its borrowed stream when setup or exit fails

Open StephenLReed opened this issue 3 months ago • 0 comments

System Info

  • TensorRT-LLM: 1.3.0rc23 (built from f20ea652); behaviour re-checked against main @ fbda11f5
  • GPU: NVIDIA GeForce RTX 5090 (sm_120), driver 595.71.05
  • PyTorch 2.11.0+cu130, CUDA 13.0, Python 3.12.13
  • Ubuntu 25.10, kernel 6.17.0-41-generic
  • Backend: PyTorch; single GPU, no TP/PP
  • Model where observed: Qwen3.6-35B-A3B-NVFP4 with FP8 KV cache (where model-specific)

Who can help?

No response

Reproduction

TemporaryCudaStream / ItemHolderBase can leak a borrowed CUDA stream back-to-pool on two paths:

  1. Partial construction. ItemHolderBase.__init__ acquires from the pool before self._item is established, and TemporaryCudaStream.__init__ can raise in wait_events after super().__init__ has taken the stream — in both cases the stream is never returned.
  2. Exit. __exit__ records its finish event before returning the stream, so a failure in record_event() skips the return entirely.

The leak recurs on every retry under the memory pressure that typically triggers it.

Expected behavior

A borrowed CUDA stream is returned to its pool whether setup and exit succeed or fail.

actual behavior

A raise during construction after acquisition, or a failure in record_event() during exit, skips the return entirely and leaks the stream.

additional notes

Establish self._item = None before acquiring, wrap the post-acquire setup so a failure closes and returns the stream, and make __exit__ return the stream in a finally so it happens whether or not the event was recorded.

Five CPU-only tests (the pool is faked, so no GPU is needed): they fail against the exception-safety baseline and pass with the change.

This stacks on the SimplePool exception-safety fix, which we are raising separately. Happy to submit them as one two-commit PR if you prefer.

Per CONTRIBUTING.md this Issue Request precedes the patch, which is ready and applies cleanly to main @ fbda11f5.

StephenLReed avatar Aug 18 '26 16:08 StephenLReed