Registry Mirror
Skip to content

Conversation

@headtr1ck

@headtr1ck headtr1ck commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Fixes the RuntimeWarning: Cannot close a netcdf_file opened with mmap=True, when netcdf_variables or arrays referring to its data still exist warnings in test_open_mfdataset_manyfiles[scipy-20-...-5].

ScipyArrayWrapper._getitem returned a view into the memory map and __getitem__ only copied it afterwards, when the file was no longer held. When reading with dask threads and more files than file_cache_maxsize, another thread could evict and close the file while that view was still alive. scipy then warns and leaves the mmap open.

The data is now read and copied inside _manager.acquire_context(), which pins the file until the copy is done (the pinning from #11622). As a side effect, the numpy < 2 copy= workaround is gone and __getitem__ no longer re-acquires the file just to check use_mmap.

Also silences the expected unclosed-file RuntimeWarning in test_file_manager_del_while_pin_lock_held, which deliberately deallocates an unclosed manager (the test suite turns on warn_for_unclosed_files globally).

No regression test: the warning is raised in a dask worker thread, so turning it into an error with filterwarnings doesn't fail the test.

Checklist

  • User visible changes (including notable bug fixes) are documented in whats-new.rst

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
      Tools: Claude Code (Claude Opus 5.5)
      Prompt: "can we fix these warnings?" (pasting the PytestUnraisableExceptionWarning from test_file_manager_del_while_pin_lock_held and the scipy mmap RuntimeWarnings from test_open_mfdataset_manyfiles)

[This is Claude Code on behalf of Michael Niklas]

🤖 Generated with Claude Code

headtr1ck and others added 4 commits September 30, 2026 17:36
ScipyArrayWrapper copied data from memory mapped files only after
releasing the file, so another dask thread could evict and close the
file while a view into its memory map was still alive. scipy then warns
"Cannot close a netcdf_file opened with mmap=True" and leaves the mmap
open. Read and copy inside acquire_context() instead, which keeps the
file open until the copy is done.

Also silence the expected unclosed-file warning in
test_file_manager_del_while_pin_lock_held.

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Claude <noreply@anthropic.com>
Guards against keeping a reference into the memory map alive while the
file is unpinned, which makes scipy warn and leave the mmap open.

Co-authored-by: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@headtr1ck
headtr1ck force-pushed the fix-scipy-mmap-eviction branch from b167878 to ffe67cc Compare September 30, 2026 15:38
@headtr1ck
headtr1ck enabled auto-merge (squash) September 30, 2026 15:39
@headtr1ck
headtr1ck merged commit 0262f5d into pydata:main Sep 30, 2026
44 checks passed
@headtr1ck
headtr1ck deleted the fix-scipy-mmap-eviction branch September 30, 2026 18:39
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.

2 participants