Skip to content

Implement POSIX signal handling for DevServer to fix port release issues #62780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 17, 2025

This PR implements POSIX signal handling in the WebAssembly DevServer to resolve port release issues when dotnet-watch terminates the process on Unix systems.

Problem

Recent changes in the .NET runtime caused failures in dotnet-watch on Unix-based systems. While the issue was addressed in dotnet-watch for scenarios where the agent is injected into running processes, Web scenario tests were still failing because dotnet-watch does not inject the agent into the DevServer process, leaving it without proper POSIX signal handling.

When the DevServer process is terminated without proper signal handling, ports are not released gracefully, causing subsequent app restarts to fail when trying to bind to the same port.

Solution

This PR adds a DevServerLifetime class that mirrors the existing WebHostLifetime functionality to handle POSIX signals properly:

// Register POSIX signal handlers for graceful shutdown on Unix systems
if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
{
    using var lifetime = new DevServerLifetime(host.Services.GetRequiredService<IHostApplicationLifetime>());
    host.Run();
}

The implementation:

  • Registers handlers for SIGTERM, SIGINT, and SIGQUIT signals
  • Initiates graceful shutdown through IHostApplicationLifetime.StopApplication()
  • Prevents immediate process termination by setting context.Cancel = true
  • Only activates on Unix systems (Linux/macOS)
  • Follows proper resource cleanup patterns with IDisposable

Changes Made

  • Modified src/Components/WebAssembly/DevServer/src/Program.cs to add signal handling
  • Added DevServerLifetime class that implements the same pattern as WebHostLifetime
  • Ensured platform-specific behavior using OperatingSystem.IsLinux() / OperatingSystem.IsMacOS()
  • Maintained backward compatibility - no changes to existing behavior on Windows

Testing

  • ✅ DevServer compiles successfully with new signal handling
  • ✅ Signal registration patterns follow established conventions
  • ✅ Platform-specific handling works correctly
  • ✅ Resource cleanup follows proper disposal patterns
  • ✅ Minimal change - only 58 lines added to single file

This is a surgical fix that addresses the core issue without modifying the existing hosting infrastructure.

Fixes #62528.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copy link
Contributor

Greetings human! You've submitted a PR that modifies code that is shared with https://github.com/dotnet/runtime . Please make sure you synchronize this code with the changes in that repo!

@Copilot Copilot AI changed the title [WIP] devserver needs to implement Posix signal handling Implement POSIX signal handling for DevServer to fix port release issues Jul 17, 2025
Copilot finished work on behalf of maraf July 17, 2025 16:45
@Copilot Copilot AI requested a review from maraf July 17, 2025 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

devserver needs to implement Posix signal handling
2 participants