tsinfer icon indicating copy to clipboard operation
tsinfer copied to clipboard

On error, SampleData context manager does not close the data stream

Open hyanwong opened this issue 6 years ago • 3 comments

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?

hyanwong avatar Oct 30 '19 14:10 hyanwong

Excellent catch --- yes, we should do our very best to be tidy here, even if the code ends up being a bit convoluted.

jeromekelleher avatar Oct 30 '19 16:10 jeromekelleher

I guess wrapping the close operation in a try... finally... is the way to do it?

hyanwong avatar Oct 30 '19 17:10 hyanwong

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.

hyanwong avatar Oct 31 '19 15:10 hyanwong