Skip to content

Make LazyInit initialization single-flight - #14

Open
adenzhou1350 wants to merge 1 commit into
deepseek-ai:mainfrom
adenzhou1350:codex/thread-safe-lazy-init
Open

adenzhou1350 wants to merge 1 commit into
deepseek-ai:mainfrom
adenzhou1350:codex/thread-safe-lazy-init

Conversation

@adenzhou1350

Copy link
Copy Markdown

Summary

  • make concurrent first use of LazyInit initialize exactly one shared object
  • preserve retry after factory exceptions or null results
  • keep copies on the same initialization state and report moved-from use cleanly

Problem

LazyInit::get() currently reads and writes the same std::shared_ptr without synchronization. Concurrent first use is therefore a data race and may also run the factory more than once. This matters for lazily constructed runtimes when several Python or host threads enter the extension together.

Validation

  • Upstream baseline: a synchronized 16-thread first-use test reports 16 factory calls instead of one.
  • Candidate: 25 rounds x 16 threads each produced one factory call, one shared address, and the expected value.
  • ThreadSanitizer: the same standalone probe reports a data race in baseline LazyInit<int>::operator->(); the candidate exits cleanly.
  • Exception retry, empty factory, null result, assignment, copy, move, -Wall -Wextra -Werror, Python syntax compilation, and git diff --check pass.

This is host-side correctness work; it does not claim a GPU performance improvement.

Signed-off-by: Xucheng Zhou <aden1350@outlook.com>
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.

1 participant