BUG: Close cached raster files before interpreter shutdown - #936
Conversation
|
I would prefer an implementation that allows xarray to handle it instead of a custom implementation. |
|
Thanks — I checked I also retested clean current rioxarray I will not defend the current custom registry. Would you prefer that I close #936 in favor of an upstream xarray lifecycle issue, or narrow the rioxarray guidance to require/document explicit close or context-manager use? |
|
Closing this draft. The current weak-registry/atexit patch is not a maintainable fit for the requested xarray-managed design.
Explicit |
Summary
Root cause
open_rasteriocreates a cached.rioaccessor cycle, so unclosed raster-backed arrays can survive until interpreter shutdown. xarray removesCachingFileManager.__del__in its own atexit hook; rasterio handles that remain open after the hook can then emit emptysys.excepthookerrors during teardown. Concurrent manager finalizers can also leave files in xarray's cache after the manager objects themselves are gone.The weak-key registry follows the file handles rather than retaining their managers. Normal close and cache eviction remove entries automatically. At process exit, the callback closes only files whose associated lock can be acquired immediately, avoiding both concurrent close and shutdown hangs.
Closes #929.
Validation
masterreproducer: RED with 5/5 teardown errors; GREEN with no stderr after this changerioxarray/_io.py, andgit diff --check: passedAI assistance disclosure
OpenAI Codex reproduced the issue, prepared the implementation and tests, audited visible overlap and contribution-policy constraints, and ran the validation above under the account owner's authorization. The AI-assisted nature of this contribution is disclosed for maintainer review.