Always close files (ReadDigraphs, WriteDigraphs)#757
Always close files (ReadDigraphs, WriteDigraphs)#757james-d-mitchell merged 9 commits intodigraphs:mainfrom
ReadDigraphs, WriteDigraphs)#757Conversation
|
Okay I think this works. The fix for Basically we're suppressing errors (to stop premature break loops before the file is closed), but still 'catching' them. If we catch an error, we then close the file, restore normal errors, and then try to encode again (which is expected to give an error). i.e. we pass through the encoder twice if we know for sure that it will This is obviously quite finicky. Some notes to make the review easier:
Hopefully the lines added to the tests will help avoid this problem in the future. |
|
Thanks @pramothragavan, it looks good, but I'll try to read this in detail today. |
|
A potentially simpler solution which does not require encoding twice might be to do something like this on the start of your function: |
|
Very nice! A minor point -- if the user enters the break loop manually, the file would close and they'd be unable to reenter the main loop? Maybe this scenario's too unrealistic and not worth the trouble |
|
Good point @pramothragavan, I think we can live with that |
|
I've implemented these changes, but in test environments we don't enter the break loop, so I've had to manually restore Outside of the scope of tests, this works great -- thanks Max! |
|
Seems good, okay to merge @pramothragavan ? |
|
Yeah reckon so @james-d-mitchell, as long as we're happy with these caveats |
Related to #377, which ensured files close on error when decoding.
ReadDigraphsis leaving files open if a malformed file is fed in when using a whole file decoder (dreadnaut/dimacs) -- my bad. I had also carelessly left a file open in the tests. The suggested changes fixes those issues.Something that's not my fault is that this issue propagates to encoding with
WriteDigraphsbut affects all file formats. I think this issue has probably been around for a while. If we encounter an error while encoding (i.e. due to the digraph being incompatible somehow with the file format), then the file isn't closed. For instance,I'm not really sure how to fix this, because you inevitably need to keep the file open while the digraph is being encoded. Is there maybe a good way of doing a try/catch style statement?
I've added a few lines to the tests to make sure the list of open files is the same before and after the test runs. Consequently, CI should fail here.