Skip to content

Fix CTRL+C shutdown error with WebSocket connections#49

Open
konard wants to merge 4 commits intomainfrom
issue-5-11619808
Open

Fix CTRL+C shutdown error with WebSocket connections#49
konard wants to merge 4 commits intomainfrom
issue-5-11619808

Conversation

@konard
Copy link
Member

@konard konard commented Sep 13, 2025

Summary

This PR fixes the unhandled exception that occurs when pressing CTRL+C to shutdown the GraphQL server while WebSocket connections are active.

Problem

When CTRL+C is pressed to shutdown the server, the following error occurs if clients are connected via WebSocket:

System.AggregateException: One or more errors occurred. (The connection was aborted because the server is shutting down and request processing didn't complete within the time specified by HostOptions.ShutdownTimeout.)

Root Cause

The ASP.NET Core host tries to gracefully shutdown within the default timeout period, but active WebSocket connections don't get properly closed before the timeout expires, causing the connections to be abruptly terminated.

Solution

This implementation provides graceful shutdown handling by:

  1. Extended Shutdown Timeout: Configure HostOptions.ShutdownTimeout to 30 seconds to allow more time for graceful shutdown
  2. Graceful Shutdown Service: Created GracefulShutdownService that hooks into the IHostApplicationLifetime.ApplicationStopping event
  3. WebSocket Connection Handling: Provides time for active WebSocket connections to close gracefully before the server terminates

Changes Made

  • Program.cs: Added HostOptions configuration with 30-second shutdown timeout
  • StartupWithRouting.cs: Registered GracefulShutdownService as a hosted service
  • GracefulShutdownService.cs: New service that handles application stopping events and provides graceful shutdown logging

Technical Details

The solution follows ASP.NET Core best practices for graceful shutdown:

  • Uses IHostApplicationLifetime to detect shutdown signals
  • Provides adequate time for WebSocket connections to close naturally
  • Logs shutdown progress for debugging
  • Works with the existing GraphQL.Server.Transports.Subscriptions.WebSockets framework

Testing

The solution has been tested by building the project successfully without compilation errors.

References

Fixes #5

🤖 Generated with Claude Code

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #5
@konard konard self-assigned this Sep 13, 2025
- Add GracefulShutdownService to handle application stopping event
- Configure HostOptions with 30-second shutdown timeout
- Register hosted service in dependency injection container
- Handle graceful shutdown of WebSocket connections before timeout

Fixes #5

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard changed the title [WIP] Error on CTRL+C shutdown Fix CTRL+C shutdown error with WebSocket connections Sep 13, 2025
@konard konard marked this pull request as ready for review September 13, 2025 05:47
🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>'
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.

Error on CTRL+C shutdown

1 participant