Lock around usages of imaging memory arenas by lysnikolaou · Pull Request #8238 · python-pillow/Pillow
Out of curiosity, how are you testing with tsan? I don't think we have it in CI (although perhaps we should add it!).
I added a few very very basic threaded tests, built CPython and Pillow with tsan enabled and then ran the tests. It all takes forever, cause CPython with tsan is very slow, but it works.
If your question is how I build Pillow with tsan enabled, it is really easy:
CC="clang -fsanitize=thread" python -m pip install .