[PR #22169] Fix potential leak #29861

Closed
opened 2026-02-21 20:46:23 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/22169

State: closed
Merged: Yes


Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

Fix #22168

This PR addresses a potential ThreadPool leakage issue in the base_session.py file within the MCP (Model Context Protocol) session management module. The fix ensures proper initialization and cleanup of thread pool executors and receiver futures to prevent resource leaks.

Key Changes:

  • Initialize _executor and _receiver_future attributes to None in the constructor for proper cleanup checks
  • Enhanced the __exit__ method to properly wait for receiver loop completion with a 5-second timeout
  • Added graceful shutdown of the ThreadPoolExecutor with timeout handling
  • Improved resource management to prevent potential memory leaks and hanging threads

Motivation:
Without proper cleanup of ThreadPool resources, the application could experience memory leaks and resource exhaustion over time, especially in long-running scenarios where multiple sessions are created and destroyed.

Screenshots

Before After
ThreadPool resources not properly cleaned up, potential memory leaks Proper initialization and cleanup of ThreadPool resources with timeout handling

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
**Original Pull Request:** https://github.com/langgenius/dify/pull/22169 **State:** closed **Merged:** Yes --- > [!IMPORTANT] > > 1. Make sure you have read our [contribution guidelines](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) > 2. Ensure there is an associated issue and you have been assigned to it > 3. Use the correct syntax to link this PR: `Fixes #<issue number>`. ## Summary Fix #22168 This PR addresses a potential ThreadPool leakage issue in the `base_session.py` file within the MCP (Model Context Protocol) session management module. The fix ensures proper initialization and cleanup of thread pool executors and receiver futures to prevent resource leaks. **Key Changes:** - Initialize `_executor` and `_receiver_future` attributes to `None` in the constructor for proper cleanup checks - Enhanced the `__exit__` method to properly wait for receiver loop completion with a 5-second timeout - Added graceful shutdown of the ThreadPoolExecutor with timeout handling - Improved resource management to prevent potential memory leaks and hanging threads **Motivation:** Without proper cleanup of ThreadPool resources, the application could experience memory leaks and resource exhaustion over time, especially in long-running scenarios where multiple sessions are created and destroyed. ## Screenshots | Before | After | |--------|-------| | ThreadPool resources not properly cleaned up, potential memory leaks | Proper initialization and cleanup of ThreadPool resources with timeout handling | ## Checklist - [ ] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!) - [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. - [x] I've updated the documentation accordingly. - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods
yindo added the pull-request label 2026-02-21 20:46:23 -05:00
yindo closed this issue 2026-02-21 20:46:23 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#29861