Skip to content

Fix deadlock caused by mx.clear_streams() holding GIL - #4413

Merged
zcbenz merged 2 commits into
ml-explore:mainfrom
aleroot:alessio/fix-clear-streams-gil
Aug 27, 2026
Merged

Fix deadlock caused by mx.clear_streams() holding GIL#4413
zcbenz merged 2 commits into
ml-explore:mainfrom
aleroot:alessio/fix-clear-streams-gil

Conversation

@aleroot

@aleroot aleroot commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

This fix the deadlock experienced in the tests. See #4410 for more information.
Added a regression test which proves it, and added even an additional fix in another point it could have backfired as well.

Notes

macOS CI sets METAL_DEVICE_WRAPPER_TYPE: 1 which changes the timing and contributes to the fact works on my machine but not on CI...

  • ☑️ I understand it is strictly prohibited to use AI to write PR description
  • AI usage disclosure:

@zcbenz
zcbenz marked this pull request as ready for review August 27, 2026 03:16

@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 a lot for the fix!

Comment thread .github/workflows/build_and_test.yml Outdated
Comment thread python/tests/test_zero_copy.py Outdated
# in Python would never run.
def test_synchronize_releases_gil(self):
a = self._adopted_source(1024)
faulthandler.dump_traceback_later(120, exit=True)

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 can use a much shorter timeout here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I will update once arrived to the office

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Picked 30 seconds. The submitted work (a handful of 1024×1024 matmuls) finishes in well under a second on any hardware, so 30 seconds still leaves generous headroom on heavily loaded CI runners. Let me know if it is fine.

@zcbenz zcbenz changed the title Fix a deadlock after test succeed due to mx.clear_streams() holding GIL while synchronously waiting for Metal Fix deadlock caused by mx.clear_streams() holding GIL Aug 27, 2026
@zcbenz
zcbenz merged commit 1e2c0ca into ml-explore:main Aug 27, 2026
29 checks passed
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