Skip to content

Conversation

@yuseok-kim-edushare
Copy link
Contributor

This pull request introduces a comprehensive and robust graceful shutdown mechanism for the Garnet server, both for Windows service and console application scenarios. The changes ensure that when a shutdown is requested (via service stop, Ctrl+C, or process exit), the server stops accepting new connections, waits for existing connections to finish, commits all data (AOF), and takes a checkpoint if necessary, all within a configurable timeout. This significantly improves data durability and operational reliability during shutdowns.

Mainly goal is Close #1382 and Resolve #1390
This PR reflect #1383 (comment)

Graceful Shutdown Implementation

  • Added a new ShutdownAsync method to main/GarnetServer/GarnetServer that orchestrates the graceful shutdown process: stops accepting new connections, waits for active connections to finish (with timeout), commits AOF, and takes a checkpoint if tiered storage is enabled.
  • Modified the Windows service (Worker.StopAsync) and console app (Program.Main) to use the new ShutdownAsync method, ensuring consistent and graceful shutdown behavior in both entrypoints. [1] [2]

Server Interface and Networking Enhancements

  • Extended the server interface (libs/server/Servers/IGarnetServer) and base classes to support stopping listening for new connections via a new StopListening method, and implemented this for TCP servers to close the listen socket cleanly. [1] [2] [3]

Data Durability and Checkpointing

  • Added new APIs in libs/server/Servers/StoreApi to take a checkpoint and to check if AOF or storage tier is enabled, supporting the shutdown flow for data durability.

Infrastructure and Code Quality Improvements

  • Ensured proper disposal patterns and resource cleanup, including calling base.Dispose() and suppressing finalization.
  • Updated using directives and minor code structure for clarity and consistency. [1] [2] [3]

Configuration and Timeout Handling

  • Added configuration for shutdown timeout in the Windows service host, defaulting to 5 seconds for graceful shutdown.

These changes collectively make server shutdowns safer and more reliable, reducing the risk of data loss or corruption during restarts or deployments.

Adds a graceful shutdown mechanism to the Garnet server, ensuring new connections are stopped, active connections are awaited, and data is safely persisted (AOF commit and checkpoint) before exit. Updates include new ShutdownAsync logic in GarnetServer, StopListening support in server classes, and integration of shutdown handling in both Windows service and console entry points.
Copilot AI review requested due to automatic review settings November 26, 2025 15:59
Copilot finished reviewing on behalf of yuseok-kim-edushare November 26, 2025 16:04
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request implements a comprehensive graceful shutdown mechanism for the Garnet server, addressing GitHub issues #1382 and #1390. The implementation provides a coordinated shutdown process that stops accepting new connections, waits for active connections to complete, commits the Append-Only File (AOF), and takes checkpoints for tiered storage—all within configurable timeouts. This enhancement significantly improves data durability and operational reliability during server restarts and deployments.

Key Changes:

  • Added ShutdownAsync method to coordinate graceful shutdown with connection draining, AOF commit, and checkpointing
  • Introduced StopListening method to cleanly stop accepting new connections while preserving existing ones
  • Updated both console application and Windows service entry points to use the new graceful shutdown flow with proper timeout handling

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
main/GarnetServer/Program.cs Converted Main to async, added shutdown signal handlers (Ctrl+C, ProcessExit), implemented graceful shutdown with timeout and disposal coordination
libs/host/GarnetServer.cs Added ShutdownAsync method orchestrating stop-listening, connection waiting, AOF commit, and checkpoint operations
libs/server/Servers/IGarnetServer.cs Extended interface with StopListening method for graceful shutdown support
libs/server/Servers/GarnetServerBase.cs Added virtual StopListening method with default no-op implementation
libs/server/Servers/GarnetServerTcp.cs Implemented StopListening by closing listen socket and tracking listening state
libs/server/Servers/StoreApi.cs Added TakeCheckpoint method and storage tier/AOF enabled properties for shutdown logic
hosting/Windows/Garnet.worker/Worker.cs Updated StopAsync to call ShutdownAsync before disposal, improved Dispose pattern with base.Dispose and GC.SuppressFinalize
hosting/Windows/Garnet.worker/Program.cs Added HostOptions configuration for 5-second shutdown timeout and System namespace import
Comments suppressed due to low confidence (1)

main/GarnetServer/Program.cs:16

  • Disposable 'CancellationTokenSource' is created but not disposed.
            var shutdownCts = new CancellationTokenSource();

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@TalZaccai TalZaccai requested a review from badrishc December 2, 2025 19:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Implement Graceful Shutdown for Garnet as a Windows Service List aof persistent restore issues

1 participant