Repository navigation
Consul discovery: fix heartbeat and shutdown leaks, send health checks consistently, simplify registration - #1758
Merged
Merged
Conversation
Contributor
Summary - All Code Coverage (ubuntu-latest)
|
bart-vmware
marked this pull request as ready for review
October 16, 2026 15:16
TimHess
requested changes
Oct 19, 2026
…erviceEntry/FromOptions)
…rar that owns a single heartbeat
…ration); send heartbeats only for a registered TTL check
…, use app settings, cover HttpClient timeouts and log entries
bart-vmware
force-pushed
the
consul-fixes
branch
from
October 20, 2026 14:16
b12049c to
76f3030
Compare
|
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
Fixes leaks and a stream of failing heartbeat requests in the Consul discovery client, and simplifies the registration code behind it.
For Steeltoe users
Everything here is a fix. No configuration changes are needed to adopt it.
RegisterHealthCheck=false, no longer send heartbeats that fail with a 404.ShutdownAsyncalways releases the heartbeat and its subscription, also for disabled clients, and a failed start leaves nothing running.Register=false, and a failed registration no longer causes a deregistration error at shutdown.IsSecureand thesecuremetadata now agree whenSchemeis not lowercase (for exampleHTTPS).ConsulDiscoveryClient.GetLocalServiceInstance()is annotated as non-null (nullability only; it never returnednull).For reviewers
Structure
ConsulServiceInstanceIServiceInstancefor the local app (FromOptions) and remote instances (FromServiceEntry). ReplacesThisServiceInstance,ConsulRegistrationand the old constructor-based version.ConsulServiceRegistrationPayload,CheckMode(None/Ttl/Http),HeartbeatOptions(only for a TTL check), and a flag for the unknown-port warning.ConsulServiceRegistrarFailFast, ownership of the single heartbeat, shutdown, deregistration. Replaces the old registrar,ConsulServiceRegistryandTtlScheduler.PeriodicHeartbeatConsulServiceRegistrationis unrelated to the deletedConsulRegistration. The old type was a third copy of the instance data; the new one only describes what is sent to Consul. The diff shows one type removed and one added, not a rename.Also deleted:
SetStatusAsync/GetStatusAsync(no callers) and four container registrations that only tests resolved. All removed types were internal.Design decisions
Port > 0; heartbeat: whenever enabled), which caused the 404s. TheConsulServiceRegistrationconstructor is now the only decision point.ServiceRegister, so a reload during a slow registration is not lost. It is started after success and disposed on failure, cancellation or shutdown.ConsulServiceRegistraris deliberately not thread-safe:StartAsyncruns once in the client constructor (the container serializes singleton construction) andShutdownAsyncruns afterwards, from the hosted service. The only concurrent code is the options-change callback, confined toPeriodicHeartbeatbehind one private lock that is never held across anawait. This follows from experiments on .NET 8, 9 and 10: a callback can still run after unsubscribing (so the heartbeat ignores changes once disposed, or setting the period of a disposedPeriodicTimerthrows on the reload thread), and settingPeriodicTimer.Periodrestarts the countdown even for the same value (so unchanged intervals are not applied, or unrelated reloads would postpone heartbeats).StartAsyncreads the options once, so payload, heartbeat and retry cannot disagree. Shutdown deregisters based on whether registration succeeded.ConsulServiceInstancekeepsHostseparate whilePortandIsSecurecome from the URI, becauseUri.Hostis normalized andHostis what is sent to Consul.Tests
ConsulServiceRegistrationTest,ConsulServiceRegistrarTestandPeriodicHeartbeatTest.TestOptionsMonitor(Common.TestResources) now returns a real subscription and exposesListenerCount, which makes leak assertions possible.Quality checklist
If your change affects other repositories, such as Documentation and/or Samples, add linked PRs here.