Skip to content

fix(inference): share SDK initialization across concurrent calls - #908

Open
Afloat16 wants to merge 2 commits into
NVIDIA:mainfrom
Afloat16:fix/llm-client-concurrent-initialization-20261126
Open

Afloat16 wants to merge 2 commits into
NVIDIA:mainfrom
Afloat16:fix/llm-client-concurrent-initialization-20261126

Conversation

@Afloat16

@Afloat16 Afloat16 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Tier 2 reviews up to five content clusters concurrently on one LLMClient. Its lazy SDK cache was checked before SDK construction without synchronization. When first calls overlap, each worker constructs a separate OpenAI or Anthropic client and overwrites the cache, repeating connection-pool setup instead of sharing one client. A credential-free probe using the real OpenAI SDK and five simultaneous callers created and returned five distinct clients on the base revision.

Protect only SDK initialization with a per-instance lock and check the cache again after acquiring it. Build and validate the client before publishing it, so a constructor failure leaves the cache empty and permits a later attempt. Completion requests run after the lock is released.

Verification

  • Clarified that retry after SDK construction fails preserves existing behavior; the simultaneous-initialization cases reproduce the race.
  • I am familiar with the Contributing Guidelines.
  • Added focused regressions: five simultaneous initial calls for each real SDK, recovery after construction fails, and overlapping completion requests through both real SDKs with an in-process HTTP transport.
  • The two initialization regressions fail on the base revision: five SDK instances instead of one. The same five-thread OpenAI probe creates one client after the change.
  • 195 focused inference-retry and Tier 2 tests passed, including all six new regressions.
  • On a fresh Ubuntu 24.04 runner with Python 3.12 and all locked extras installed, ran the required make lint, make test, and make build gates against commit 6375935ecd99128fb37a32f5fb6b5195ad44184f: all passed. The full test result was 45,545 passed, 338 skipped, and 19 deselected; the skips and selection are the repository defaults.
  • The wheel and source distribution built successfully. Changed-file formatting and git diff --check also passed.
  • Full verification log: https://github.com/Afloat16/SkillEvaluator/actions/runs/45816814436/job/139898560275
  • No provider credentials, private datasets, or proprietary benchmark content were added.

Release Impact

  • Updated CHANGELOG.md.

Signed-off-by: Afloat <ShuffleMonkey@163.com>

@chrisknvidia chrisknvidia left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I reproduced the race on main with the PR's own tests: five simultaneous first calls built five OpenAI clients and five Anthropic clients, and the overlapping-completion test for Anthropic tripped it in 14 of 20 runs (two clients). At this head all six pass. The lock is only held around construction, a constructor failure leaves _client empty so the next call retries, the exceptions callers see are unchanged, the lock is per instance so two differently configured clients never share an SDK object, and the lock order (_client_lock then _provider_config_lock) is never reversed anywhere in the module. The inference, retry, rubric and embedding suites are the same between main and this head.

One not-blocking note inline on the retry-after-failure test, which passes on main too (the old code also left the cache empty when the constructor raised), so it guards against a stickier future fix rather than reproducing the bug.

Not blocking: EmbeddingClient in src/skillevaluator/embedding/client.py has the same unsynchronized lazy _client and _provider_config caches. It is out of scope here, but if it is ever called from the same worker pool it would want the same treatment.

Signed-off-by: Afloat <ShuffleMonkey@163.com>

This branch has not been deployed

No deployments
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.

2 participants