Skip to content

disable_quick_edit() crashes when restarting BLACS from a ConPTY console #27

Description

@damienBloch

Issue

On Windows 11, with zprocess 2.27.1, when running BLACS from a ConPTY console (VS Code integrated terminal or Windows Terminal), recompiling the connection table and then pressing Restart blacs crashes BLACS during startup with SetConsoleMode error 87 in zprocess.

Traceback

Traceback (most recent call last):
  File "C:\Users\dbloch\labscript-suite\blacs\blacs\__main__.py", line 15, in <module>
    import labscript_utils.excepthook
  File "C:\Users\dbloch\labscript-suite\labscript-utils\labscript_utils\__init__.py", line 101, in <module>
    zprocess.disable_quick_edit()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "C:\Users\dbloch\labscript-suite\userlib\.venv\Lib\site-packages\zprocess\utils.py", line 190, in disable_quick_edit
    console.SetConsoleMode(new_mode)
    ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
pywintypes.error: (87, 'SetConsoleMode', 'The parameter is incorrect.')

Possible cause

In disable_quick_edit(), the Get call is wrapped in try/except pywintypes.error, but the Set call is not, so the exception propagates:

# zprocess/utils.py
if (orig_mode & ENABLE_EXTENDED_FLAGS) and not (orig_mode & ENABLE_QUICK_EDIT):
    return
new_mode = (orig_mode | ENABLE_EXTENDED_FLAGS) & ~ENABLE_QUICK_EDIT
console.SetConsoleMode(new_mode)   # <-- unguarded, raises error 87 in a ConPTY

This only surfaces on restart, not first launch: SetConsoleMode() always fails in a ConPTY, so the crash happens only when the early-return guard above is not taken.
On first launch the console mode satisfies the guard and the call is skipped.
The relaunched process inherits a console mode that no longer satisfies it, so SetConsoleMode() runs and fails.

Quick fix

By putting the same try/except for SetConsoleMode() than GetConsoleMode() this avoids the crash:

try:
    console.SetConsoleMode(new_mode)
except pywintypes.error:
    return 
atexit.register(console.SetConsoleMode, orig_mode)

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions