fix(streams): observe discarded materialized Task<Done>s to prevent daemon-unobserved crashes#998
Merged
Aaronontheweb merged 3 commits intoMay 14, 2026
Conversation
…ved-task crashes Akka.Streams stages (Sink.ForEach via IgnoreSink, Source.Queue) create internal Task<Done> instances via TaskCompletionSource. Several code paths discarded those tasks (Keep.Left on Sink.ForEach; never calling WatchCompletionAsync on Source.Queue). On stream teardown the TCSes are faulted with AbruptTerminationException or StreamDetachedException; since .NET tracks "observed" per-Task, the discarded tasks fired TaskScheduler.UnobservedTaskException and surfaced as daemon-unobserved crash logs during hot reload and idle passivation. - SessionPipelineHandle.InitializeWithChannelAsync: Keep.Both + await both watch + sink tasks; wrap onStreamTerminated callback in try/catch. - SessionPipelineHandle.InitializeWithQueueAsync: Keep.Both + observe sink task; observe Source.Queue's WatchCompletionAsync task. - ChannelPipeline.CreateAsync: wrap inner Sink.ForEach with MapMaterializedValue + ContinueWith(OnlyOnFaulted) to observe the discarded foreach task. - RecordingSessionPipeline (test helper): same fix. - New SessionPipelineUnobservedTaskTests covers both observation patterns.
…elper Extract the MapMaterializedValue + ContinueWith pattern into StreamTaskObservation with an ObservingFault extension on Sink<TIn, Task<Done>>. Replaces four near-identical inline blocks across SessionPipelineHandle, ChannelPipeline, and RecordingSessionPipeline. Also drops the Keep.Both + dual-await dance in InitializeWithChannelAsync now that the sink's internal task is observed at sink-construction time, so ObserveTerminationAsync collapses back to a single await. Net 75 lines removed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
daemon-unobservedcrash classes (AbruptTerminationException,StreamDetachedException) triggered during ActorSystem hot reload and idle passivation.IgnoreSinkbackingSink.ForEach,QueueSource._completion) create internalTaskCompletionSource<Done>instances that fault on teardown. Our code discarded those tasks viaKeep.Left/ never callingWatchCompletionAsync()..NETtracks "observed" per-Task, so the discarded tasks firedTaskScheduler.UnobservedTaskExceptionon the finalizer thread.RunContinuationsAsynchronouslywidened the observation window. Crash-log rate went from ~2 over 6 days pre-bump to 90+ over 17 days post-bump (~25× jump). Upstream issues filed: akkadotnet/akka.net#8209, #8210, #8211.Changes
Netclaw.Actors/Channels/StreamTaskObservation.cswith two helpers:ObserveSilently(Task)— fault-onlyContinueWiththat reads.Exception.Sink<TIn, Task<Done>>.ObservingFault()— extension wrappingMapMaterializedValueto swapTask<Done>forNotUsedafter attachingObserveSilently.SessionPipelineHandle.InitializeWithChannelAsync/InitializeWithQueueAsync: apply.ObservingFault()to the outputSink.ForEach; callObserveSilentlyonSource.Queue.WatchCompletionAsync(). Wrap theonStreamTerminatedcallback in try/catch.ChannelPipeline.CreateAsync: same.ObservingFault()on the inputSink.ForEach.RecordingSessionPipeline(test helper): same fix in both reactive and non-reactive branches.SessionPipelineUnobservedTaskTestscovers both observation patterns (Keep.Both+await-both andMapMaterializedValue+continuation).Test plan
dotnet test src/Netclaw.Actors.Tests— 1584/1584 passingdotnet test src/Netclaw.Daemon.Tests— 529/529 passingdotnet slopwatch analyze— 0 issues./scripts/Add-FileHeaders.ps1 -Verify— cleancrash-*.logfiles withAbruptTerminationException/StreamDetachedException.