Skip to content

fix: Fix resource leaks and shutdown handling in shared clients - #1244

Merged
ShannonDing merged 20 commits into
apache:masterfrom
wenxuwan:fix_shutdown_clean
Oct 10, 2026
Merged

ShannonDing merged 20 commits into
apache:masterfrom
wenxuwan:fix_shutdown_clean

Conversation

@wenxuwan

Copy link
Copy Markdown
Member

What is the purpose of the change

Fix resource retention and blocked shutdown paths when producers and consumers are repeatedly created and closed within a long-running process, such as a sidecar.

Ensure that releasing the last shared client owner closes its transports, wakes pending requests, and allows background workers to exit. Rebuilding a client with previously used options must create an independent NameServer transport without retaining retired clients or blocking unrelated clients.

Brief changelog

  • Close the NameServer transport when the last client owner releases the shared client.
  • Prevent remoting connections from reopening after shutdown, cancel pending TCP/TLS connection setup, and complete pending requests exactly once.
  • Make background delays, rebalance dispatch, and consumer concurrency-slot waits respond to shutdown.
  • Wake blocked pull-consumer Poll() calls on shutdown with the existing errors.ErrService; preserve ErrNoNewMsg for normal timeouts.
  • Stop transaction-check workers on producer shutdown and prevent repeated or failed starts from creating additional workers.
  • Isolate NameServer transports and route caches across client generations while preserving discovery configuration and credentials.
  • Copy NameServer snapshots outside the global client registry lock, then recheck the registry to handle concurrent creation safely.
  • Avoid capturing retired client options in transaction handlers so previous client generations can be garbage-collected.

Verifying this change

Added regression tests covering:

  • Repeated client, producer, and consumer creation and shutdown.
  • Last-owner connection cleanup and pending-request cancellation.
  • TLS handshake cancellation and concurrent response completion.
  • Blocked rebalance dispatch, concurrency-slot acquisition, and Poll() cancellation.
  • Real TCP route queries with ACL credentials after client reconstruction.
  • Overlapping old-client shutdown and replacement creation.
  • Unrelated client creation and shutdown during slow service discovery.
  • Garbage collection of retired client caches while the replacement remains alive.

Validation passed on the final revision:

  • Full race tests for internal/... and producer.
  • Consumer lifecycle, shutdown, polling, and rebalance regression tests under the race detector.
  • SOFAMQ integration tests using the modified SDK under the race detector.

Coverage percentage has not been measured.

Checklist

  • File and link a GitHub issue for this change.
  • Format the PR title as [ISSUE #<number>] <description> using the linked issue.
  • Provide a description explaining what changed, how, and why.
  • Verify the requested >80% test coverage. Regression tests have been added.
  • File an Apache Individual Contributor License Agreement if required.

}
if config.UseTls {
host, _, _ := net.SplitHostPort(addr)
tlsConn := tls.Client(conn, &tls.Config{ServerName: host, InsecureSkipVerify: true})

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This setting was already present before this PR. I kept it unchanged while making the TLS handshake cancellable on shutdown. Enabling certificate verification here could break existing deployments using self-signed certificates.

I suggest handling certificate verification and CA configuration in a separate PR

@ShannonDing
ShannonDing merged commit 461524a into apache:master Oct 10, 2026
6 of 7 checks passed
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