question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incompatible with pytest-xdist / thread safety

See original GitHub issue

Describe the bug

I’m using pytest-xdist to run my tests in parallel (mostly a single test parameterized for a vast amount of different test data). Since I wanted to try snapshot-based testing, I’ve added syrupy to the mix. After initial creation of __snapshots__/mytest.ambr (using the --snapshot-update), it disappears from disk at some point during the test run. Without pytest-xdist (i.e. without using -n auto option) this doesn’t happen.

To reproduce

I don’t have a clean reproduction, only an observation. My setup is a single test for a bit of processing, parameterized with a couple of hundreds different input files. While the .ambr file appear initially, after about 80% of tests done, the file disappears.

Expected behavior

Snapshot files get updated with correct data without disappearing, even when used with pytest-xdist or pytest-parallel.

Environment:

  • OS: Ubuntu 20.04
  • Syrupy Version: 1.4.0
  • Python Version: 3.8

Additional context

I assume the problem happens because each test execution writes to the file individually and thus a race condition happens. Not sure what can be done about it, maybe buffer the snapshot file somewhere and write it only at the end of the test run? There are surely better ideas. I assume that e.g. the JUnit reporter works correctly with pytest-xdist, maybe check what it does?

Either way, I wanted to bring this to your attention, would be happy if there was a short- to middle-term solution. If not, I’ll run the suite without parallelization, it’s not that big of a deal at the moment, at least for me 😃

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:15 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
noahnucommented, Sep 3, 2022

Taking a stab at this over the weekend! ⚒️

@mcataford you’ll want to work off of the next branch. I was running into performance issues which will need to be tackled first. We’re running benchmarks against the main branch, so you can use those benchmarks as a reference.

1reaction
noahnucommented, Feb 4, 2022

Will try take a deeper look this weekend but what needs to be clarified:

  • How do we know when xdist is done and wrapping up? Is there a pytest hook?
  • How can we communicate the snapshots we want to write to a single worker/controller? Is there a built in communication mechanism?

If I can’t solve it immediately, I’ll do some refactoring in syrupy to hopefully make it easier to reason about. I’ve been meaning to do some serious refactoring to the syrupy internals for a while and have been planning it for a v2 release since it’ll affect plugin development.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is pytest-xdist thread safe with singleton - Stack Overflow
I want to implement the same behavior in Python. For the singleton, I can use a MetaClass. To run the test in paralel,...
Read more >
Session-Scoped Fixtures are not Session-Scoped with Pytest ...
I am fairly new to this project. I very recently migrated a software project test-suite from nosetest to pytest, mainly because of the...
Read more >
Changelog — pytest documentation
<patch> ). Backward incompatible (breaking) changes will only be introduced in major versions with advance notice in the Deprecations section of releases.
Read more >
pytest Documentation - Read the Docs
When distributing tests on the local machine using pytest-xdist, care is taken to automatically configure a basetemp.
Read more >
pytest-rerunfailures - Python Package Health Analysis - Snyk
pytest plugin to re-run tests to eliminate flaky failures For more information ... This plugin is not compatible with pytest-xdist's --looponfail flag.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found