Skip to content

Use atomic<shared_ptr> for current RPC agent - #185633

Closed
Anndrey24 wants to merge 2 commits into
pytorch:mainfrom
Anndrey24:rpc-atomic
Closed

Anndrey24 wants to merge 2 commits into
pytorch:mainfrom
Anndrey24:rpc-atomic

Conversation

@Anndrey24

@Anndrey24 Anndrey24 commented May 29, 2026 •

Copy link
Copy Markdown
Contributor

std::atomic_load, std::atomic_exchange, and std::atomic_compare_exchange_* overloads for std::shared_ptr<T> are deprecated since C++20 (ref). Newer GCC/libstdc++ warns on these deprecated overloads, and warning-as-error builds can turn this into a build failure, as encountered in #182016.

This PR updates RpcAgent::currentRpcAgent_ from a plain std::shared_ptr<RpcAgent> accessed through the deprecated free-function atomic API to std::atomic<std::shared_ptr<RpcAgent>> accessed through the modern member API when __cpp_lib_atomic_shared_ptr is available.

The implementation keeps the legacy free-function path on older standard libraries that do not provide the C++20 std::atomic<std::shared_ptr<T>> specialization.

cc. @aditew01 @malfet

@pytorch-bot

pytorch-bot Bot commented May 29, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/185633

Note: Links to docs will display an error until the docs builds have been completed.

⏳ 1 Pending, 2 Unrelated Failures

As of commit 8c033e3 with merge base 79a1684 (image):

UNSTABLE - The following jobs are marked as unstable, possibly due to flakiness on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot Bot added the release notes: distributed (rpc) release notes category label May 29, 2026
@malfet

malfet commented May 29, 2026

Copy link
Copy Markdown
Collaborator

I've tried it yesterday already: it will not work on MacOS unfortunately

@Anndrey24

Copy link
Copy Markdown
Contributor Author

I've tried it yesterday already: it will not work on MacOS unfortunately

I see, thank you for checking! I remember there being a reason why I kept the deprecated APIs too, but I couldn't remember the exact CI failure.

Should I add back both versions to this PR in this case?

@Skylion007

Copy link
Copy Markdown
Collaborator

Sigh yeah don't think this works sadly...

@aditew01 aditew01 added the ciflow/trunk Trigger trunk jobs on your pull request label Jun 1, 2026
@aditew01

aditew01 commented Jun 1, 2026

Copy link
Copy Markdown
Collaborator

Sigh yeah don't think this works sadly...

@Skylion007 does the changes looks fine now? it should resolve the errors we were seeing.

@aditew01

aditew01 commented Jun 1, 2026

Copy link
Copy Markdown
Collaborator

CI LGTM!

@Skylion007
Skylion007 self-requested a review June 1, 2026 14:59
@Skylion007

Copy link
Copy Markdown
Collaborator

Still failing on trunk sadly.

@Skylion007

Copy link
Copy Markdown
Collaborator

Oh weird, trunk failures may ne unrelated?

@aditew01

aditew01 commented Jun 2, 2026

Copy link
Copy Markdown
Collaborator

Oh weird, trunk failures may ne unrelated?

yes, seems unrelated

@aditew01

aditew01 commented Jun 3, 2026

Copy link
Copy Markdown
Collaborator

@claude review if PR and CI runs looks fine.

@claude

claude Bot commented Jun 3, 2026 •

Copy link
Copy Markdown

Claude finished @aditew01's task in 1m 15s —— View job


Review of PR #185633

  • Read changed files and review the diff
  • Check CI status
  • Provide review feedback

Code Review

Overall: The PR looks good. It correctly addresses the C++20 deprecation of the free-function std::atomic_* overloads for shared_ptr by conditionally using the modern std::atomic<std::shared_ptr<T>> when available.

Header (rpc_agent.h)

The feature-test macro gate at lines 13-16 is correct:

#if defined(__cpp_lib_atomic_shared_ptr) && \
    __cpp_lib_atomic_shared_ptr >= 201711L
#define TORCH_RPC_HAS_ATOMIC_SHARED_PTR 1
#endif

This properly checks for the C++20 feature macro and falls back gracefully on toolchains that don't provide it (e.g., Apple's libc++ as @malfet noted). The >= 201711L check is the standard value per the spec.

Implementation (rpc_agent.cpp)

All four usage sites are correctly updated:

  1. Static member definition (line 257) — uses {nullptr} initialization for the atomic variant
  2. isCurrentRpcAgentSet() — uses .load() instead of std::atomic_load
  3. getCurrentRpcAgent() — uses .load() instead of std::atomic_load
  4. setCurrentRpcAgent() — uses .compare_exchange_strong() and .exchange() member functions instead of free functions

The semantics are preserved identically — same memory ordering (sequentially consistent by default in both APIs), same logic flow.

