Skip to content

Fix cache_clear behaviour in all fixtures and tests - #35746

Merged
bolkedebruin merged 1 commit into
apache:mainfrom
potiuk:fix-cache-clear-side-effect-for-test-serde
Nov 20, 2023
Merged

bolkedebruin merged 1 commit into
apache:mainfrom
potiuk:fix-cache-clear-side-effect-for-test-serde

Conversation

@potiuk

@potiuk potiuk commented Nov 19, 2023 •

Copy link
Copy Markdown
Member

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 #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_clear test 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.rst or {issue_number}.significant.rst, in newsfragments.

@potiuk

potiuk commented Nov 19, 2023

Copy link
Copy Markdown
Member Author

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

@potiuk
potiuk force-pushed the fix-cache-clear-side-effect-for-test-serde branch 4 times, most recently from 756722f to 74f50aa Compare November 19, 2023 22:41
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
@potiuk
potiuk force-pushed the fix-cache-clear-side-effect-for-test-serde branch from 74f50aa to 4145181 Compare November 19, 2023 22:43
@potiuk

potiuk commented Nov 19, 2023

Copy link
Copy Markdown
Member Author

Cliché, but yet another proof that Cache invalidation is the most .....

@amoghrajesh amoghrajesh left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Elegant fix. LGTM +1

Comment thread tests/conftest.py
Comment on lines +1078 to +1085
try:
_get_grouped_entry_points.cache_clear()
try:
yield
finally:
_get_grouped_entry_points.cache_clear()
finally:
ExecutorLoader.validate_database_executor_compatibility.cache_clear()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Super interesting pattern and the reasoning for it :D

@bolkedebruin

Copy link
Copy Markdown
Contributor

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.

@bolkedebruin
bolkedebruin merged commit 7e7ac10 into apache:main Nov 20, 2023
@potiuk

potiuk commented Nov 20, 2023 •

Copy link
Copy Markdown
Member Author

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 pytest-xdist. This is the same for any other pytest execution - simply in pytest-xdist you are less reliant on specific execution sequence of the tests - so it is just more likely to happen in a flaky way. When you run things in a single pytest command - the execution sequence of the test is deterministic (alphabetical sequence of packages, modules, test classes and tests) - which means that you might never found out you had the problem (in case of #35699 the thing was that the last test case in the test suite for serde did not have special configuratin but it DID have the fixture applied - so it re-read the cache from "original" configiuration - thus any tests that were run before or after had "no pollution" (but this was accidental).

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

@ephraimbuddy ephraimbuddy added this to the Airflow 2.8.0 milestone Nov 20, 2023
@potiuk
potiuk deleted the fix-cache-clear-side-effect-for-test-serde branch April 21, 2026 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky TestSerializers.test_params test

4 participants