Skip to content

Commit 9256e22

Browse files
committed
DBA-732 clean shutdown
1 parent 45a8d39 commit 9256e22

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

src/postgres_mcp/server.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class AccessMode(str, Enum):
4949
# Global variables
5050
db_connection = DbConnPool()
5151
current_access_mode = AccessMode.UNRESTRICTED
52+
shutdown_in_progress = False
5253

5354

5455
async def get_sql_driver() -> Union[SqlDriver, SafeSqlDriver]:
@@ -545,32 +546,25 @@ async def main():
545546
logger.warning("Signal handling not supported on Windows")
546547
pass
547548

548-
# Run the app with FastMCP's stdio method
549-
try:
550-
await mcp.run_stdio_async()
551-
finally:
552-
# Close the connection pool when exiting
553-
await shutdown()
549+
await mcp.run_stdio_async()
554550

555551

556552
async def shutdown(sig=None):
557553
"""Clean shutdown of the server."""
558-
if sig:
559-
logger.info(f"Received exit signal {sig.name}")
554+
global shutdown_in_progress
560555

561-
logger.info("Closing database connections...")
562-
await db_connection.close()
556+
import os
557+
if shutdown_in_progress:
558+
logger.warning("Forcing immediate exit")
563559

564-
# Give tasks a chance to complete
565-
try:
566-
tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()]
567-
if tasks:
568-
logger.info(f"Waiting for {len(tasks)} tasks to complete...")
569-
await asyncio.gather(*tasks, return_exceptions=True)
570-
except Exception as e:
571-
logger.warning(f"Error during shutdown: {e}")
560+
os._exit(1) # Use immediate process termination instead of sys.exit
561+
562+
shutdown_in_progress = True
563+
564+
if sig:
565+
logger.info(f"Received exit signal {sig.name}")
572566

573-
logger.info("Shutdown complete.")
567+
os._exit(0)
574568

575569

576570
if __name__ == "__main__":

0 commit comments

Comments
 (0)