espressif / vscode-esp-idf-extension

Visual Studio Code extension for ESP-IDF projects
Apache License 2.0
1.05k stars 302 forks source link

debug adapter fails with Python 3.12/homebrew/MacOS arm64 due to 3.12 regression, works with 3.11/homebrew (VSC-1339) #1177

Closed mhaberler closed 6 months ago

mhaberler commented 6 months ago

OS

MacOS

Operating System version

Sonoma 14.4.1 (23E224)

Visual Studio Code version

Version: 1.88.0 (Universal) Commit: 5c3e652f63e798a5ac2f31ffd0d863669328dc4c

ESP-IDF version

5.2.1

Python version

3.12.2_1 (broken), 3.11.9 (works), both homebrew

Doctor command output

doctor output for 3.11 and 3.12 doctor-3.12.txt doctor-3.11.txt esp_idf_vsc_ext.log

Extension

esp_idf_vsc_ext.log attached above (for failed 3.12 debug attempt towards end)

Description

when debugging using a homebrew Python 3.12 install:

Explorer->Debug Console shows:

Debug adapter -> Extension: DEBUG_ADAPTER_STARTED Debug adapter -> Extension: DEBUG_ADAPTER_READY2CONNECT Debug Adapter initialized 2 can't create new thread at interpreter shutdown <----------------- this is the cause

when debugging using a homebrew Python 3.11 install:

Explorer->Debug Console does NOT show the above error message!

the cause is very likely the 3.12 regression described here: https://stackoverflow.com/questions/77631477/runtimeerror-cant-create-new-thread-at-interpreter-shutdown-python-3-12

This is at program startup - debugger stops at app_main():

Bildschirmfoto 2024-04-09 um 22 50 01

this is after setting the breakpoint: Bildschirmfoto 2024-04-09 um 22 50 33 Bildschirmfoto 2024-04-09 um 22 51 36

Debug Message

Debug adapter -> Extension: DEBUG_ADAPTER_STARTED
Debug adapter -> Extension: DEBUG_ADAPTER_READY2CONNECT
Debug Adapter initialized
2
can't create new thread at interpreter shutdown  <----------------- this is the cause

Other Steps to Reproduce

I hit an attachment size limit above and cannot add more text

The third image above shows the behaviour after hitting the continue icon NB the yellow caret did NOT move, see the 'can't create new thread at interpreter shutdown' error message in the Debug console

Summary:

the following sequence worked for me

Recommendation:

I have checked existing issues, online documentation and the Troubleshooting Guide

mhaberler commented 6 months ago

after some more trial and error, the following seems a less painful path which works for me:

brianignacio5 commented 6 months ago

We are working on migrating to a new debug adapter that is more stable that our current python one in this PR:

https://github.com/espressif/vscode-esp-idf-extension/pull/1131

You are welcome to test it with the PR vsix installer if you like.

mhaberler commented 6 months ago

I did in fact use the #1131 vsix version but forgot to mention this in the current issue report

meaining: this issue applies to #1131 as well

since the underlying issue is present on Windows as well (and hence likely linux too) debugging using this extension is likely broken for all platforms as long as your debug adapter permits 3.12 and imports threading.py which the #1131 vsix also does

and since the #1131 vsix uses threading it evades me how this code is supposed to fix a problem introduced by permitting a combination of Python3.12 and threading.py ?

iow: the #1131 extension PR cannot possibly fix the issue reported here

