Skip to content

Make the scheduler's active task count atomic - #4882

Merged
zcbenz merged 1 commit into
ml-explore:mainfrom
ipazarbasi:scheduler-nactive-race
Nov 21, 2026
Merged

zcbenz merged 1 commit into
ml-explore:mainfrom
ipazarbasi:scheduler-nactive-race

Conversation

@ipazarbasi

@ipazarbasi ipazarbasi commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

n_active_tasks() reads n_active_tasks_ without the mutex while stream threads update it under the lock. The unlocked read from eval_impl is a data race.

Needs TSan-instrumented tests to verify.

  • ☑️ I understand it is strictly prohibited to use AI to write PR description
  • AI usage disclosure: AI addressed the feedback and added new tests.

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we should not really expose n_active_tasks() as a public method since it is going to cause race condition when called separately from wait_for_one(), even with the fix in this PR. Can you move the code in

mlx/mlx/transforms.cpp

Lines 323 to 337 in 8f0f391

if (scheduler::n_active_tasks() > MAX_ACTIVE_TASKS ||
(get_active_memory() > get_memory_limit() &&
scheduler::n_active_tasks() > 0)) {
// Commit any open streams
for (auto& s : open_streams) {
if (s.device == Device::gpu) {
gpu::finalize(s);
}
}
scheduler::wait_for_one();
while (get_active_memory() > get_memory_limit() &&
scheduler::n_active_tasks() > 0) {
scheduler::wait_for_one();
}
}
to a method of Scheduler and add proper locks?

Also since we are going to use std::atomic for n_active_tasks_, there is not much need for completion_cv and mtx, you can use the notification APIs of std::atomic for the same purpose.

@zcbenz
zcbenz marked this pull request as ready for review November 20, 2026 04:10
@ipazarbasi

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback. I will work on this. I can remove the mutex and the condition variable now. When I discovered the race, I didn't want to make changes that could affect ordering.

@ipazarbasi
ipazarbasi force-pushed the scheduler-nactive-race branch from 9f235c1 to fe114cc Compare November 20, 2026 05:09
Comment thread mlx/scheduler.cpp Resolved
@ipazarbasi
ipazarbasi force-pushed the scheduler-nactive-race branch from fe114cc to c9bf935 Compare November 20, 2026 06:58
`n_active_tasks()` reads `n_active_tasks_` without the mutex while
stream threads update it under the lock. The unlocked read from
eval_impl was a data race.

This patch makes `n_active_tasks_` an atomic, removes the mutex,
condition variable, and wait_for_one function.
@ipazarbasi
ipazarbasi force-pushed the scheduler-nactive-race branch from c9bf935 to aff8de3 Compare November 21, 2026 05:24
@ipazarbasi
ipazarbasi requested a review from zcbenz November 21, 2026 06:30

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the nice fix & refactoring!

@zcbenz
zcbenz merged commit a3a610b into ml-explore:main Nov 21, 2026
24 checks passed
rcfa pushed a commit to cubiculum-ai/mlx that referenced this pull request Nov 26, 2026
Ported: mlx/scheduler.h is Apple's in full. The fork's test "test
concurrent scheduler active task observations" (osaurus-ai/mlx PR 15) is
removed: it calls n_active_tasks() and wait_for_one(), which this commit
removes.

(cherry picked from commit a3a610b)
Brooooooklyn added a commit to mlx-node/mlx that referenced this pull request Nov 29, 2026
…tive tracer

One env var, read once as a level; unset or 0 is off and costs one
member load per eval.
  1  [metal-command] JSON per committed command buffer and [mlx-evaluation]
     JSON per eval_impl
  2  also [metal-op] <primitive name> per GPU primitive
  3  also its input -> output dtypes

Adapted for upstream ml-explore#4647 (get_default_max_ops_mb_per_buffer + env::*),
ml-explore#4711 (verify_graph), ml-explore#4882 (Scheduler::wait_for_capacity overload), and
ml-explore#5004 (tracing_counter).
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