Skip to content

chore: move array definitions inside test functions in test_1490_jax_reducers_combinations.py#3829

Merged
ikrommyd merged 2 commits intoscikit-hep:mainfrom
ikrommyd:move-arrays-in-test-function-jax
Feb 2, 2026
Merged

chore: move array definitions inside test functions in test_1490_jax_reducers_combinations.py#3829
ikrommyd merged 2 commits intoscikit-hep:mainfrom
ikrommyd:move-arrays-in-test-function-jax

Conversation

@ikrommyd
Copy link
Copy Markdown
Collaborator

In #3816 (comment), we encountered a failure during multi-threaded testing that went away with a re-run.
We hit this error in jax https://github.com/jax-ml/jax/blob/0d34e25aecf6466f9014186a2175f56f52106163/jax/_src/array.py#L498-L502
in pytest-run-parallel, things defined at module level are shared among threads (and therefore those arrays are too).
In case there is a mutable state somewhere, it's best to define arrays inside the test functions so that they are not shared among threads. We do just that here.

@codecov
Copy link
Copy Markdown

codecov bot commented Jan 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.65%. Comparing base (53744b2) to head (42a54f0).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files

see 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions
Copy link
Copy Markdown

The documentation preview is ready to be viewed at http://preview.awkward-array.org.s3-website.us-east-1.amazonaws.com/PR3829

Copy link
Copy Markdown
Member

@ianna ianna left a comment

Choose a reason for hiding this comment

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

@ikrommyd - it looks like the two high level arrays that supposed to be immutable have been moved — does it mean that awkward is not thread-safe?

@ikrommyd
Copy link
Copy Markdown
Collaborator Author

ikrommyd commented Jan 28, 2026

@ikrommyd - it looks like the two high level arrays that supposed to be immutable have been moved — does it mean that awkward is not thread-safe?

The high-level arrays are "kinda" mutable when it comes to __setitem__ (which uses ak.with_field). Also the python layouts are immutable but their buffers are not. And we are not in control of what jax does to the buffers internally.

Well awkward is not thread-safe in the same sense that numpy is not thread-safe. You can have multiple threads modifying a buffer and there are no locks inside numpy protecting you. The python part of awkward should be thread safe but that doesn't mean you should share arrays between threads and start doing in place mutations to the buffers.

Also the issue may be from jax here. I don't think you should spawn threads that do stuff to the same jax buffer either. We hit the error when we tried to get the unsafe_buffer_pointer of a jax buffer because something caused the buffer to be "sharded". The buffers themselves are not thread-safe if multiple threads try to do something to them.

Awkward is thread safe in the sense that you can spawn multiple python threads running awkward code without a problem. That's what we test in the test with pytest-run-parallel (that we can run the test functions in parallel without a problem). This is the same kind of testing that numpy has and the same "thread-safety".

https://numpy.org/doc/stable/reference/thread_safety.html

Copy link
Copy Markdown
Member

@ianna ianna left a comment

Choose a reason for hiding this comment

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

@ikrommyd - Thanks for detailed expalnation! Please, merge it if you finished with it. Thanks!

@ikrommyd ikrommyd merged commit 1987142 into scikit-hep:main Feb 2, 2026
40 checks passed
@ikrommyd ikrommyd deleted the move-arrays-in-test-function-jax branch February 3, 2026 17:54
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