Repository navigation
Fix cache_clear behaviour in all fixtures and tests - #35746
Conversation
|
Very insteresting finding :). It took me a while to figure out the exact scenario where #35699 could have happen -but I believe this one perfectly explains it. cc: @bolkedebruin |
756722f to
74f50aa
Compare
When running test with pytest-xdist, tests can be executed in random order in multiple workers. This means that if a test leaves a side-effect, it might affect another test - much more easily, because you are not able to rely on the sequence of test execution. The `recalculate_patterns` fixture in "test_serde" had such a side effect and caused apache#35699 to happen. It was used in order to clear the cache BEFORE the test so that it could reload the patterns using different configugration provided by the test as new configuration. The side effect of it was however such, that after the test completed, the new configuration remained in the cache AFTER the test has been completed - thus if THE SAME xdist WORKER started a test that relied on the cache containting "regular" value (and it did not use the same fixtuere to clean it), it - surpisingly - got the value that was read from the special configuration provided by the previous test. This PR fixes it by clearing the cache also AFTER the test has completed, which means that any other test running in the same worker will refresh the test again - with the configuration it is supposed to have. This is done with try/finally pattern, because context manager / yield will not execute the post-yield code when an exception is thrown, so if there is a failed assertion, the cache_clear afer yield might not happen and such `cache_clear` test that fails might still cause cache pollution. Fixes: apache#35699
74f50aa to
4145181
Compare
|
Cliché, but yet another proof that |
| try: | ||
| _get_grouped_entry_points.cache_clear() | ||
| try: | ||
| yield | ||
| finally: | ||
| _get_grouped_entry_points.cache_clear() | ||
| finally: | ||
| ExecutorLoader.validate_database_executor_compatibility.cache_clear() |
There was a problem hiding this comment.
Super interesting pattern and the reasoning for it :D
|
Ah indeed, I was wondering how the pollution could happen at all. I wasn't aware of pytest-xdist. It feels to me like a bug in pytest-xdist, but this is a good fix. |
Just to explain. It's not bug in pytest nor pytest-xdist, It was OUR bug in OUR fixtures coming from lack of understanding that this kind of change has side effects in global shared state which is reused between tests run with pytest (or pytest-xdist). So it's not a "good" fix. It's proper way of writing such fixtures. It's not exclusive to Simply the fact that pytest-xdist i slightly less deterministic revealed the problem - but it's not "pytest-xdist" bug. It's just the way how tests work - generally in Pytest (and most other test frameworks) tests are run in the same python interpreter, one after the other - so such cache pollution might happen (this is what for example pytest-antilru goal is to remove that side effect - it attempts to remove the cache altogether so that side effects are not carried through other tests. But it either has bugs or we have other side effects because when I turn it on it breaks few tests (did not have time to take a look). If you would like to avoid (some, not all) side effects you would have to really fork every single test method to separate python interpreter and do it before any other initialization of any other python packages happen - but that would be very, very slow (3000 tests * (even) 0.1 s fork = 300 seconds - but that does not include the time for initialization of other package/iimports). Also it does not remove all side-effects, for example you can store global state in disk or shared memory or shared state might be present in the form of spawned/forked other processes etc. ec. There are plenty of other way you can have global shared state that some tests implicitly rely on but some other tests are changing that state and they do not clean-up after themselves (as they should). In this case pytest (and other test frameworks of this kind) leave the responsibility with cleaning/restoring the shared global state to the tests (this is what setup/teardown and pytest fixtures are supposed to do). So while the setting where it all runs in a single interpreter (or in case of pytest-xdist - in several python interpreters that run in parallel) is not perfect (it cannot be) it's also "good enough" balance between speed and isolation in most cases as long as you remember to not modify (or if you remember to restore) shared global state in the tests. In our case it was evidently a bug in our fixtures implementation - not in pytest, nor in pytest-xdist. Simply, it violates the (implicit) agreement that when you change global state in tests that other tests might rely on, we should restore that global state after the test. In this case - we changed shared global state of the cache and we did not restore it properly. That was our bug not pytest, nor pytest-xdist. It comes from not understanding the side effect such shared global state might have. Most of flaky test are result of similar bugs. BTW. I am actually planning - this is the best practice for flaky tests and stability - to run the tests in randomized order https://pypi.org/project/pytest-random-order/ is one of the plugins - initially for NonDB tests. It is however a bit problematic, because when you randomize the tests then any PR might introduce a side effect that will only be visible when you run the tests in other order, so it's a bit tricky to get right, but it has the potential to uncover and tame the flaky tests we currently have. The nice thing about those random order plugins is that when they fail, you can run them with the same seed when the tests failed, that increases the chances of reproducing the flaky tests (but combined with pytest-xdist it also add some randomness due to parallell/distributed execution). For those who are interested - I heartily recommend that recent podcast from Talk Python To me - "Tamiing Flaky tests" - where a lot of that is explained and there are some interesting techniques (which I am gradually employing) on how to deal with the flaky tests https://talkpython.fm/episodes/show/429/taming-flaky-tests |
When running test with pytest-xdist, tests can be executed in random order in multiple workers. This means that if a test leaves a side-effect, it might affect another test - much more easily, because you are not able to rely on the sequence of test execution.
The
recalculate_patternsfixture in "test_serde" had such a side effect and caused #35699 to happen.It was used in order to clear the cache BEFORE the test so that it could reload the patterns using different configugration provided by the test as new configuration.
The side effect of it was however such, that after the test completed, the new configuration remained in the cache AFTER the test has been completed - thus if THE SAME xdist worker started ANOTHER test that relied on the cache containting "regular" value (and it did not use the same fixtuere to clean it), it - surpisingly - got the value that was read from the special configuration provided by the previous test.
This PR fixes it by clearing the cache also AFTER the test has completed, which means that any other test running in the same worker will refresh the test again - with the configuration it is supposed to have. This is done with try/finally pattern, because context manager / yield will not execute the post-yield code when an exception is thrown, so if there is a failed assertion, the cache_clear afer yield might not happen and such
cache_cleartest that fails might still cause cache pollution.Fixes: #35699
^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named
{pr_number}.significant.rstor{issue_number}.significant.rst, in newsfragments.