Skip to content

Conversation

pankaj-bind
Copy link

Problem

When running tests in parallel with pytest-xdist, the @pytest.mark.urls fixture can cause a race condition. Multiple test workers may try to modify the global ROOT_URLCONF and clear the URL cache at the same time, leading to unpredictable behavior and intermittent test failures.

Solution

This change introduces a threading.Lock() around the URL configuration and cache-clearing logic in the _django_set_urlconf fixture. This ensures that these operations are atomic, preventing multiple threads from interfering with each other and making the URL cache clearing thread-safe.

Copy link
Member

@kingbuzzman kingbuzzman left a comment

Choose a reason for hiding this comment

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

If your test is anything to go off of, this does not show a problem currently in the code as it stand. Can you elaborate more?

pytest.importorskip("xdist")

django_pytester.makepyfile(
empty="urlpatterns = []",
Copy link
Member

Choose a reason for hiding this comment

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

Not used

Suggested change
empty="urlpatterns = []",

ROOT_URLCONF = "empty"
"""
)
def test_urls_concurrent(django_pytester: DjangoPytester) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

I copied this test over to my main branch, and ran this on py3.13

count=0
while true; do
    count=$((count + 1))
    if ! pytest tests/test_urls.py -vv > /dev/null 2>&1; then
        echo "Test failed on iteration $count"
        break
    fi
    printf "."
done

it has not failed -- keeps going, after 100+ iterations (keep in mind, i only copied the test, not the "fix")

Copy link
Member

Choose a reason for hiding this comment

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

PS. I also ran this with pytest -n auto (same loop as above) -- no difference, i dont see the race condition

@kingbuzzman kingbuzzman marked this pull request as draft August 13, 2025 18:21
@bluetech
Copy link
Member

pytest-xdist runs workers in separate processes, not threads. So it's unclear what this is intended to fix.

@kingbuzzman
Copy link
Member

@pankaj-bind I don't want to discourage you, what is the issue you're trying to solve? we can help you with it.

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.

3 participants