Skip to content

Fix resource leak: add proper cleanup for engine lifecycle#14

Open
hobostay wants to merge 1 commit intofikrikarim:mainfrom
hobostay:fix/engine-resource-leak
Open

Fix resource leak: add proper cleanup for engine lifecycle#14
hobostay wants to merge 1 commit intofikrikarim:mainfrom
hobostay:fix/engine-resource-leak

Conversation

@hobostay
Copy link
Copy Markdown

@hobostay hobostay commented Apr 8, 2026

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, the load_models() function:

  1. Creates the engine and calls engine.__enter__() to acquire resources
  2. Loads the TTS backend

However, the engine's __exit__() was never called in two scenarios:

  1. When the application shuts down
  2. If TTS loading fails after engine initialization

Changes

  1. Added cleanup in lifespan shutdown handler: Calls engine.__exit__() when the FastAPI application shuts down, ensuring GPU resources are released gracefully.

  2. 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

  • Prevents GPU memory leaks on application shutdown
  • Ensures proper cleanup if TTS backend initialization fails
  • Makes the resource management more robust

🤖 Generated with Claude Code

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>
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.

1 participant