Skip to content

Prevent file descriptor leak and modernize BufferedWriter creation#101

Open
pixeebot[bot] wants to merge 1 commit intomasterfrom
pixeebot/drip-2024-12-01-pixee-java/prevent-filewriter-leak-with-nio
Open

Prevent file descriptor leak and modernize BufferedWriter creation#101
pixeebot[bot] wants to merge 1 commit intomasterfrom
pixeebot/drip-2024-12-01-pixee-java/prevent-filewriter-leak-with-nio

Conversation

@pixeebot
Copy link
Copy Markdown

@pixeebot pixeebot bot commented Dec 1, 2024

This change prevents a file descriptor leak and modernizes the file writing API pattern.

The way the code is written now, the FileWriter never gets closed. Thus, it is up to the garbage collector's objection finalization process to close them at some point. This is not a good practice, and it can lead to a file descriptor leak. In hot code paths, it could cause exhaustion of all the available file descriptors for the system and lead to denial-of-service conditions.

Our changes look something like this:

-  BufferedWriter writer = new BufferedWriter(new FileWriter(f));
+  BufferedWriter writer = Files.newBufferedWriter(f.toPath());
More reading

🧚🤖 Powered by Pixeebot

Feedback | Community | Docs | Codemod ID: pixee:java/prevent-filewriter-leak-with-nio

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