Skip to content

Fix Metal command queue retention when a worker thread initializes MLX - #4936

Merged
zcbenz merged 3 commits into
ml-explore:mainfrom
oxiglade:fix/metal-worker-encoders
Nov 23, 2026
Merged

zcbenz merged 3 commits into
ml-explore:mainfrom
oxiglade:fix/metal-worker-encoders

Conversation

@dcvz

@dcvz dcvz commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

When a worker is the first thread to use MLX, is_main_thread() remembers that worker's thread ID. After the worker exits, macOS can reuse the ID. get_command_encoders() can then put later workers' Metal command encoders in the process-lifetime map instead of their thread-local maps. Their command queues stay alive after the workers exit.

Updates is_main_thread() to check for the process main thread on Apple platforms and Linux.

@dcvz
dcvz force-pushed the fix/metal-worker-encoders branch 2 times, most recently from e72b8e4 to 31f0853 Compare November 21, 2026 18:11
@dcvz
dcvz force-pushed the fix/metal-worker-encoders branch from 31f0853 to 2707e2d Compare November 21, 2026 18:15

@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.

Didn't know the API and it makes things much more reliable! Can you use it to implement the is_main_thread utility instead?

@zcbenz zcbenz added the await response This pull request is waiting for response from the author. label Nov 23, 2026
@dcvz

dcvz commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Done. Moved pthread_main_np() into is_main_thread() under #if defined(__APPLE__), so non-Apple builds keep their thread-id comparison fallback.

@dcvz
dcvz force-pushed the fix/metal-worker-encoders branch from cdbde11 to a646ddc Compare November 23, 2026 07:08
@zcbenz zcbenz removed the await response This pull request is waiting for response from the author. label Nov 23, 2026

@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 update, please consider opening PRs from personal accounts in future because we are unable to rebase or edit the PR from org account even when you enabled it.

Comment thread mlx/backend/metal/device.cpp Resolved
Comment thread mlx/utils.cpp Resolved
@dcvz

dcvz commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@zcbenz unfortunately pthread_main_np isn't as widely available as one would hope, but you were right on trying to handle linux better. I've updated the approach to also have a more accurate check for linux.

@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.

This is a lot better than what we did, thanks for the nice work!

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