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

Tempfile closing and unlinking #12328

Closed
Alagaesia93 opened this issue Oct 31, 2023 · 5 comments · Fixed by #12330
Closed

Tempfile closing and unlinking #12328

Alagaesia93 opened this issue Oct 31, 2023 · 5 comments · Fixed by #12330

Comments

@Alagaesia93
Copy link

Alagaesia93 commented Oct 31, 2023

Is your feature request related to a problem? Please describe.

I was today years old when I found out that Tempfile are supposed to be unlinked and closed for the GC to free the memory. I had a huge spike in memory and took me hours of investigations

Describe the solution you'd like

whenever using a Tempfile, raise a Rubocop error if it's not unlinked and closed - something similar to what happens when calling Kernel.open

Describe alternatives you've considered

I have not considered any, tbh

Additional context

I'm more than happy to help with a PR, but I don't know where to start and I don't have enough time in the next few weeks

@fatkodima
Copy link
Contributor

Change line here

TARGET_METHODS = { File: :open }.freeze
, add a test case in the relevant file, generate a changelog entry and thats it.

Note, that rubocop does only static analysis of the code (no target code execution), and the AutoResourceCleanup cop is pretty useless, because it can generate a lot of false positives, thats why it is disable by default. It detects only #open methods. It is possible, that in the source some file is created in one place and closed in another. This cop won't identify this.

@Alagaesia93
Copy link
Author

Thank @fatkodima, that was exactly my concern. I'm not super experienced in Rubocop, so I have no idea how to check that a Tempfile is created and never closed. It could be in the same metod, or another method, or somewhere else. Is that possible with Rubocop?

@fatkodima
Copy link
Contributor

Is that possible with Rubocop?

No, because rubocop is a static code analyzer and does not run the user's code. The best it can do, as seen in the mentioned cop, is checking that File.open with block is used.

@fatkodima
Copy link
Contributor

Actually you can check that the file is closed in the same method, but this is a perfect case for calling open with a block.

@koic
Copy link
Member

koic commented Nov 3, 2023

I opened #12330 and also refactored it to tweak some redundant logic.

koic added a commit to koic/rubocop that referenced this issue Nov 5, 2023
…le.open`

Fixes rubocop#12328.

This PR makes `Style/AutoResourceCleanup` aware of `Tempfile.open`.
koic added a commit that referenced this issue Nov 5, 2023
…leanup_aware_of_tempfile_open

[Fix #12328] Make `Style/AutoResourceCleanup` aware of `Tempfile.open`
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 a pull request may close this issue.

3 participants