Skip to content

Stop reporting a listener that was stopped on purpose as a fault - #1

Merged
aritchie merged 1 commit into
shinyorg:mainfrom
Kebechet:fix-stale-listener-fault-report
Dec 4, 2026
Merged

aritchie merged 1 commit into
shinyorg:mainfrom
Kebechet:fix-stale-listener-fault-report

Conversation

@Kebechet

@Kebechet Kebechet commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Description of Change

OnListeningEnded tells a stop from a dead listener by stopRequested. It runs as a continuation of the accept loops, and ExecuteSynchronously is only a hint: when the continuation is queued instead of inlined, it can run after the stop that ended those loops has finished and the next start has cleared the flag. The stopped generation is then logged at error as a listener that died underneath a running server.

We hit it in production on iOS with 2.2.1 (same code as main here). Our app stops and starts the server when it finds iOS has reclaimed the listening socket of the suspended app. The breadcrumbs, 12 ms apart:

12:53:47.701  Shutting down
12:53:47.713  Listening on http://127.0.0.1:60145
12:53:47.718  ERROR The server stopped listening while it believed it was running
              HttpServerListenerException: The accept loop ended while the server was still running.

That exception is the stand-in for loops that ended without throwing. The accept loop ends that way only when its token (stopping) was cancelled or stopRequested was set, and only StopCoreAsync does either, so the stand-in always means a stop. The error came 5 ms after the new listener was up.

The fix adds a second check to that guard: the loops must still be the server's own, the same identity rule RecoverFromListenerFaultAsync already applies to this window ("a stop may have completed and a start may have put fresh listeners behind a fresh task"). Recovery was never at risk, because that identity check stopped it; only the false error got out.

Issues Resolved

None filed; this PR is the report.

API Changes

None

Behavioral Changes

A stopped listener whose end is observed after the next start is no longer logged as a fault. A listener that dies underneath the running server is reported and recovered exactly as before.

Testing Procedure

  • New ResilienceTests.A_stopped_listener_reported_after_the_next_start_is_not_a_fault: start, stop, start, then hand OnListeningEnded the stopped loops (by reflection, since when a queued continuation runs is the runtime's choice). Before the fix it logs exactly the production error above; after it, nothing at error, and the server keeps serving. Passed 5 runs in a row.
  • ResilienceTests: 12/12 on net10.0, including the existing dead-listener tests.
  • Full suite on Windows 11, net10.0 and net11.0: the only failures also fail without this change on this machine (ACME chain building, the HTTP/3 ContentDigestTests case, a WebDAV name containing <, which Windows paths reject, and two 5 s timing tests).
  • Not tested on a device: the queued-continuation timing cannot be forced there.

Proposed release note for the documentation repo, under a new ## 3.2.1 TBD (I did not open a PR there):

<RN type="fix">**A listener stopped on purpose is no longer reported as a fault.** When the server was stopped and started again in quick succession, as an app does on resume, the stopped listener could be logged at error as one that died underneath a running server.</RN>

PR Checklist

  • Rebased on top of the target branch at time of PR
  • Changes adhere to coding standard
  • Sent to a v(branch) or DEV branch

OnListeningEnded runs as a continuation of the accept loops and tells a stop from a dead listener by stopRequested.
ExecuteSynchronously is only a hint: when the continuation is queued instead, it can run after the stop that ended
those loops has finished and the next start has cleared the flag. The stopped generation was then logged at error as
"The server stopped listening while it believed it was running", with the stand-in "The accept loop ended while the
server was still running." An app that stops and starts the server on resume hits this. Seen in production on iOS
(2.2.1): "Shutting down", then "Listening on ...", then that error 5 ms after the new listener was up.

The accept loop ends without throwing only when its token was cancelled or stopRequested was set, and only a stop does
either, so that stand-in always means a stop. OnListeningEnded now also checks that the loops are still the server's
own, the identity rule RecoverFromListenerFaultAsync already applies to the same window.
Copilot AI balanced review requested due to automatic review settings December 4, 2026 12:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues were identified for the change preventing stale listener callbacks from reporting false faults after a restart.

No open issues 🎉

What changed in this PR

Prevents delayed callbacks from intentionally stopped listeners from logging false faults after the HTTP server restarts.

Changes:

  • Checks listener task identity before reporting a fault.
  • Adds a regression test and logger factory helper to verify stale callbacks produce no errors and the restarted server remains responsive.
File Description
tests/​Shiny.Net.HttpServer.Tests/​ResilienceTests.cs Tests a stopped listener callback arriving after restart.
tests/​Shiny.Net.HttpServer.Tests/​RecordingLogger.cs Adds a factory adapter for capturing test logs.
src/​Shiny.Net.HttpServer/​Hosting/​HttpServer.cs Ignores callbacks from obsolete listener tasks.

Review effort: Balanced

@aritchie

aritchie commented Dec 4, 2026

Copy link
Copy Markdown
Member

Thanks @Kebechet — great catch, and a really clear write-up with the production breadcrumbs. The identity check matches the rule RecoverFromListenerFaultAsync already uses, and the full suite passes. Merged; it'll ship in 3.2.1 with your release note.

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.

3 participants