Skip to content

Racing condition xdist fix #273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 19, 2025
Merged

Conversation

cvanelteren
Copy link
Contributor

@cvanelteren cvanelteren commented Jun 19, 2025

Attempting to set the rng generator explicitly rather than using an integer.

Copy link

codecov bot commented Jun 19, 2025

Codecov Report

Attention: Patch coverage is 84.00000% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
ultraplot/tests/conftest.py 82.60% 0 Missing and 4 partials ⚠️

📢 Thoughts on this report? Let us know!

@cvanelteren
Copy link
Contributor Author

Resolved intermittent failures in test_contour_legend_with_label and test_contour_legend_without_label when running with pytest -n 2 by implementing process-specific temporary directories for each xdist worker. The issue was caused by multiple parallel processes writing matplotlib image comparison results to the same file paths, leading to "Image files did not match" errors due to file system race conditions.

@cvanelteren cvanelteren requested a review from Copilot June 19, 2025 10:44
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR addresses race conditions when running matplotlib image comparison tests in parallel by explicitly setting RNG generators and isolating matplotlib state per worker.

  • Updated pytest-mpl marker usage and network layout seeding
  • Introduced an autouse fixture to isolate temp directories and backends
  • Configured per-worker result paths in pytest_configure

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/test_1dplots.py Removed parentheses from @pytest.mark.mpl_image_compare and switched seed to a default_rng instance for network layouts
tests/conftest.py Added isolate_mpl_testing fixture for per-worker isolation and adjusted pytest_configure to set worker-specific result directories
Comments suppressed due to low confidence (3)

ultraplot/tests/test_1dplots.py:578

  • NetworkX layout functions expect an int or RandomState seed; passing a Generator may lead to unexpected behavior. Use seed=SEED or wrap in a RandomState object.
            layout_kw = dict(seed=np.random.default_rng(SEED))

ultraplot/tests/test_1dplots.py:528

  • Removing the parentheses might prevent pytest-mpl from applying default tolerances or baselines; consider restoring @pytest.mark.mpl_image_compare() or verifying marker behavior.
@pytest.mark.mpl_image_compare

ultraplot/tests/conftest.py:90

  • uplt is not imported in this scope, causing a NameError. Import the appropriate module or use plt instead.
    uplt.close("all")

@cvanelteren
Copy link
Contributor Author

cvanelteren commented Jun 19, 2025

Note I am not completely sure on the proposed fix here but it cannot hurt.

The three tests that are failing are:

  • ultraplot.tests.test_1dplots.test_networks
  • ultraplot.tests.test_legend.test_contour_legend_with_label
  • ultraplot.tests.test_legend.test_contour_legend_without_label

Not really sure at this moment what is causing the issue. The network one is highly likely to be caused by the rng which may be fixed in the first commit here. The other two are a bit of a mystery to me.

@beckermr
Copy link
Collaborator

I doubt the change from a seed to an RNG will help networkx, but we can try it.

@beckermr beckermr merged commit c939ae7 into Ultraplot:main Jun 19, 2025
16 of 25 checks passed
@cvanelteren
Copy link
Contributor Author

cvanelteren commented Jun 19, 2025

Ah damn was still drafting this. Couldn't hurt. Not sure why the last two tests are not working properly. It only happens on python 3.10 with mpl 3.90 I believe.

Not your fault didn't mark it as draft.

@beckermr
Copy link
Collaborator

Ah sorry. It made the failures on the other PR worse, so we could back it out and try again if you wanted.

@cvanelteren
Copy link
Contributor Author

We can leave this in as I think it improved the situation from a robustness standpoint (the tmp folders); the network seed was probable not necessary but was a sanity check on my end and the decorator had to be fixed anyway.

@cvanelteren
Copy link
Contributor Author

I probed around nx codebase and couldn't directly see how they handled if the seed was an int, hence the generator. In their codebase they immediately called .rand on the object.

cvanelteren added a commit to cvanelteren/ultraplot that referenced this pull request Jun 20, 2025
beckermr pushed a commit that referenced this pull request Jun 20, 2025
* Revert "Add xdist to image compare (#266)"

This reverts commit 500e45b.

* Revert "Racing condition xdist fix (#273)"

This reverts commit c939ae7.
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