-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Use zarr-fixture to prevent thread leakage errors #9967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kmuehlbauer
merged 6 commits into
pydata:main
from
kmuehlbauer:fix-dask-zarr-integration-test
Jan 22, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5fb6db0
Use zarr-fixture to prevent thread leakage errors
kmuehlbauer 5559043
Apply suggestions from code review
kmuehlbauer 739310f
Add whats-new.rst entry
kmuehlbauer 1631e15
Merge branch 'main' into fix-dask-zarr-integration-test
kmuehlbauer bc79046
Explicitely add pyarrow to windows builds, as importing dask.datafram…
kmuehlbauer 2658c7c
Merge branch 'main' into fix-dask-zarr-integration-test
kmuehlbauer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,9 @@ | |
from distributed.client import futures_of | ||
from distributed.utils_test import ( # noqa: F401 | ||
cleanup, | ||
client, | ||
cluster, | ||
cluster_fixture, | ||
gen_cluster, | ||
loop, | ||
loop_in_thread, | ||
|
@@ -46,6 +48,7 @@ | |
from xarray.tests.test_dataset import create_test_data | ||
|
||
loop = loop # loop is an imported fixture, which flake8 has issues ack-ing | ||
client = client # client is an imported fixture, which flake8 has issues ack-ing | ||
|
||
|
||
@pytest.fixture | ||
|
@@ -214,35 +217,61 @@ def test_dask_distributed_read_netcdf_integration_test( | |
assert_allclose(original, computed) | ||
|
||
|
||
# fixture vendored from dask | ||
# heads-up, this is using quite private zarr API | ||
# https://github.com/dask/dask/blob/e04734b4d8959ba259801f2e2a490cb4ee8d891f/dask/tests/test_distributed.py#L338-L358 | ||
@pytest.fixture(scope="function") | ||
def zarr(client): | ||
zarr_lib = pytest.importorskip("zarr") | ||
# Zarr-Python 3 lazily allocates a dedicated thread/IO loop | ||
# for to execute async tasks. To avoid having this thread | ||
# be picked up as a "leaked thread", we manually trigger it's | ||
# creation before using zarr | ||
try: | ||
_ = zarr_lib.core.sync._get_loop() | ||
_ = zarr_lib.core.sync._get_executor() | ||
Comment on lines
+231
to
+232
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eyes wide open here. This is quite private API. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this to the note, thanks for making this clear! |
||
yield zarr_lib | ||
except AttributeError: | ||
yield zarr_lib | ||
finally: | ||
# Zarr-Python 3 lazily allocates a IO thread, a thread pool executor, and | ||
# an IO loop. Here we clean up these resources to avoid leaking threads | ||
# In normal operations, this is done as by an atexit handler when Zarr | ||
# is shutting down. | ||
try: | ||
zarr_lib.core.sync.cleanup_resources() | ||
except AttributeError: | ||
pass | ||
|
||
|
||
@requires_zarr | ||
@pytest.mark.parametrize("consolidated", [True, False]) | ||
@pytest.mark.parametrize("compute", [True, False]) | ||
def test_dask_distributed_zarr_integration_test( | ||
loop, consolidated: bool, compute: bool | ||
client, | ||
zarr, | ||
consolidated: bool, | ||
compute: bool, | ||
) -> None: | ||
if consolidated: | ||
write_kwargs: dict[str, Any] = {"consolidated": True} | ||
read_kwargs: dict[str, Any] = {"backend_kwargs": {"consolidated": True}} | ||
else: | ||
write_kwargs = read_kwargs = {} | ||
chunks = {"dim1": 4, "dim2": 3, "dim3": 5} | ||
with cluster() as (s, [a, b]): | ||
with Client(s["address"], loop=loop): | ||
original = create_test_data().chunk(chunks) | ||
with create_tmp_file( | ||
allow_cleanup_failure=ON_WINDOWS, suffix=".zarrc" | ||
) as filename: | ||
maybe_futures = original.to_zarr( # type: ignore[call-overload] #mypy bug? | ||
filename, compute=compute, **write_kwargs | ||
) | ||
if not compute: | ||
maybe_futures.compute() | ||
with xr.open_dataset( | ||
filename, chunks="auto", engine="zarr", **read_kwargs | ||
) as restored: | ||
assert isinstance(restored.var1.data, da.Array) | ||
computed = restored.compute() | ||
assert_allclose(original, computed) | ||
original = create_test_data().chunk(chunks) | ||
with create_tmp_file(allow_cleanup_failure=ON_WINDOWS, suffix=".zarrc") as filename: | ||
maybe_futures = original.to_zarr( # type: ignore[call-overload] #mypy bug? | ||
filename, compute=compute, **write_kwargs | ||
) | ||
if not compute: | ||
maybe_futures.compute() | ||
with xr.open_dataset( | ||
filename, chunks="auto", engine="zarr", **read_kwargs | ||
) as restored: | ||
assert isinstance(restored.var1.data, da.Array) | ||
computed = restored.compute() | ||
assert_allclose(original, computed) | ||
|
||
|
||
@gen_cluster(client=True) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.