Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

mpremote: Fix disconnect handling on Windows and Linux.#17321

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
andrewleech wants to merge1 commit intomicropython:master
base:master
Choose a base branch
Loading
fromandrewleech:mpremote_disconnect

Conversation

andrewleech
Copy link
Contributor

Summary

Improves device disconnection handling in mpremote to show a clean "device disconnected" message instead of stack traces on Windows and fixes terminal formatting issues on Linux.

Problem

When a device disconnects during an mpremote session:

  • Windows: Shows a full stack trace with ClearCommError failed or similar errors
  • Linux: 'device disconnected' with unclear terminal formatting, causing indentation issues

Solution

This PR fixes disconnect handling to:

  1. Catch serial disconnection exceptions in both console.py and repl.py
  2. Properly exit terminal raw mode before showing the message
  3. Display a clean "device disconnected" message after terminal cleanup
  4. Ensure consistent behavior across Windows and Linux platforms

File Changes

tools/mpremote/mpremote/console.py:

  • Windows: Modified waitchar() to catch SerialException and re-raise with cleaner message
  • Added proper exception handling for ClearCommError failed cases

tools/mpremote/mpremote/repl.py:

  • Modified do_repl_main_loop() to catch serial exceptions and return disconnect state
  • Added checks for both Windows and Linux disconnect error patterns

tools/mpremote/mpremote/main.py:

  • Modified main loop to handle disconnect state after console cleanup
  • Added newline and message printing after terminal mode is restored

Testing

Before/After Examples - I connected to ESP32S2 (usb serial) and once confirmed the repl is working, physically unplug the devices.

Windows (Before)

PS C:\Users\anl> mpremoteConnected to MicroPython at COM7Use Ctrl-] or Ctrl-x to exit this shellMicroPython v1.25.0-1.ge7b0fe4775.dirty on 2025-05-09; Generic ESP32S2 module with ESP32S2Type "help()" for more information.>>>>>> Traceback (most recent call last):  File "C:\Users\anl\AppData\Roaming\uv\python\cpython-3.10.11-windows-x86_64-none\lib\runpy.py", line 196, in _run_module_as_main    return _run_code(code, main_globals, None,  File "C:\Users\anl\AppData\Roaming\uv\python\cpython-3.10.11-windows-x86_64-none\lib\runpy.py", line 86, in _run_code    exec(code, run_globals)  File "c:\users\anl\.local\bin\mpremote.exe\__main__.py", line 8, in <module>  File "C:\Users\anl\AppData\Roaming\uv\tools\mpremote\lib\site-packages\mpremote\main.py", line 584, in main    disconnected = do_repl(state, argparse_repl().parse_args([]))  File "C:\Users\anl\AppData\Roaming\uv\tools\mpremote\lib\site-packages\mpremote\repl.py", line 102, in do_repl    disconnected = do_repl_main_loop(  File "C:\Users\anl\AppData\Roaming\uv\tools\mpremote\lib\site-packages\mpremote\repl.py", line 11, in do_repl_main_loop    console_in.waitchar(state.transport.serial)  File "C:\Users\anl\AppData\Roaming\uv\tools\mpremote\lib\site-packages\mpremote\console.py", line 99, in waitchar    while not (self.inWaiting() or pyb_serial.inWaiting()):  File "C:\Users\anl\AppData\Roaming\uv\tools\mpremote\lib\site-packages\serial\serialutil.py", line 594, in inWaiting    return self.in_waiting  File "C:\Users\anl\AppData\Roaming\uv\tools\mpremote\lib\site-packages\serial\serialwin32.py", line 259, in in_waiting    raise SerialException("ClearCommError failed ({!r})".format(ctypes.WinError()))serial.serialutil.SerialException: ClearCommError failed (PermissionError(13, 'The device does not recognize the command.', None, 22))PS C:\Users\anl>

Windows (After)

PS C:\Users\anl> mpremoteConnected to MicroPython at COM7Use Ctrl-] or Ctrl-x to exit this shellMicroPython v1.25.0-1.ge7b0fe4775.dirty on 2025-05-09; Generic ESP32S2 module with ESP32S2Type "help()" for more information.>>>>>>>>>device disconnectedPS C:\Users\anl>

Linux (Before)

anl@ANL2-LAP:~/micropython/tools/mpremote$ mpremoteConnected to MicroPython at /dev/ttyACM0Use Ctrl-] or Ctrl-x to exit this shellMicroPython v1.25.0-1.ge7b0fe4775.dirty on 2025-05-09; Generic ESP32S2 module with ESP32S2Type "help()" for more information.>>>>>> device disconnected                       anl@ANL2-LAP:~/micropython/tools/mpremote$

Linux (After)

anl@ANL2-LAP:~/micropython/tools/mpremote$ mpremoteConnected to MicroPython at /dev/ttyACM0Use Ctrl-] or Ctrl-x to exit this shellMicroPython v1.25.0-1.ge7b0fe4775.dirty on 2025-05-09; Generic ESP32S2 module with ESP32S2Type "help()" for more information.>>>>>>device disconnectedanl@ANL2-LAP:~/micropython/tools/mpremote$
  • Tested on Windows 11 with ESP32S2 USB serial
  • Tested on Linux (Ubuntu WSL) with ESP32S2 USB serial
  • Verified terminal formatting is restored properly
  • Confirmed clean exit without stack traces

Josverl reacted with heart emoji
- Handle SerialException on Windows when device disconnects- Print clean 'device disconnected' message instead of stack trace- Fix terminal formatting issues on Linux after disconnect- Return disconnected state after console cleanup to avoid terminal issuesThis ensures proper disconnect messages on both platforms withoutshowing confusing error traces to users.Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
@codecovCodecov
Copy link

codecovbot commentedMay 19, 2025
edited
Loading

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.54%. Comparing base(45e4deb) to head(ad0f34e).
Report is 56 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@##           master   #17321      +/-   ##==========================================- Coverage   98.54%   98.54%   -0.01%==========================================  Files         169      169                Lines       21898    21897       -1     ==========================================- Hits        21580    21579       -1  Misses        318      318

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report?Share it here.

🚀 New features to boost your workflow:
  • ❄️Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actionsGitHub Actions
Copy link

Code size report:

   bare-arm:    +0 +0.000% minimal x86:    +0 +0.000%    unix x64:    +0 +0.000% standard      stm32:    +0 +0.000% PYBV10     mimxrt:    +0 +0.000% TEENSY40        rp2:    +0 +0.000% RPI_PICO_W       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS  qemu rv32:    +0 +0.000% VIRT_RV32

@Josverl
Copy link
Contributor

Josverl commentedMay 19, 2025
edited
Loading

I think this is a good improvement to makempremote a better cmdline tool.

I notice that you have not added or changed any tests.
is that possible or would the need access to amock facility in testing ?

@andrewleech
Copy link
ContributorAuthor

I think this is a good improvement to makempremote a better cmdline tool.

I notice that you have not added or changed any tests.
is that possible or would the need access to amock facility in testing ?

Good point! I've been trying to get better at test coverage lately but yeah I'm not sure how to test a feature like this without hardware in the loop.

@dpgeorgedpgeorge added the toolsRelates to tools/ directory in source, or other tooling labelMay 20, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
toolsRelates to tools/ directory in source, or other tooling
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@andrewleech@Josverl@dpgeorge@pi-anl

[8]ページ先頭

©2009-2025 Movatter.jp