Skip to content

Commit 5f2a313

Browse files
authored
DBA-732 graceful shutdown (#42)
1 parent 7eb2390 commit 5f2a313

File tree

4 files changed

+28
-24
lines changed

4 files changed

+28
-24
lines changed

justfile

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,22 @@ dev:
1414
nix-claude-desktop:
1515
NIXPKGS_ALLOW_UNFREE=1 nix run "github:k3d3/claude-desktop-linux-flake#claude-desktop-with-fhs" --impure
1616

17-
release version note="Release v{{version}}" extra="": # NOTE version format should be 0.0.0
17+
release-help:
18+
@echo "- update version in pyproject.toml"
19+
@echo "- uv sync"
20+
@echo "- git commit"
21+
@echo "- git push && merge to main"
22+
@echo '- just release 0.0.0 "note"'
23+
@echo 'OR'
24+
@echo '- just prerelease 0.0.0 1 "note"'
25+
26+
release version note extra="": # ="Release v{{version}}" extra="": # NOTE version format should be 0.0.0
1827
#!/usr/bin/env bash
1928
if [[ "{{version}}" == v* ]]; then
2029
echo "Error: Do not include 'v' prefix in version. It will be added automatically."
2130
exit 1
2231
fi
23-
uv build && git tag -a "v{{version}}" -m "Release v{{version}}" && git push --tags && gh release candidate "v{{version}}" --title "PostgreSQL MCP v{{version}}" --notes "{{note}}" {{extra}} dist/*.whl dist/*.tar.gz
32+
uv build && git tag -a "v{{version}}" -m "Release v{{version}}" || true && git push --tags && gh release create "v{{version}}" --title "PostgreSQL MCP v{{version}}" --notes "{{note}}" {{extra}} dist/*.whl dist/*.tar.gz
2433

25-
prerelease version rc note="Release candidate {{rc}} for version {{version}}":
34+
prerelease version rc note: #="Release candidate {{rc}} for version {{version}}":
2635
just release "{{version}}rc{{rc}}" "{{note}}" "--prerelease"

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "postgres-mcp"
3-
version = "0.1.1rc1"
3+
version = "0.1.1"
44
description = "PostgreSQL Tuning and Analysis Tool"
55
readme = "README.md"
66
requires-python = ">=3.12"

src/postgres_mcp/server.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class AccessMode(str, Enum):
4848
# Global variables
4949
db_connection = DbConnPool()
5050
current_access_mode = AccessMode.UNRESTRICTED
51+
shutdown_in_progress = False
5152

5253

5354
async def get_sql_driver() -> Union[SqlDriver, SafeSqlDriver]:
@@ -542,32 +543,26 @@ async def main():
542543
logger.warning("Signal handling not supported on Windows")
543544
pass
544545

545-
# Run the app with FastMCP's stdio method
546-
try:
547-
await mcp.run_stdio_async()
548-
finally:
549-
# Close the connection pool when exiting
550-
await shutdown()
546+
await mcp.run_stdio_async()
551547

552548

553549
async def shutdown(sig=None):
554550
"""Clean shutdown of the server."""
555-
if sig:
556-
logger.info(f"Received exit signal {sig.name}")
551+
global shutdown_in_progress
557552

558-
logger.info("Closing database connections...")
559-
await db_connection.close()
553+
import os
560554

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

570-
logger.info("Shutdown complete.")
565+
os._exit(128 + sig if sig is not None else 0)
571566

572567

573568
if __name__ == "__main__":

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)