typesense / typesense-docsearch-scraper

A fork of Algolia's awesome DocSearch Scraper, customized to index data in Typesense (an open source alternative to Algolia)
https://typesense.org/docs/guide/docsearch.html
Other
101 stars 36 forks source link

Automatically Close Resources #63

Closed pixeeai closed 1 month ago

pixeeai commented 6 months ago

Change Summary

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 * [https://cwe.mitre.org/data/definitions/772.html](https://cwe.mitre.org/data/definitions/772.html) * [https://cwe.mitre.org/data/definitions/404.html](https://cwe.mitre.org/data/definitions/404.html)

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

PR Checklist

pixeeai commented 6 months ago

FYI - This change was autogenerated from a new trending GitHub app - called Pixeebot. A code-quality GitHub App; like Dependabot, but for source code.