fix(parser): fix temporary file leak in HTMLParser download link hand… #95
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR: Fix Temporary File Leak in HTMLParser's Download Link Handling
Description
This PR addresses a critical temporary file leak in the
_handle_download_linkmethod ofHTMLParser. When downloading remote resources (PDF, Markdown, etc.) for delegated parsing, temporary files were not reliably deleted if an exception occurred during parsing or if the function exited early via the HTML branch. The fix implements a robusttry...finallyblock to guarantee file cleanup under all execution paths.Type of Change
Changes Made
temp_path = Noneto track whether a temporary file was successfully created.finallyblock, removing ad-hocunlinkcalls in success branches.try...exceptto ensure that errors during deletion (e.g., file system locks) do not interfere with the returned results.htmlbranch return path still triggers thefinallyblock for cleanup.Testing
A manual verification script was used to simulate failures:
Test Scenario: Forced a
ValueErrorby passing an unsupported file type after download.Result: Verified that the temporary file was physically removed from the system's
Tempdirectory despite the exception.Checklist
Screenshots (if applicable)
N/A
Additional Notes
The fix adheres to the KISS (Keep It Simple, Stupid) principle by using standard Python resource management instead of complex custom cleanup logic.