Skip to content

[wip][core] Relieve lock contention on reference removal by offloading to the IO thread - #63049

Closed
Yicheng-Lu-llll wants to merge 6 commits into
ray-project:masterfrom
Yicheng-Lu-llll:offload-ref-cleanup-to-io-thread
Closed

Yicheng-Lu-llll wants to merge 6 commits into
ray-project:masterfrom
Yicheng-Lu-llll:offload-ref-cleanup-to-io-thread

Conversation

@Yicheng-Lu-llll

@Yicheng-Lu-llll Yicheng-Lu-llll commented Apr 30, 2026 •

Copy link
Copy Markdown
Member

Description

When an object's reference count drops to zero, the driver publishes two notices to the remote raylets that care about it:

  • the raylet pinning the object's primary copy can now free that memory(with Aaron's change, it will send to all nodes that has copy.)
  • any raylet still waiting to learn where the object lives should give up, because the object is gone.

Both are published synchronously on the driver's Python thread and contend for the publisher's lock.

On the map-batches benchmark, py-spy native sampling put 3,556 of about 20,000 scheduling-loop samples (17.4%) on that lock, with a further 3.3% on the reference counter's lock as a knock on.

The proposal is to post the two notices asynchronously on the core worker's IO context instead.

Advantage

  • Solves the lock contention
  • voids any potential deadlock between the python and c++ threads
  • If this adds pressure on the C++ core worker, we can focus on splitting the C++ worker into multiple threads without having to worry about the Python thread
  • We later even want to move all these kind of operations that run on python thread needs lock, can do async, to past to c core woer thread,

Correctness

I think the argument here is that we already handle "ref counting goes to zero" asynchronously in a lot of places. Like Ed said, our GC now checks for cycles from time to time, and if there is one, it deletes the ref, which can then trigger ref counting going to zero, so that is already async.

And if the last ref to this object is not on the owner but on a remote worker node, then when the subscription notification tells us the remote worker has also deleted its ref, we already put "ref counting goes to zero" on the C core worker thread. So this change actually makes us more uniform.

perf

We could verify that the lock contention disappears and save the 17.4% times.

Note that posting to the IO context turns out to be really cheap, with no serious lock contention there.

…thread

Signed-off-by: yicheng <yicheng@anyscale.com>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request updates the RemoveLocalReference method in CoreWorker to execute its logic asynchronously on an IO thread, which is intended to reduce lock contention. A critical safety concern was raised regarding the use of a raw this pointer within the asynchronous lambda, as it could lead to a use-after-free error if the CoreWorker instance is destroyed before the task executes. The reviewer suggested capturing shared_from_this() to safely extend the object's lifetime during the operation.

Comment thread src/ray/core_worker/core_worker.h Resolved
@Yicheng-Lu-llll
Yicheng-Lu-llll marked this pull request as ready for review May 12, 2026 19:49
@Yicheng-Lu-llll
Yicheng-Lu-llll requested a review from a team as a code owner May 12, 2026 19:49
@Yicheng-Lu-llll Yicheng-Lu-llll changed the title [core] Relieve lock contention on reference removal by offloading to the IO thread [wip][core] Relieve lock contention on reference removal by offloading to the IO thread May 12, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit a1c06bf. Configure here.

Comment thread src/ray/core_worker/core_worker.h Resolved
Comment thread src/ray/core_worker/core_worker.h Resolved
@ray-gardener ray-gardener Bot added the core Issues that should be addressed in Ray Core label May 13, 2026
@github-actions

github-actions Bot commented Jun 1, 2026

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had
any activity for 14 days. It will be closed in another 14 days if no further activity occurs.
Thank you for your contributions.

You can always ask for help on our discussion forum or Ray's public slack channel.

If you'd like to keep this open, just leave any comment, and the stale label will be removed.

@github-actions github-actions Bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Jun 1, 2026
@Yicheng-Lu-llll Yicheng-Lu-llll added the go add ONLY when ready to merge, run all tests label Jun 4, 2026
@github-actions github-actions Bot added unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it. and removed stale The issue is stale. It will be closed within 7 days unless there are further conversation labels Jun 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant