Repository navigation
Running the tests in multiple threads fails because of thread-unsafe Python code #8492
Description
Activity
My thoughts.
More specifically, various registries (like these ones) are kept as global variables. Changing these in multiple threads leads to data races.
I wouldn't expect users to be changing them in multiple threads. I would imagine that for almost all scenarios, users would register their plugins and associated metadata, and then move on. We have
Image.register_*functions to add settings. We don't haveImage.unregister_*functions to remove settings. If someone found a reason to deprecate the public forms of these registries (ID,OPEN,EXTENSION, etc.) and encourage users to just use our functions instead (register_open(),register_extension(), etc.), that would sound acceptable to me.Leave them alone and document that registering plugins in multiple threads is not supported (for now?).
I think that modifying plugins is not supported across multiple threads. If multiple threads run
Image.register_open(TiffImageFile.format, TiffImageFile, _accept)
at once, that shouldn't pose a problem.
I can imagine that a user might want to load a truncated image and return to normal operations afterwards
from PIL import ImageFile ImageFile.LOAD_TRUNCATED_IMAGES = True im = Image.open(...) im.load() ImageFile.LOAD_TRUNCATED_IMAGES = False
and that might lead to unexpected results in a multithreaded environment. Perhaps we need to re-explore passing additional parameters to
Image.open(), #569, as a way for users to operate in that scenario.Similarly to plugins, I don't expect users to be registering multiple
StubHandlers. The purpose of test_file_bufrstub'stest_handler()is to assert that aStubHandlercan be registered and is used by basic image operations. I don't think it needs to test that you can register multiple handlers in separate threads and have those handlers operate in isolation.I don't expect there is a universal method to ask pytest to run a single test in a single-threaded way - you're running pytest-run-parallel, #8454 is running pytest-freethreaded - so perhaps the simplest solution to the Tests/test_file_bufrstub.py problem is to just return early if the test is already in progress on another thread. I've created #8501 as a suggestion.
Perhaps we need to re-explore passing additional parameters to Image.open(), #569, as a way for users to operate in that scenario.
This is a good idea, I'll explore this. Making these
ContextVars (or introducing new ones that people should use if they want to be changing them from multiple threads) sounds more straightforward though.We're recently merged #9945, decreasing the time it takes to run tests in our CI through
pytest-xdist.This makes me wonder. If
- we're seeing the performance benefits sharing across multiple threads in our CIs,
- we're agreed that not all Pillow functionality needs to be thread safe. Some code can be considered settings that you would not change in parallel
- and there isn't a universal method to designate a test to be single-threaded
does the problem in this issue need to be actioned any further?
Apart from the failure in #8454 and other hard crashes at the C level (I'll open separate issues for those), quite a few tests fail when run in multiple threads because of thread-unsafe Python code.
The failures come mainly from three different thread safety issues. Two of those issues come from issues in the tests themselves:
pytest.warnswhich is inherently thread-unsafe. The solution for this is to always run those tests in a single thread.uuid.uuid4()and prepending that to the filenameImage.saveis called with).The third, however, is because of thread-unsafe Python code in Pillow. More specifically, various registries (like these ones) are kept as global variables. Changing these in multiple threads leads to data races. Fixing these is going to be harder, but the possible solutions are:
ContextVars orthreading.locals. This is a backwards-incompatible change though and users that touch these in their code will have to update it to use the context variable APIs (.get()&.set()).Does anyone else have ideas regarding other ways to handle this?
An easy way to reproduce this (though this fails because of this global variable) is to install
pytest-run-paralleland then run the following under the free-threaded build:Failure info
I've verified that the following patch fixes the issue.