fix(relayenv): discard SDK client build that finishes after Close() - #800
Draft
aaron-zeisler wants to merge 1 commit into
Draft
fix(relayenv): discard SDK client build that finishes after Close()#800aaron-zeisler wants to merge 1 commit into
aaron-zeisler wants to merge 1 commit into
Conversation
startSDKClient builds the SDK client without holding c.mu, since the build can block up to sdkInitTimeout. It then unconditionally installs the result into c.clients. If Close() runs to completion while a build is still in flight, Close() has already closed and cleared c.clients and will never revisit it - a later Close() call short-circuits on c.closed without touching the map. A build that finishes afterward would silently install its client into the map, leaking its streaming connection and goroutines forever. Check c.closed after re-acquiring the lock and close the client instead of installing it when the environment has already been torn down.
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 a resource leak in
envContextImpl.startSDKClientwhere an SDK client build that finishes after the environment has already beenClose()'d gets installed intoc.clientsinstead of being closed, leaking its streaming connection and goroutines forever.Background
startSDKClientbuilds the SDK client viac.sdkClientFactory(...)without holdingc.mu(the build can block for up tosdkInitTimeout), then re-acquires the lock and unconditionally doesc.clients[sdkKey] = client, with no check ofc.closed.Close()only closes and clears the clients present inc.clientsat the moment it runs, setsc.closed = true, and never revisits the map again — a laterClose()call short-circuits on theclosedguard before touchingc.clients. If a build that was started beforeClose()(fromaddCredential'sgo c.startSDKClient(...)on SDK key rotation, or from the initial build kicked off inNewEnvContext) finishes afterClose()has already run to completion, it silently installs a client that will never be closed again. This is reachable in production whenever an environment/credential is torn down (e.g. removed via auto-config/RAC) while an SDK client build for it is still in flight, not just in tests.This was spotted as a side-finding while investigating an unrelated
feat/concurrent-keystest flake — that branch already carries an equivalent guard in its version ofstartSDKClientfor a related (but more elaborate, generation/anchor-based) scenario.v8predates that guard and needed its own, scoped to whatv8actually has.Changes
startSDKClientnow checksc.closedafter re-acquiring the lock; if the environment was closed while the build was in flight, the newly built client is closed instead of installed, and the evaluator/data-store wiring andinitErrare left untouched.Close()while it's blocked, then lets the build complete — asserting the client is closed and never appears inc.clients. Verified the test fails against the pre-fix code and passes after the fix, under-race.