diff --git a/core/database.py b/core/database.py index efaadb5..088f114 100644 --- a/core/database.py +++ b/core/database.py @@ -587,7 +587,17 @@ class NFOGuardDatabase: """Close all database connections""" if hasattr(self._local, 'connection'): try: + # For PostgreSQL, ensure all transactions are committed/rolled back + try: + # Force rollback any open transactions + self._local.connection.rollback() + except Exception: + pass + + # Close the connection self._local.connection.close() delattr(self._local, 'connection') - except Exception: + print("✅ Database connection closed successfully") + except Exception as e: + print(f"⚠️ Error closing database connection: {e}") pass # Connection may already be closed \ No newline at end of file diff --git a/main.py b/main.py index 5ca49c0..97eb35a 100644 --- a/main.py +++ b/main.py @@ -146,6 +146,18 @@ def signal_handler(signum, frame): _log("WARNING", f"Error closing database: {e}") _log("INFO", "Graceful shutdown complete") + + # Force exit after 2 seconds if graceful shutdown doesn't work + import threading + def force_exit(): + import time + time.sleep(2) + _log("WARNING", "Force exiting after timeout") + os._exit(0) + + force_thread = threading.Thread(target=force_exit, daemon=True) + force_thread.start() + sys.exit(0)