turtle170 commited on
Commit
695cb69
·
verified ·
1 Parent(s): 65c2c95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -44
app.py CHANGED
@@ -255,7 +255,7 @@ class BackendProcessor:
255
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - ZEROENGINE - %(message)s', force=True)
256
  logger = logging.getLogger(__name__)
257
 
258
- # Suppress asyncio warnings during shutdown (multiple methods)
259
  import warnings
260
  warnings.filterwarnings("ignore", category=RuntimeWarning, message=".*asyncio.*")
261
  warnings.filterwarnings("ignore", category=UserWarning, message=".*asyncio.*")
@@ -263,9 +263,22 @@ warnings.filterwarnings("ignore", category=UserWarning, message=".*asyncio.*")
263
  # Set environment variable to suppress asyncio warnings
264
  os.environ['PYTHONASYNCIODEBUG'] = '0'
265
 
266
- # Suppress asyncio event loop warnings
267
  import asyncio
268
- asyncio.get_event_loop = lambda: None # Prevent event loop access during shutdown
 
 
 
 
 
 
 
 
 
 
 
 
 
269
 
270
  # --- KERNEL INITIALIZATION ---
271
  try:
@@ -1900,51 +1913,20 @@ if __name__ == "__main__":
1900
  except Exception as e:
1901
  logger.error(f"[CLEANUP] Model cleanup error: {e}")
1902
 
1903
- # Final garbage collection with asyncio cleanup
1904
  logger.info("[CLEANUP] Performing final cleanup...")
1905
  try:
1906
- # More aggressive asyncio cleanup
1907
  import asyncio
1908
- import sys
1909
-
1910
- # Force close all event loops
1911
  try:
1912
- # Get all running loops and close them
1913
- if hasattr(asyncio, 'all_tasks'):
1914
- tasks = asyncio.all_tasks()
1915
- logger.info(f"[CLEANUP] Found {len(tasks)} asyncio tasks")
1916
-
1917
- # Cancel all tasks
1918
- for task in tasks:
1919
- if not task.done():
1920
- task.cancel()
1921
-
1922
- # Wait for tasks to complete
1923
- if tasks:
1924
- asyncio.gather(*tasks, return_exceptions=True)
1925
- logger.info("[CLEANUP] All tasks cancelled")
1926
-
1927
- # Close event loops
1928
- if hasattr(asyncio, 'get_event_loop'):
1929
- try:
1930
- loop = asyncio.get_event_loop()
1931
- if loop and not loop.is_closed():
1932
- logger.info("[CLEANUP] Closing main event loop...")
1933
- loop.close()
1934
- logger.info("[CLEANUP] Main event loop closed")
1935
- except RuntimeError:
1936
- logger.info("[CLEANUP] No event loop to close")
1937
-
1938
- # Try to close any remaining loops
1939
- if hasattr(asyncio, '_get_running_loop'):
1940
- try:
1941
- running_loop = asyncio._get_running_loop()
1942
- if running_loop:
1943
- logger.info("[CLEANUP] Closing running loop...")
1944
- running_loop.close()
1945
- except:
1946
- pass
1947
-
1948
  except Exception as e:
1949
  logger.info(f"[CLEANUP] Asyncio cleanup note: {e}")
1950
 
 
255
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - ZEROENGINE - %(message)s', force=True)
256
  logger = logging.getLogger(__name__)
257
 
258
+ # Suppress asyncio warnings during shutdown (targeted approach)
259
  import warnings
260
  warnings.filterwarnings("ignore", category=RuntimeWarning, message=".*asyncio.*")
261
  warnings.filterwarnings("ignore", category=UserWarning, message=".*asyncio.*")
 
263
  # Set environment variable to suppress asyncio warnings
264
  os.environ['PYTHONASYNCIODEBUG'] = '0'
265
 
266
+ # Store original get_event_loop for restoration
267
  import asyncio
268
+ _original_get_event_loop = asyncio.get_event_loop
269
+
270
+ # Custom get_event_loop that handles shutdown gracefully
271
+ def _safe_get_event_loop():
272
+ try:
273
+ return _original_get_event_loop()
274
+ except RuntimeError as e:
275
+ if "no running event loop" in str(e).lower() or "closed" in str(e).lower():
276
+ # Return None gracefully instead of raising
277
+ return None
278
+ raise
279
+
280
+ # Replace with safe version
281
+ asyncio.get_event_loop = _safe_get_event_loop
282
 
283
  # --- KERNEL INITIALIZATION ---
284
  try:
 
1913
  except Exception as e:
1914
  logger.error(f"[CLEANUP] Model cleanup error: {e}")
1915
 
1916
+ # Final garbage collection with minimal asyncio cleanup
1917
  logger.info("[CLEANUP] Performing final cleanup...")
1918
  try:
1919
+ # Simple asyncio cleanup - don't interfere with Gradio
1920
  import asyncio
 
 
 
1921
  try:
1922
+ # Just try to close the main loop if it exists and is accessible
1923
+ loop = _original_get_event_loop()
1924
+ if loop and hasattr(loop, 'is_closed') and not loop.is_closed():
1925
+ logger.info("[CLEANUP] Closing event loop...")
1926
+ loop.close()
1927
+ logger.info("[CLEANUP] Event loop closed")
1928
+ except (RuntimeError, AttributeError):
1929
+ logger.info("[CLEANUP] No event loop to close")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1930
  except Exception as e:
1931
  logger.info(f"[CLEANUP] Asyncio cleanup note: {e}")
1932