Skip to content

ensure threads are stopped in Progress().track #2934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

ThunderKey
Copy link

Type of changes

  • Bug fix
  • New feature
  • Documentation / docstrings
  • Tests
  • Other

Checklist

  • I've run the latest black with default args on new code.
  • I've updated CHANGELOG.md and CONTRIBUTORS.md where appropriate.
  • I've added tests for new code.
  • I accept that @willmcgugan may be pedantic in the code review.

Description

If Progress().track is used with a generator and an exception is raised within this generator the _TrackThread is not stopped and the program does not finish. This change ensures that the _TrackThreads are stopped correctly.

A simple example would be:

from rich.progress import Progress

with Progress() as progress:
    tuple(i * object() for i in progress.track(range(10)))

This raises an error, since there is no * operator for int and object. The error is shown correctly, but the program does not complete, because a _TrackThread is still running.

The problem is, that generators are not aware of exceptions thrown. Therefore, track does not realize that the iteration was aborted and keeps the _TrackThread alive.

My workaround is to store all _TrackThread instances in a set and stop them in the Progress().stop method.

Unfortunately, this does not work for rich.progress.track, because this function is also not informed about exceptions. Therefore, the with statement is not aborted and continues to run.

I've prepared the changelog, but since I don't know your release schedule I've omitted the date of the release.

If Progress().track is used with a generator and an exception is raised within this generator
the _TrackThread is not stopped and the program does not finish.
This change ensures that the _TrackThreads are stopped correctly
@ThunderKey ThunderKey force-pushed the fix-progress-track-with-error-in-generators branch from 2511f54 to 53e766a Compare April 25, 2023 09:18
@willmcgugan
Copy link
Member

Thanks, fixed in master (slightly different solution(

@willmcgugan willmcgugan closed this Jul 1, 2024
@NicoWeio
Copy link

NicoWeio commented Jul 1, 2024

For reference: #3402

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.

3 participants