tsinfer
tsinfer copied to clipboard
On error, SampleData context manager does not close the data stream
One thing stopping us testing on windows is that, when __exit__() is called on a SampleData object, and it fails (e.g. because we are out of memory), then we don't end up calling .close() on the data.store within the object, so the filehandle remains open. I guess it would be neater to close it on error, somehow?
Excellent catch --- yes, we should do our very best to be tidy here, even if the code ends up being a bit convoluted.
I guess wrapping the close operation in a try... finally... is the way to do it?
It turns out that this is complex, and may require reworking of the __exit()__ function for a DataContainer (see discussion here). It's not just on error either, because even after the __exit__() method is called, we deliberately keep a (read-only) copy of the data open, including keeping an open filehandle. This will keep causing a problem on windows, which doesn't like deleting files that are still open.
At the moment we can work around this second issue in tests by explicitly closing the data structure, if it is stored on file.