Skip to content

fix: close progress bars on exception in io.py#422

Open
kredd2506 wants to merge 1 commit intogoogle:mainfrom
kredd2506:fix/progress-bar-resource-leak
Open

fix: close progress bars on exception in io.py#422
kredd2506 wants to merge 1 commit intogoogle:mainfrom
kredd2506:fix/progress-bar-resource-leak

Conversation

@kredd2506
Copy link
Copy Markdown

@kredd2506 kredd2506 commented Mar 21, 2026

Description

Wrap tqdm progress bar usage in try/finally blocks in two functions in langextract/io.py so that progress_bar.close() is always called, even when an exception is raised mid-iteration.

Location 1 — save_annotated_documents: If the annotated_documents generator raises (e.g. an InferenceOutputError from a failed LLM call), the original code left the progress bar open since progress_bar.close() sat unconditionally after the with open(...) block.

Location 2 — download_text_from_url: If response.iter_content() raises a requests.RequestException (e.g. a connection reset mid-download), the except block re-raised without calling progress_bar.close().

Fixes #401

Bug fix

How Has This Been Tested?

Ran the existing test suite (excluding the pre-existing openai import failure unrelated to this change):

$ python -m pytest tests/ -q --ignore=tests/inference_test.py
364 passed, 76 warnings, 29 subtests passed

Also manually verified with a generator that raises mid-stream — confirmed progress_bar.close() is called in both the exception and success paths.

Checklist:

  • I have read and acknowledged Google's Open Source Code of conduct.
  • I have read the Contributing page, and I either signed the Google Individual CLA or am covered by my company's Corporate CLA.
  • I have discussed my proposed solution with code owners in the linked issue(s) and we have agreed upon the general approach.
  • I have made any needed documentation changes, or noted in the linked issue(s) that documentation elsewhere needs updating.
  • I have added tests, or I have ensured existing tests cover the changes
  • I have followed Google's Python Style Guide and ran pylint over the affected code.

@github-actions github-actions bot added the size/XS Pull request with less than 50 lines changed label Mar 21, 2026
@google-cla
Copy link
Copy Markdown

google-cla bot commented Mar 21, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Wrap progress bar usage in try/finally blocks in save_annotated_documents
and download_text_from_url so progress_bar.close() is always called even
when an exception is raised mid-iteration.

Fixes google#401
@kredd2506 kredd2506 force-pushed the fix/progress-bar-resource-leak branch from 22b2e88 to 9f1047f Compare March 21, 2026 18:51
@github-actions
Copy link
Copy Markdown

⚠️ Branch Update Required

Your branch is 1 commits behind main. Please update your branch to ensure CI checks run with the latest code:

git fetch origin main
git merge origin/main
git push

Note: Enable "Allow edits by maintainers" to allow automatic updates.

@github-actions
Copy link
Copy Markdown

⚠️ Branch Update Required

Your branch is 3 commits behind main. Please update your branch to ensure CI checks run with the latest code:

git fetch origin main
git merge origin/main
git push

Note: Enable "Allow edits by maintainers" to allow automatic updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XS Pull request with less than 50 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: progress_bar resource leak — not closed on exception in save_annotated_documents and download_text_from_url

1 participant