Fix resource leak: add proper cleanup for engine lifecycle#14
Open
hobostay wants to merge 1 commit intofikrikarim:mainfrom
Open
Fix resource leak: add proper cleanup for engine lifecycle#14hobostay wants to merge 1 commit intofikrikarim:mainfrom
hobostay wants to merge 1 commit intofikrikarim:mainfrom
Conversation
The engine.__enter__() was called during initialization but the corresponding engine.__exit__() was never called, leading to resource leaks. Changes: 1. Add engine.__exit__() call in lifespan shutdown handler 2. Add cleanup in load_models() if TTS loading fails after engine initialization This ensures GPU resources are properly released when: - The application shuts down gracefully - TTS backend loading fails during initialization Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes a resource leak where the Gemma 4 E2B engine's
__enter__()was called during initialization but the corresponding__exit__()was never called, leading to GPU resources not being properly released.Root Cause
In
src/server.py, theload_models()function:engine.__enter__()to acquire resourcesHowever, the engine's
__exit__()was never called in two scenarios:Changes
Added cleanup in lifespan shutdown handler: Calls
engine.__exit__()when the FastAPI application shuts down, ensuring GPU resources are released gracefully.Added cleanup during initialization: If TTS loading fails after the engine has been initialized, the engine is properly cleaned up before propagating the exception.
Impact
🤖 Generated with Claude Code