Skip to content
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

Fix progress bar cursor not being restored when interrupted #3690

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

shyam-ramani
Copy link

Fix for Progress Bar Cursor Not Being Restored When Interrupted

Issue

When a progress bar operation is interrupted (e.g., by Ctrl+C), the cursor remains hidden because console.show_cursor(False) is called at the start of the progress operation, but the corresponding console.show_cursor(True) is never called.

This can leave users with an invisible cursor in their terminal after interrupting a process with Ctrl+C, forcing them to manually reset their terminal.

Root Cause

The Progress and Live classes hide the cursor when starting, but if the operation is interrupted before completion, the code that restores the cursor visibility isn't executed.

Fix

This PR implements a three-layered approach to ensure the cursor is always restored, even during interruptions:

  1. Try/Except Blocks: Added try/except blocks around the stop method, ensuring cursor visibility is restored even if an error occurs during cleanup.

  2. Signal Handlers: Added SIGINT (Ctrl+C) handlers that show the cursor before re-raising the KeyboardInterrupt, ensuring clean cursor state even during user interruptions.

  3. Exit Handlers: Added atexit handlers to ensure cursor visibility is restored if the program exits for any reason.

The implementation is platform-aware and only sets up signal handlers on platforms that support them (not on Windows).

Example

Before:

# Terminal has visible cursor
with Progress() as progress:
    # Cursor is hidden during progress
    task = progress.add_task("Processing...", total=100)
    # If user presses Ctrl+C here
    # The cursor remains hidden after program exits

After:

# Terminal has visible cursor
with Progress() as progress:
    # Cursor is hidden during progress
    task = progress.add_task("Processing...", total=100)
    # If user presses Ctrl+C here
    # Our handler ensures cursor is shown before exiting
    # Terminal keeps visible cursor

Benefits

  1. Prevents "invisible cursor" after program interruption
  2. Improves user experience by maintaining proper terminal state
  3. Works across platforms (with appropriate fallbacks for Windows)
  4. Multiple redundant mechanisms ensure cursor restoration in various scenarios

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.

1 participant