satpy icon indicating copy to clipboard operation
satpy copied to clipboard

Fix ahi_hsd cleanup during interpreter shutdown

Open sfinkens opened this issue 2 months ago • 5 comments

I get the following warning when reading bzipped AHI HSD files

Exception ignored in: <function AHIHSDFileHandler.__del__ at 0x7f5fff886200>
Traceback (most recent call last):
  File ".../satpy/readers/ahi_hsd.py", line 409, in __del__
AttributeError: 'NoneType' object has no attribute 'path'

This is because the os module is set to None during interpreter shutdown. From the docs:

del() can be executed during interpreter shutdown. As a consequence, the global variables it needs to access (including other modules) may already have been deleted or set to None

I followed the advice in the docs and registered the cleanup using weakref.finalize.

sfinkens avatar Nov 18 '25 10:11 sfinkens

Looking at the documentation, I don't see it clearly written that weakref.finalize would solve the problem you mention. Could you verify yourself that it does indeed work?

mraspaud avatar Nov 18 '25 12:11 mraspaud

Yes, with finalizer the warnings are gone. Did you see that section in the documentation?

A finalizer will never invoke its callback during the later part of the interpreter shutdown when module globals are liable to have been replaced by None.

sfinkens avatar Nov 18 '25 13:11 sfinkens

no, but I'm probably looking in the wrong place :) https://docs.python.org/3/reference/datamodel.html#object.del

mraspaud avatar Nov 18 '25 14:11 mraspaud

Oh, sorry that's from https://docs.python.org/3/library/weakref.html#weakref.finalize

sfinkens avatar Nov 18 '25 14:11 sfinkens

Thanks for the link, I understand what finalize does here, so LGTM! I have to admit I'm a bit disappointed by __del__'s behaviour :) or I don't understand what it is for.

mraspaud avatar Nov 19 '25 10:11 mraspaud