Skip to content

Progress bar is duplicated when used both as a context manager and as an iterable wrapper #301

Description

@dreamscached

Description

When used both as context manager (e.g. with) and as iterable wrapper, exhausting the iterable and exiting the with scope causes the progress bar to duplicate.

Code

Consider the following example code:

from time import sleep
from progressbar import ProgressBar
from progressbar.widgets import AnimatedMarker, SimpleProgress, GranularBar, Timer

def simple_iterator():
    for i in range(100):
        sleep(0.02)
        yield i

progress = ProgressBar(
    max_value=100,
    redirect_stdout=True,
    redirect_stderr=True,
    widgets=[AnimatedMarker("⡏⠟⠻⢹⣸⣴⣦⣇"), " ",
             SimpleProgress(), " ",
             GranularBar(markers="░▒▓█"), " ",
             Timer()]
)

with progress:
    for i in progress(simple_iterator()):
        pass

ASCIInema demo: https://asciinema.org/a/UEEc81s5GyrAR3QaqLDWGIMGX

Versions

  • Python version: 3.12.6 (main, Sep 8 2024, 13:18:56) [GCC 14.2.1 20240805]
  • Python distribution/environment: CPython
  • Operating System: Arch Linux, etc.
  • Package version: 4.5.0

Activity

  1. dreamscached commented on Sep 22, 2024

    @dreamscached
    Author

    I assume finish is invoked twice for some reason, because leaving in either a context manager only or iterable wrapper only, it works as expected.

  2. dreamscached commented on Oct 30, 2024

    @dreamscached
    Author

    Bumping because it's still an issue and was not solved.

  3. reopened this on Oct 30, 2024
  4. wolph commented on Oct 30, 2024

    @wolph
    Owner

    Bumping because it's still an issue and was not solved.

    Sorry about he inactivity, I've seen the issue but I've had very little time.

    It's definitely a bug :)

  5. added 2 commits that reference this issue on Jun 20, 2026
  6. wolph commented on Jul 18, 2026

    @wolph
    Owner

    Fixed on develop: using a bar as both a context manager and an iterable no longer finishes/draws it twice. Locked by tests/test_with.py::test_context_manager_and_iterable_no_duplicate and the reporter's-exact-widget-set variant test_context_manager_and_iterable_reporter_widgets_no_duplicate added in #326. Closing.

  7. added a commit that references this issue on Aug 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions