Skip to content

bugfix: restore naming server client lifecycle after close - #1199

Open
yangjj-iso wants to merge 9 commits into
apache:masterfrom
yangjj-iso:codex/fix-naming-server-lifecycle
Open

yangjj-iso wants to merge 9 commits into
apache:masterfrom
yangjj-iso:codex/fix-naming-server-lifecycle

Conversation

@yangjj-iso

@yangjj-iso yangjj-iso commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor
  • I have registered the PR changes.

What this PR does:

  • Give each NamingServer registry service its own client, so closing one service leaves other services running.
  • Recreate the cached GetInstance client after it has been closed, while preserving reuse of an active client.
  • Add lifecycle regression tests and close service-owned clients in existing tests.
  • Verify that a recreated client performs background health checks against a local HTTP server, after the old client has fully stopped.
  • Document initialization as a startup operation: callers must stop using and close the prior registry before reinitialization; initialization and registry access must be serialized.

Which issue(s) this PR fixes:

Fixes #1198

Special notes for your reviewer:

Validation of 3a40d90 after merging current master (82570d9), with Go 1.25.5 on Linux:

  • Targeted lifecycle tests passed with go test -race ./pkg/discovery -run 'TestGetInstanceRecreatesClosedClient|TestNamingServerRegistryServicesOwnClients|TestInitRegistry_WithNamingServerConfig|TestInitRegistryWithErrorSupportedProviders' -count=30.
  • go test ./pkg/discovery -skip '^(TestWatch|TestSubscribeStartsWatchLoopPerVGroup)$' -count=1 passed.
  • go vet ./pkg/discovery, gofmt, and git diff --check passed.
  • The full discovery suite was attempted. TestWatch fails because this environment denies the netlink operation used to determine the local IP; TestSubscribeStartsWatchLoopPerVGroup also times out. Both failures reproduce on unchanged master at 82570d9. Full-suite success is not claimed for this environment.
  • Both changelog conflicts are resolved, retaining upstream entries. The published tree matches the locally tested tree (beb4891594fa8cb26eae8ad9ae2c28b9a48b02bf).

Hosted validation of 293c4e3: all 17 checks pass, including full unit tests, integration tests, lint, build, Go Vet, CodeQL, licenses, and the three Go-version transaction jobs. The previous integration startup failure came from the unused docker/setup-buildx-action@v3 step being disallowed by the repository's action policy. This follow-up uses the hosted runner's existing Docker installation and verifies it with docker version and docker info; all integration steps remain in place. actionlint and git diff --check pass for the workflow change.

Does this PR introduce a user-facing change?:

Yes. NamingServer registry services can be recreated after closure, and closing one no longer stops another service's client.

Fix NamingServer client reuse after Close and isolate registry service client lifetimes.

@github-actions github-actions Bot added bug Something isn't working documentation Improvements or additions to documentation coding milestone labels Sep 24, 2026
@codecov-commenter

codecov-commenter commented Sep 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 65.33%. Comparing base (02bfd00) to head (e208812).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1199      +/-   ##
==========================================
+ Coverage   65.31%   65.33%   +0.01%     
==========================================
  Files         312      312              
  Lines       23890    23899       +9     
==========================================
+ Hits        15604    15614      +10     
+ Misses       6873     6871       -2     
- Partials     1413     1414       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@thunguo

thunguo commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Plz resolve the conflicts.

Preserve all upstream release notes and both entries for apache#1198.

Validation: targeted NamingServer lifecycle regressions pass with the race detector (three runs) on Go 1.20.14. Two full-package Watch tests also fail unchanged on master because this environment denies local interface discovery.

@XiaoFeiASK XiaoFeiASK left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the fix, @yangjj-iso. The direction is right: each NamingServer registry service now owns its client, and GetInstance no longer returns a closed cached client. All CI checks pass on 4beb449; I also ran go test ./pkg/discovery -count=1 and the targeted lifecycle tests under -race locally.

I do not see a merge-blocking correctness issue in this revision. Two non-blocking points are below.

P2 (non-blocking): repeated InitRegistry leaves the previous client running

newNamingServerRegistryService(...) now creates a fresh client on every call (pkg/discovery/naming_server.go:248-250). That client immediately starts a ticker and health-check goroutine (pkg/discovery/naming_server.go:273-284).

InitRegistryWithError still replaces registryServiceInstance without closing the previous instance (pkg/discovery/init.go:44-51). If InitRegistry is called again without closing the old registry, the old client is no longer reachable from GetRegistry(), but its ticker, health-check goroutine, and any watchLoop created by Lookup/Subscribe continue running. Repeated reinitialization therefore accumulates background work and may keep delivering events from a stale registry.

This is not blocking today because the normal client path is guarded by sync.Once (pkg/client/client.go:133-136). If repeated initialization is not supported, please state that contract; otherwise close the previous instance before replacement or reject a second initialization.

P3: the regression test only proves the client is not closed

