This is an automated email from the ASF dual-hosted git repository.

maximebeauchemin pushed a commit to branch thorough_mem_leak
in repository https://gitbox.apache.org/repos/asf/superset.git

commit 8f5e1188633390d2f9d310ca715f27f43448967b
Author: Maxime Beauchemin <[email protected]>
AuthorDate: Mon Sep 8 10:40:32 2025 -0700

    feat: add optional garbage collection after requests
    
    Adds FORCE_GARBAGE_COLLECTION_AFTER_EVERY_REQUEST feature flag
    and corresponding Flask after_request hook to force garbage
    collection after each request when enabled.
    
    🤖 Generated with [Claude Code](https://claude.ai/code)
    
    Co-Authored-By: Claude <[email protected]>
---
 superset/config.py                  |  2 ++
 superset/initialization/__init__.py | 11 +++++++++++
 2 files changed, 13 insertions(+)

diff --git a/superset/config.py b/superset/config.py
index b41992fe1e..7214eaa21a 100644
--- a/superset/config.py
+++ b/superset/config.py
@@ -626,6 +626,8 @@ DEFAULT_FEATURE_FLAGS: dict[str, bool] = {
     "DATE_RANGE_TIMESHIFTS_ENABLED": False,
     # Enable Matrixify feature for matrix-style chart layouts
     "MATRIXIFY": False,
+    # Force garbage collection after every request
+    "FORCE_GARBAGE_COLLECTION_AFTER_EVERY_REQUEST": False,
 }
 
 # ------------------------------
diff --git a/superset/initialization/__init__.py 
b/superset/initialization/__init__.py
index cdecd96b92..142f6fcf1b 100644
--- a/superset/initialization/__init__.py
+++ b/superset/initialization/__init__.py
@@ -638,6 +638,17 @@ class SupersetAppInitializer:  # pylint: 
disable=too-many-public-methods
                     response.headers[k] = v
             return response
 
+        @self.superset_app.after_request
+        def cleanup_analytics_memory(response: Response) -> Response:
+            """Force garbage collection after each request if feature flag 
enabled"""
+            if feature_flag_manager.is_feature_enabled(
+                "FORCE_GARBAGE_COLLECTION_AFTER_EVERY_REQUEST"
+            ):
+                import gc
+
+                gc.collect()
+            return response
+
         @self.superset_app.context_processor
         def get_common_bootstrap_data() -> dict[str, Any]:
             # Import here to avoid circular imports

Reply via email to