pytroll / pytroll/satpy

fci_l1c_nc reader can't properly release file handle after reading under Windows

Open
#3,263 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.2k
Forks
335
Avg merge
1d 18h
Merged PRs (30d)
10

Description

Describe the bug
Just like the title said. Those nc files are still in lock so I can't move or delete them except for the last TRAIL part. Actually I'm not sure if it's a bug or just another nasty file handling problem in windows.

To Reproduce
Some ordinary satpy lines

scn = Scene(files=files)
scn.load([...])
scn.resample(...)
scn.save_dataset(...)

Temporary Solution
After done with your Scene object, try these lines:

for reader in scn._readers.values():
    fhd = getattr(reader, "file_handlers", {})
    for fhs in fhd.values():
        for fh in fhs:
            if hasattr(fh, "close"):
                fh.close()
            else:
                h = getattr(fh, "file_handle", None)
                if h is not None:
                    try:
                        h.close()
                    except Exception:
                        pass
                    fh.file_handle = None

They'll get every possible file handler in your Scene and close it so the files could be release. They work in my case at least.

Expected behavior
The file handles should be closed without further lines.

Environment Info:

  • OS: Windows 11 24h2
  • Satpy Version: 0.58

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the fci_l1c_nc reader and trace how its file handlers are owned and released through the Scene lifecycle. Reproduce on Windows using the provided Scene, load, resample, and save_dataset sequence, then verify the input files can be moved or deleted without the manual close workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.