Minor nit: The cppreference comment link in setCurrentRpcAgent() is now split across the #if/#else branches (lines 288-295). This is slightly awkward but acceptable since the two URLs point to different pages (member vs. free function documentation).

CI Status

Workflow Status
trunk ❌ Failed (2 Windows build jobs)
Lint ✅ Passed
TSan ✅ Passed
pull ✅ Passed
BC Lint ✅ Passed

The 2 failures are win-vs2022-cpu-py3 / build and win-vs2022-cuda13.0-py3 / build — both are pre-existing "BROKEN TRUNK" failures present on the merge base (b6b9732), as confirmed by the pytorch-bot. They are unrelated to this PR's changes.

Notably, macos-py3-arm64 / build passed successfully, confirming the #ifdef gate works correctly on Apple's libc++ (which doesn't yet provide __cpp_lib_atomic_shared_ptr).

Verdict

PR looks good to merge. The code is correct, the feature gating is proper, CI failures are unrelated trunk breakages, and the macOS concern raised by @malfet is resolved by the conditional compilation approach in the second commit.


@aditew01
aditew01 requested a review from malfet June 3, 2026 11:40
@cyyever

cyyever commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

@pytorchbot rebase -b main

@cyyever

cyyever commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

@pytorchbot merge -i

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/main. Check the current status here

`std::atomic_load`, `std::atomic_exchange`, and `std::atomic_compare_exchange_*` overloads for `std::shared_ptr<T>` are deprecated since C++20. Newer GCC/libstdc++ warns on these deprecated overloads, and warning-as-error builds can turn this into a build failure.

This commit updates `RpcAgent::currentRpcAgent_` from a plain `std::shared_ptr<RpcAgent>` accessed through the deprecated free-function atomic API to `std::atomic<std::shared_ptr<RpcAgent>>` accessed through the modern member API.
Use `std::atomic<std::shared_ptr<...>>` only when the standard library
supports it, and fall back to the older shared_ptr atomic APIs otherwise.
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Successfully rebased rpc-atomic onto refs/remotes/origin/main, please pull locally before adding more changes (for example, via git checkout rpc-atomic && git pull --rebase)

@pytorch-bot

pytorch-bot Bot commented Jun 12, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@cyyever

cyyever commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

@pytorchbot merge -i

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged while ignoring the following 0 checks:

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@Anndrey24
Anndrey24 deleted the rpc-atomic branch June 12, 2026 09:32
pytorchmergebot pushed a commit that referenced this pull request Jun 16, 2026
Based on #180850, this PR updates AArch64 CI toolchains to GCC15.

The necessary GCC15 enablement fixes are part of #187167 and #185633.

Pull Request resolved: #182016
Approved by: https://github.com/aditew01, https://github.com/Skylion007

Co-authored-by: Aditya Tewari <[email protected]>
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
`std::atomic_load`, `std::atomic_exchange`, and `std::atomic_compare_exchange_*` overloads for `std::shared_ptr<T>` are deprecated since C++20 ([ref](https://en.cppreference.com/cpp/memory/shared_ptr/atomic)). Newer GCC/libstdc++ warns on these deprecated overloads, and warning-as-error builds can turn this into a build failure, as encountered in pytorch#182016.

This PR updates `RpcAgent::currentRpcAgent_` from a plain `std::shared_ptr<RpcAgent>` accessed through the deprecated free-function atomic API to `std::atomic<std::shared_ptr<RpcAgent>>` accessed through the modern member API when `__cpp_lib_atomic_shared_ptr` is available.

The implementation keeps the legacy free-function path on older standard libraries that do not provide the C++20 `std::atomic<std::shared_ptr<T>>` specialization.

cc. @aditew01 @malfet
Pull Request resolved: pytorch#185633
Approved by: https://github.com/Skylion007, https://github.com/cyyever
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
`std::atomic_load`, `std::atomic_exchange`, and `std::atomic_compare_exchange_*` overloads for `std::shared_ptr<T>` are deprecated since C++20 ([ref](https://en.cppreference.com/cpp/memory/shared_ptr/atomic)). Newer GCC/libstdc++ warns on these deprecated overloads, and warning-as-error builds can turn this into a build failure, as encountered in pytorch#182016.

This PR updates `RpcAgent::currentRpcAgent_` from a plain `std::shared_ptr<RpcAgent>` accessed through the deprecated free-function atomic API to `std::atomic<std::shared_ptr<RpcAgent>>` accessed through the modern member API when `__cpp_lib_atomic_shared_ptr` is available.

The implementation keeps the legacy free-function path on older standard libraries that do not provide the C++20 `std::atomic<std::shared_ptr<T>>` specialization.

cc. @aditew01 @malfet
Pull Request resolved: pytorch#185633
Approved by: https://github.com/Skylion007, https://github.com/cyyever
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request ciflow/vllm Merged open source release notes: distributed (rpc) release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants