Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly close all open files on exit #121

Merged
merged 1 commit into from
Oct 16, 2023

Conversation

zickgraf
Copy link
Contributor

@zickgraf zickgraf commented Oct 9, 2023

The old logic iterated over a list while modifying this list, effectively skipping every second open file.

To avoid such errors in my own code, I have added an error to my GAP which triggers if I remove elements from a list while iterating over it: zickgraf/gap@f8d4c35. Would this be a welcome change for GAP?

@ChrisJefferson
Copy link
Member

This looks good to me, and I certainly can't think of any bugs this could introduce. It's obviously hard to add a test for, but looks correct. Good catch!

@ChrisJefferson
Copy link
Member

With regards the error, I personally would be tempted to instead add this as an "InfoWarning", at level 1. This means if anyone does have code which is relying on this behaviour (honestly, I'm not positive how they would be), it will at least still run. However, I imagine in most cases where people write such code it is an error.

gap/io.gi Outdated
# so iterating over IO.OpenFiles directly would skip every
# second file. To avoid this, we iterate over a shallow copy
# of IO.OpenFiles.
for file in ShallowCopy( IO.OpenFiles ) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

Alternative implementation:

Suggested change
for file in ShallowCopy( IO.OpenFiles ) do
while Length(IO.OpenFiles) > 0 do
file := Remove(IO.OpenFiles);

This also avoids lots of unnecessary memory shuffling (though if you have so many files open that this would matter, you probably have other problems)...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think modifying IO.OpenFiles should only be done by the IO_... functions to ensure consistency, so I do not want to modify it by calling Remove(IO.OpenFiles);. I have pushed a different suggestion which avoids ShallowCopy, too.

The old logic iterated over a list while modifying this list, effectively
skipping every second open file.
@zickgraf
Copy link
Contributor Author

With regards the error, I personally would be tempted to instead add this as an "InfoWarning", at level 1.

(How) could I use "InfoWarning" from C code?

Copy link
Member

@fingolfin fingolfin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me thanks!

@fingolfin
Copy link
Member

(How) could I use "InfoWarning" from C code?

I guess one could import InfoWarning from the library "as usual", and then use the kernel helper InfoCheckLevel to check the level, and use that to wrap a Pr call.

@fingolfin fingolfin merged commit 038472c into gap-packages:master Oct 16, 2023
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants