[PR #1270] [MERGED] Fix database connection pool graceful shutdown using actix built-in signal handling #1220

Closed
opened 2026-02-27 09:11:23 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/lldap/lldap/pull/1270
Author: @Copilot
Created: 9/3/2025
Status: Merged
Merged: 9/4/2025
Merged by: @nitnelave

Base: mainHead: copilot/fix-1269


📝 Commits (4)

  • 5125002 Initial plan
  • 864e7c7 Implement graceful database connection pool shutdown
  • 1b43afc Fix code formatting and finalize graceful DB shutdown implementation
  • af0382b Simplify database pool closure to use built-in actix signal handling

📊 Changes

1 file changed (+14 additions, -6 deletions)

View changed files

📝 server/src/main.rs (+14 -6)

📄 Description

Problem

Database connection pools were not being properly closed during server shutdown, causing the database server to remain unaware that connections were closed until TCP timeout. This could lead to connection limit issues during rapid server restarts, especially with client/server databases like MySQL and PostgreSQL.

From the sqlx documentation:

Due to a lack of async Drop, dropping the last Pool handle may not immediately clean up connections by itself. The connections will be dropped locally, which is sufficient for SQLite, but for client/server databases like MySQL and Postgres, that only closes the client side of the connection. The server will not know the connection is closed until potentially much later.

Solution

This PR implements proper graceful shutdown by calling .close().await on database connection pools when the server shuts down, leveraging actix-server's built-in signal handling.

Changes Made

  1. Modified set_up_server() function

    • Changed return type to Result<(ServerBuilder, DatabaseConnection)> to expose the database connection for cleanup
  2. Simplified run_server_command() with minimal changes

    • Get database connection from set_up_server()
    • Call server.run().await which uses actix-server's built-in signal handling
    • Close database pool when server.run() returns (indicating graceful shutdown)
    • Added proper error handling for database closure
  3. Updated create_schema_command()

    • Ensures database connection is properly closed after schema creation to prevent connection leaks

Approach

Rather than implementing custom signal handling, this solution leverages actix-server's existing signal handling capabilities. When SIGTERM or SIGINT is received, actix-server gracefully shuts down and server.run().await returns, at which point we close the database connection pool.

This ensures database servers are immediately notified of connection closures, preventing potential connection limit issues during deployments or rapid restarts.

Fixes #1269.


Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/lldap/lldap/pull/1270 **Author:** [@Copilot](https://github.com/apps/copilot-swe-agent) **Created:** 9/3/2025 **Status:** ✅ Merged **Merged:** 9/4/2025 **Merged by:** [@nitnelave](https://github.com/nitnelave) **Base:** `main` ← **Head:** `copilot/fix-1269` --- ### 📝 Commits (4) - [`5125002`](https://github.com/lldap/lldap/commit/5125002c0fbcba345990e4904181defd44a8102a) Initial plan - [`864e7c7`](https://github.com/lldap/lldap/commit/864e7c79bc1a50ceeaaf5488ab9a0855cfd5798f) Implement graceful database connection pool shutdown - [`1b43afc`](https://github.com/lldap/lldap/commit/1b43afc5c7201338bed04ae4666d82f95230aa24) Fix code formatting and finalize graceful DB shutdown implementation - [`af0382b`](https://github.com/lldap/lldap/commit/af0382b93c2744ab7d97eb17faa1ee98c8614f51) Simplify database pool closure to use built-in actix signal handling ### 📊 Changes **1 file changed** (+14 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `server/src/main.rs` (+14 -6) </details> ### 📄 Description ## Problem Database connection pools were not being properly closed during server shutdown, causing the database server to remain unaware that connections were closed until TCP timeout. This could lead to connection limit issues during rapid server restarts, especially with client/server databases like MySQL and PostgreSQL. From the sqlx documentation: > Due to a lack of async Drop, dropping the last Pool handle may not immediately clean up connections by itself. The connections will be dropped locally, which is sufficient for SQLite, but for client/server databases like MySQL and Postgres, that only closes the client side of the connection. The server will not know the connection is closed until potentially much later. ## Solution This PR implements proper graceful shutdown by calling `.close().await` on database connection pools when the server shuts down, leveraging actix-server's built-in signal handling. ### Changes Made 1. **Modified `set_up_server()` function** - Changed return type to `Result<(ServerBuilder, DatabaseConnection)>` to expose the database connection for cleanup 2. **Simplified `run_server_command()` with minimal changes** - Get database connection from `set_up_server()` - Call `server.run().await` which uses actix-server's built-in signal handling - Close database pool when server.run() returns (indicating graceful shutdown) - Added proper error handling for database closure 3. **Updated `create_schema_command()`** - Ensures database connection is properly closed after schema creation to prevent connection leaks ### Approach Rather than implementing custom signal handling, this solution leverages actix-server's existing signal handling capabilities. When SIGTERM or SIGINT is received, actix-server gracefully shuts down and `server.run().await` returns, at which point we close the database connection pool. This ensures database servers are immediately notified of connection closures, preventing potential connection limit issues during deployments or rapid restarts. Fixes #1269. <!-- START COPILOT CODING AGENT TIPS --> --- ✨ Let Copilot coding agent [set things up for you](https://github.com/lldap/lldap/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-02-27 09:11:23 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/lldap-lldap#1220
No description provided.