Repository navigation
Closed as not planned
Labels
plugin: warningsrelated to the warnings builtin pluginstatus: needs informationreporter needs to provide more information; can be closed after 2 or more weeks of inactivity
Description
I'm a Dask maintainer and I'm currently working on 3.14t support.
My test suite runs Dask with the default threading scheduler on 3.14t.
Dask spawns a thread, logs a RuntimeWarning inside the thread, and then joins it.
On all other Python interpreters, @pytest.mark.filterwarnings successfully suppresses the warning.
On Python 3.14t, if any other test previously spawned and rejoined dask threads, the warning is not suppressed.
Reproducer
import dask.array as da
import pytest
def test_1():
a = da.zeros(1)
a.compute()
@pytest.mark.filterwarnings("ignore:invalid value encountered in divide:RuntimeWarning")
def test_2():
a = da.zeros(1) / da.zeros(1)
a.compute()Outcome:
- Python 3.14.2,
pytest: OK - Python 3.14.2t,
pytest -k test_2: OK - Python 3.14.2t,
pytest:
FAILED test1.py::test_2 - RuntimeWarning: invalid value encountered in divide
- Python 3.14.2t,
pytest, changecompute()tocompute(scheduler="sync"): OK
I've failed to further simplify the reproducer further. This works fine:
import numpy as np
import pytest
import threading
import dask.array as da
def test_1():
a = da.zeros(1)
a.compute()
@pytest.mark.filterwarnings("ignore:invalid value encountered in divide:RuntimeWarning")
def test2():
t = threading.Thread(target=np.divide, args=(0, 0))
t.start()
t.join()If I merge the two tests, ending up with a single test that spawns and destroys the dask thread pool twice, the issue disappears. This works fine:
import dask.array as da
import pytest
@pytest.mark.filterwarnings("ignore:invalid value encountered in divide:RuntimeWarning")
def test_1():
a = da.zeros(1)
a.compute()
a = da.zeros(1) / da.zeros(1)
a.compute()Environment
pytest 9.0.2
numpy 2.4.0
dask 2025.12.0
Python 3.14.2
Metadata
Metadata
Assignees
Labels
plugin: warningsrelated to the warnings builtin pluginstatus: needs informationreporter needs to provide more information; can be closed after 2 or more weeks of inactivity
Activity
Zac-HD commentedon Jan 3, 2026
tagging @Liam-DeVoe if you're still looking at threading?
RonnyPfannschmidt commentedon Jan 4, 2026
This looks like a threading vs warnings global state issue
Liam-DeVoe commentedon Jan 5, 2026
This is an interaction with
-X context_aware_warnings, enabled by default in 3.14t. When a thread is shared across tests, updates to the warnings filter underwith catch_warnings()don't propagate to the existing thread.with warnings.catch_warnings()from@pytest.mark.filterwarningsHere's a minimal reproducer:
pytest could recommend that threads are not shared across tests? This does seem unfortunate.
(cc @ngoldbaum)
ngoldbaum commentedon Jan 5, 2026
Hmm, I think Dask is going to need to do something here to update the warnings state in its thread pool when it re-uses workers. I'll surface this internally in our group...
RonnyPfannschmidt commentedon Jan 5, 2026
Im going to consider this a upstream integration issue for cpython/ dask for now
crusaderky commentedon Jan 5, 2026
Yes, threads remain alive across tests:
https://github.com/dask/dask/blob/8d2cc26e68e300e9cb78e838684ad5fcd8d0948f/dask/threaded.py#L70-L81
I guess that to cope with this I could change the module linked above as follows? I'm not too happy to reference CPython internals though...
ngoldbaum commentedon Jan 5, 2026
@nascheme probably has insight about what is appropriate to do here
nascheme commentedon Jan 5, 2026
Possible solution, add the warnings filter decorator to the function being called by the worker, rather than inside the function that runs in the parent, e.g.
This seems to work in 3.14t. Mutating the warning state of another thread doesn't seem like a good solution (hard to make thread-safe). Another option would be to make a
ThreadPoolExecutorsubclass that takes the warning state from the calling thread, pass it along with the task to run in the worker, establish the warnings context before calling the submitted function. I think all that could be done without modifying the test code, just change the ThreadPoolExecutor class being used. If it can be made both in free-threaded and default builds, decorating the submitted function seems like the cleanest solution to me.nascheme commentedon Jan 5, 2026
Oh, that doesn't work the same. When using the
-Werroroption, the test fails while in the default build the test passes. It seems the@pytest.mark.filterwarningsdecorator doesn't work on a nested function like that. I'm not sure if it could be made to work, looking into it.nascheme commentedon Jan 5, 2026
I think this works:
crusaderky commentedon Jan 6, 2026
This is not practically feasible in a dask.array, dask.dataframe, or dask.bag workflow.
The final user provides the data which triggers the warning, but has no direct control on the internals of the function that is being run inside the thread pool.
The initial example was a good real life use case of what is exposed to the final user:
To make it realistic, you just replace
dask.zeroswith an arbitrarily complicated chain of operations that ends up with a zero somewhere in the denominator.The user doesn't invoke
np.ndarray.__truediv__; they invokeda.Array.__truediv__which in turn will later causecompute()to internally callnp.ndarray.__truediv__in a thread.This is feasible. I'll play around with it.
crusaderky commentedon Jan 6, 2026
Dask PR: dask/dask#12224
Closing this, as the issue is clearly not about pytest anyway.
Thanks for the help everybody!