Skip to content

Automatically Close Resources#1

Open
pixeebot[bot] wants to merge 1 commit intomainfrom
pixeebot/drip-2024-03-09-pixee-python/fix-file-resource-leak
Open

Automatically Close Resources#1
pixeebot[bot] wants to merge 1 commit intomainfrom
pixeebot/drip-2024-03-09-pixee-python/fix-file-resource-leak

Conversation

@pixeebot
Copy link

@pixeebot pixeebot bot commented Mar 9, 2024

This codemod wraps assignments of open calls in a with statement. Without explicit closing, these resources will be "leaked" and won't be re-claimed until garbage collection. In situations where these resources are leaked rapidly (either through malicious repetitive action or unusually spiky usage), connection pool or file handle exhaustion will occur. These types of failures tend to be catastrophic, resulting in downtime and many times affect downstream applications.

Our changes look something like this:

import tempfile
path = tempfile.NamedTemporaryFile().name
-file = open(path, 'w', encoding='utf-8')
-file.write('Hello World')
+with open(path, 'w', encoding='utf-8') as file:
+   file.write('Hello World')
More reading

I have additional improvements ready for this repo! If you want to see them, leave the comment:

@pixeebot next

... and I will open a new PR right away!

Powered by: pixeebot (codemod ID: pixee:python/fix-file-resource-leak)

@pixeebot
Copy link
Author

pixeebot bot commented Mar 19, 2024

I'm confident in this change, and the CI checks pass, too!

If you see any reason not to merge this, or you have suggestions for improvements, please let me know!

@pixeebot
Copy link
Author

pixeebot bot commented Mar 19, 2024

Just a friendly ping to remind you about this change. If there are concerns about it, we'd love to hear about them!

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.

0 participants