Skip to content

Conversation

@Arkatufus
Copy link
Contributor

Fixes #7963

Fix CoordinatedShutdown CLR Termination Hook for .NET 10 Compatibility

Summary

This PR addresses a breaking change in .NET 10 where AppDomain.ProcessExit is no longer raised when termination signals (SIGTERM, SIGHUP) are received. This would have caused CoordinatedShutdown to fail silently in containerized environments (Kubernetes, Docker) when running on .NET 10+.

Breaking Change Reference

Changes

New Termination Signal Handler Abstraction

Introduced ITerminationSignalHandler interface with two implementations:

  • PosixTerminationSignalHandler (.NET 6+): Uses PosixSignalRegistration to handle SIGTERM/SIGHUP signals directly, with ProcessExit as fallback
  • LegacyTerminationSignalHandler (netstandard2.0): Uses ProcessExit only (maintains backward compatibility)

Key features:

  • Sets context.Cancel = true to allow graceful shutdown time before OS terminates
  • Uses Interlocked.CompareExchange to ensure callback only executes once
  • Supports dependency injection for testability

Files Modified/Created

File Change
src/core/Akka/Actor/TerminationSignalHandler.cs New - Interface and implementations
src/core/Akka/Actor/CoordinatedShutdown.cs Refactored InitClrHook to use abstraction
src/core/Akka.Tests/Actor/TerminationSignalHandlerSpec.cs New - 7 unit tests
src/core/Akka.Tests/Akka.Tests.csproj .NET 10 compatibility fixes

Additional .NET 10 Test Fixes

  1. System.Linq.Async conflict: Excluded transitive System.Linq.Async package for .NET 10+ to resolve ambiguous method calls with BCL's built-in System.Linq.AsyncEnumerable

  2. ConfigurationSpec exclusion: Added CORECLR define for .NET 10 to properly exclude App.config-based tests (which only work on .NET Framework)

Test Coverage

New tests verify:

  • Handler registration when run-by-clr-shutdown-hook is enabled/disabled
  • Shutdown tasks execute when termination signal is received
  • ClrExitReason is set correctly during CLR shutdown
  • Handler is disposed when ActorSystem terminates normally
  • CLR hooks only execute once (idempotency)
  • Exception handling in shutdown tasks

Backward Compatibility

Framework Handler Behavior
netstandard2.0 / .NET Framework 4.8 LegacyTerminationSignalHandler ProcessExit only (unchanged)
.NET 6/8 PosixTerminationSignalHandler SIGTERM/SIGHUP + ProcessExit fallback
.NET 10+ PosixTerminationSignalHandler Properly handles SIGTERM

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.

.NET 10 breaks CoordinatedShutdown

2 participants