what will fix it is avoiding either Python3.12 and/or `threading``

the tricky part is: esp-idf installs and works just fine with Python3.12 - it ONLY breaks this extension at runtime leading users to believe all prerequisites are fullfilled

this really should be made an install time error until 3.12 or later and/or threading.py is fixed

brianignacio5 commented 6 months ago

The thing is, the new debug adapter doesn't use Python. If you are is because probably you are still using .vscode/launch.json configuration for older debug adapter. Please test the new one with update launch.json

 {
   "configurations": [
     {
       "type": "gdbtarget",
       "request": "attach",
       "name": "Eclipse CDT Remote",
       "program": "${workspaceFolder}/build/${command:espIdf.getProjectName}.elf",
       "initCommands": [
         "set remote hardware-watchpoint-limit 2",
         "mon reset halt",
         "maintenance flush register-cache",
         "thb app_main",
       ],
       "gdb": "${command:espIdf.getXtensaGdb}",
       "target": {
         "port": "3333"
       },
     }
   ]
 }
mhaberler commented 6 months ago

well using your above launch.json fragment finally worked with the new debug adapter debugger behaviour looks entirely reasonable now

so debug_adapter.py ff seem to be dead code now to start with, threading error message is gone

for others to follow:

and debugging starts to work reasonably well.

let me understand what the best practice is for debugging:

I noticed at least on MacOS openocd remains running even when exiting VSCode, which interferes with the next debugging session, leading to a message

[OpenOCD]
❌ Open On-Chip Debugger v0.12.0-esp32-20230921 (2023-09-21-13:27)
Licensed under GNU GPL v2
For bug reports, read
    http://openocd.org/doc/doxygen/bugs.html
debug_level: 2
adapter speed: 20000 kHz
WARNING: boards/esp32-wrover.cfg is deprecated, and may be removed in a future release.
         If your board is ESP32-WROVER-KIT, use board/esp32-wrover-kit-1.8v.cfg instead.
Info : auto-selecting first available session transport "jtag". To override use 'transport select <transport>'.
Error: couldn't bind tcl to socket on port 6666: Address already in use   <-----

but it seems to do no harm as the debugger comes up nevertheless

mhaberler commented 6 months ago

above I wrote:

I noticed at least on MacOS openocd remains running even when exiting VSCode, which interferes with the next debugging session, leading to a message

correction: it does harm.

scenario:

this needs fixing as well - hard to figure a leftover openocd in the background is at fault

brianignacio5 commented 6 months ago

For a new project, you can create a new project using the ESP-IDF: Show Examples or ESP-IDF: Project Wizard command to create a new project from example with .vscode files, or just add .vscode files to currently opened directory with ESP-IDF: Add vscode configuration folder command.

With regards to openOCD, how are you running openOCD, are you using the extension openOCD Server ? Are you stopping the debugger before closing the vscode window or just closing the window ? Any output related to openOCD not closing properly ?

I ask because I have tested on macOS and openOCD seems to close normally after I stop the debug session.

mhaberler commented 6 months ago

just leaving VSCode during a debug session leaves openocd running - this could have been the cause exiting (I assume what is now the red diagonal unplug icon) terminates openocd properly it could be this was with the previous extension version, where you had the red square icon for terminating the session - if the debugging session went haywire that icon eventually got you out of the debug session but tended to leave an openocd in the background

will have a closer eye on that exactly when it happens

mhaberler commented 6 months ago

oh I see, + Add .vscode subdirectory files now adds the Eclipse CDT Remote config to launch.json

if I wipe the .vscode directory and restart VSCode, it creates .vscode/settings.json as per target selection:

{
    "idf.adapterTargetName": "esp32c3"
}

for debugging to work, I need a .vscode/settings.json (example M5stack stamp-c3u = esp32c3) like so:

{
    "idf.adapterTargetName": "esp32c3",
    "idf.port": "/dev/cu.usbmodem2131301",
    "idf.flashType": "UART",
    "idf.openOcdConfigs": [
        "board/esp32c3-builtin.cfg"     // <---- add this, then things work
    ]
}

it is unclear to me how I get from the former to the latter other than editing, is this the proper way to do it?

I guess what I'm trying to do is an idiots recipe for going from hello_world to a working debug session given a piece of hardware

next hop: extend towards the Multiple Build Configurations Example for working with different targets

then: add Arduino as component without bringing down the house, and have debugging still work.

mhaberler commented 6 months ago

AHA. launch.json with Eclipse CDT Remote config is only added by + Add .vscode subdirectory files if launch.json does not exist yet

brianignacio5 commented 6 months ago

You can check the tutorial documentation should provide most of extension functionality.

mhaberler commented 6 months ago

sorry, after spending a disordinate amount of time trying to work with this extension I give up.

I have decided to stay with Platformio for the easy cases and command line debug for hard cases.

brianignacio5 commented 6 months ago

If it not to much to ask, could you describe which issues you faced with this extension and what cases you think we could improve ?