Skip to content

Commit 5b9c13b

Browse files
committed
DBA-732 clean shutdown
1 parent 45a8d39 commit 5b9c13b

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

src/postgres_mcp/server.py

Lines changed: 14 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,26 @@ 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
563557

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}")
558+
if shutdown_in_progress:
559+
logger.warning("Forcing immediate exit")
560+
561+
os._exit(1) # Use immediate process termination instead of sys.exit
562+
563+
shutdown_in_progress = True
564+
565+
if sig:
566+
logger.info(f"Received exit signal {sig.name}")
572567

573-
logger.info("Shutdown complete.")
568+
os._exit(0)
574569

575570

576571
if __name__ == "__main__":

0 commit comments

Comments
 (0)