TestGetInstanceRecreatesClosedClient checks that second != first and that second.closeChan is open (pkg/discovery/naming_server_test.go:59-73). It would still pass if the replacement client failed to start health checks or could not watch. A mock-server assertion that the recreated client performs a health check or watch would make the lifecycle regression explicit.

The change itself looks correct and I do not see anything else blocking.

Resolve both changelog conflicts while preserving upstream entries.
Document startup-only registry initialization and explicit prior-service cleanup.
Verify the recreated client resumes background health checks against a local server.
@yangjj-iso

Copy link
Copy Markdown
Contributor Author

Addressed the follow-up in 3a40d90 and resolved the changelog conflicts against current master (82570d9).

  • Documented the startup lifecycle on InitRegistry / InitRegistryWithError: callers must stop using and close the previous registry before reinitializing, and serialize initialization with registry access. Runtime replacement remains outside this PR.
  • Strengthened TestGetInstanceRecreatesClosedClient with a local HTTP server. It waits for the first client's health check, closes that client (which joins its worker), discards any buffered old request, and requires a new background health check from the replacement.

The lifecycle race tests pass for 30 repetitions; the discovery suite passes with the two environment-dependent watch tests explicitly excluded; vet and formatting pass. Both excluded failures reproduce on unchanged master in this environment (netlink access is denied in TestWatch). Exact commands and limitations are in the updated PR description.

Hosted validation currently has 16 successful checks, including the unit-test workflow. The integration workflow reports startup_failure before any job starts, as it did on the previous head. Full hosted success is not claimed.

The enterprise action policy blocks docker/setup-buildx-action@v3 before
any integration job starts. Both sample Compose stacks use prebuilt
images, so verify the runner's existing Docker client and daemon instead
of creating an unused Buildx builder. Keep all integration steps enabled.

Validation: actionlint 1.7.7 and git diff --check pass.
@github-actions github-actions Bot added the ci/cd label Nov 7, 2026
@yangjj-iso

Copy link
Copy Markdown
Contributor Author

Follow-up in 293c4e3: the integration workflow's startup failure was caused by docker/setup-buildx-action@v3 being disallowed by the repository's action policy.

The integration scripts only run prebuilt images and do not need a Buildx builder. I replaced that unused setup action with docker version and docker info against the hosted runner's Docker installation. All integration test steps remain in place.

actionlint and git diff --check pass. Integration run 42379145815 now completes successfully, including the Docker-backed sample and Saga tests.

@yangjj-iso
yangjj-iso requested a review from XiaoFeiASK November 7, 2026 14:09
longPollClient: &http.Client{Timeout: 30 * time.Second},
namingServerMu.Lock()
defer namingServerMu.Unlock()
if namingServerInstance != nil {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P2] GetInstance 的“关闭后重建”分支在本仓库已无生产调用方,单例生命周期机制变为测试专属代码

本 PR 把 newNamingServerRegistryService 改为直接调用 newNamingServerClient(pkg/discovery/naming_server.go:248),GetInstance 从此在本仓库内只被 naming_server_test.go 调用(grep -rn "GetInstance\b" --include=*.go . 仅命中定义与测试)。也就是说,changes/dev.md 声明的“recreate a closed NamingServer client”实际是由“每个 registry service 独立持有 client”实现的;namingServerInstance、namingServerMu、resetInstance(仍保留 Close() + clearNamingAddrCache() 的清理逻辑)以及这里新增的关闭检测分支,只剩测试在使用。

触发条件与影响:后续若有代码按 GetInstance 的语义复用它,拿到的生命周期模型与仓库实际的注册路径不同;两套模型并存时,resetInstance 清理的是单例,而 registry service 关闭的是自持 client,改动其中一处不会同步到另一处,resetInstance 的清缓存行为也不再被任何生产路径验证。

建议二选一:① 删除 GetInstance / resetInstance / namingServerInstance / namingServerMu,测试改用 newNamingServerClient 并显式 Close;② 让 newNamingServerRegistryService 仍经由 GetInstance,把重建语义收敛到单一入口。若 GetInstance 因对外 API 需要保留,请在注释中写明它不参与 registry 初始化路径,并在变更日志里区分“单例重建”与“每服务独立 client”两件事。

@XiaoFeiASK

Copy link
Copy Markdown
Contributor

lgtm

yangjj-iso and others added 3 commits November 23, 2026 00:46
Keep the NamingServer lifecycle entry and the upstream IPv6 coordinator entry in the development changelog. Preserve the upstream file mode.

Targeted discovery race tests passed for 15 repetitions. The affected packages passed with the two discovery watch tests excluded after confirming both also fail on unchanged master in this environment. Go vet and diff checks passed.
Signed-off-by: redyyang <redyyang@tencent.com>
The sample module declares Go 1.25. Integration now uses Go 1.25 while the library minimum-version jobs keep Go 1.20. Reproduced the integration module edits and built the sample integration packages with Go 1.25.5.

Signed-off-by: redyyang <redyyang@tencent.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ci/cd coding documentation Improvements or additions to documentation milestone

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] [Discovery] NamingServer GetInstance returns a closed client after Close

5 participants