Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -439,7 +439,15 @@ def passive_gc_daemon():
|
|
| 439 |
global passive_gc_active
|
| 440 |
while passive_gc_active:
|
| 441 |
try:
|
| 442 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 443 |
if AGGRESSIVE_GC:
|
| 444 |
total_collected = 0
|
| 445 |
for pass_num in range(3):
|
|
@@ -453,6 +461,7 @@ def passive_gc_daemon():
|
|
| 453 |
logger.info(f"[PASSIVE-GC] Aggressive cleanup: {total_collected} objects collected")
|
| 454 |
except Exception as e:
|
| 455 |
logger.error(f"[PASSIVE-GC] Error: {e}")
|
|
|
|
| 456 |
|
| 457 |
def start_passive_gc():
|
| 458 |
"""Start passive garbage collector thread"""
|
|
@@ -467,8 +476,12 @@ def stop_passive_gc():
|
|
| 467 |
global passive_gc_active, passive_gc_thread
|
| 468 |
passive_gc_active = False
|
| 469 |
if passive_gc_thread and passive_gc_thread.is_alive():
|
| 470 |
-
|
| 471 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 472 |
|
| 473 |
# Start the passive GC thread
|
| 474 |
start_passive_gc()
|
|
@@ -1861,7 +1874,7 @@ if __name__ == "__main__":
|
|
| 1861 |
"""Cleanup function called on application exit"""
|
| 1862 |
logger.info("[CLEANUP] Starting graceful shutdown...")
|
| 1863 |
|
| 1864 |
-
# Stop passive GC
|
| 1865 |
stop_passive_gc()
|
| 1866 |
|
| 1867 |
# Cleanup kernel resources
|
|
@@ -1873,8 +1886,19 @@ if __name__ == "__main__":
|
|
| 1873 |
except Exception as e:
|
| 1874 |
logger.error(f"[CLEANUP] Model cleanup error: {e}")
|
| 1875 |
|
| 1876 |
-
# Final garbage collection
|
| 1877 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1878 |
for _ in range(3):
|
| 1879 |
gc.collect(2)
|
| 1880 |
logger.info("[CLEANUP] Final GC complete")
|
|
|
|
| 439 |
global passive_gc_active
|
| 440 |
while passive_gc_active:
|
| 441 |
try:
|
| 442 |
+
# Use shorter sleep intervals to allow faster shutdown
|
| 443 |
+
for _ in range(30): # 30 seconds = 30 * 1 second sleeps
|
| 444 |
+
if not passive_gc_active:
|
| 445 |
+
break
|
| 446 |
+
time.sleep(1)
|
| 447 |
+
|
| 448 |
+
if not passive_gc_active:
|
| 449 |
+
break
|
| 450 |
+
|
| 451 |
if AGGRESSIVE_GC:
|
| 452 |
total_collected = 0
|
| 453 |
for pass_num in range(3):
|
|
|
|
| 461 |
logger.info(f"[PASSIVE-GC] Aggressive cleanup: {total_collected} objects collected")
|
| 462 |
except Exception as e:
|
| 463 |
logger.error(f"[PASSIVE-GC] Error: {e}")
|
| 464 |
+
break
|
| 465 |
|
| 466 |
def start_passive_gc():
|
| 467 |
"""Start passive garbage collector thread"""
|
|
|
|
| 476 |
global passive_gc_active, passive_gc_thread
|
| 477 |
passive_gc_active = False
|
| 478 |
if passive_gc_thread and passive_gc_thread.is_alive():
|
| 479 |
+
# Wait for thread to finish with timeout
|
| 480 |
+
passive_gc_thread.join(timeout=5.0)
|
| 481 |
+
if passive_gc_thread.is_alive():
|
| 482 |
+
logger.warning("[PASSIVE-GC] Thread did not shut down gracefully")
|
| 483 |
+
else:
|
| 484 |
+
logger.info("[PASSIVE-GC] Background garbage collector stopped")
|
| 485 |
|
| 486 |
# Start the passive GC thread
|
| 487 |
start_passive_gc()
|
|
|
|
| 1874 |
"""Cleanup function called on application exit"""
|
| 1875 |
logger.info("[CLEANUP] Starting graceful shutdown...")
|
| 1876 |
|
| 1877 |
+
# Stop passive GC first
|
| 1878 |
stop_passive_gc()
|
| 1879 |
|
| 1880 |
# Cleanup kernel resources
|
|
|
|
| 1886 |
except Exception as e:
|
| 1887 |
logger.error(f"[CLEANUP] Model cleanup error: {e}")
|
| 1888 |
|
| 1889 |
+
# Final garbage collection with asyncio cleanup
|
| 1890 |
try:
|
| 1891 |
+
import asyncio
|
| 1892 |
+
# Close any remaining event loops
|
| 1893 |
+
try:
|
| 1894 |
+
loop = asyncio.get_event_loop()
|
| 1895 |
+
if loop and not loop.is_closed():
|
| 1896 |
+
loop.close()
|
| 1897 |
+
except RuntimeError:
|
| 1898 |
+
# No event loop or already closed
|
| 1899 |
+
pass
|
| 1900 |
+
|
| 1901 |
+
# Final GC
|
| 1902 |
for _ in range(3):
|
| 1903 |
gc.collect(2)
|
| 1904 |
logger.info("[CLEANUP] Final GC complete")
|