Fix progress bar cursor not being restored when interrupted #3690
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.
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 correspondingconsole.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
andLive
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:
Try/Except Blocks: Added try/except blocks around the stop method, ensuring cursor visibility is restored even if an error occurs during cleanup.
Signal Handlers: Added SIGINT (Ctrl+C) handlers that show the cursor before re-raising the KeyboardInterrupt, ensuring clean cursor state even during user interruptions.
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:
After:
Benefits