microsoft/vscode-python

Debug adapter process has terminated unexpectedly

DonJayamanne opened this issue · 40 comments

From @zeus on January 7, 2017 17:23

Environment data

VS Code version: 1.8.1
Python Extension version: 0.5.5
Python Version: 2.7.13
OS and version: Windows 10
Your launch.json (if dealing with debugger issues):

        {
        
            "name": "Attach (Remote Debug)",
            "type": "python",
            "request": "attach",
            "localRoot": "${workspaceRoot}",
            "remoteRoot": "${workspaceRoot}",
            "port": 4000,
            "secret": "my_secret",
            "host":"localhost"
        }

Your settings.json:

Logs

Output from Python output panel

None

Output from Console window (Help->Developer Tools menu)

index.js:22 [uncaught exception]: Error: TypeError: Cannot read property 'Threads' of undefined
    at PythonDebugger.threadsRequest (C:\Users\zeus\.vscode\extensions\donjayamanne.python-0.5.5\out\client\debugger\Main.js:321:27)
    at PythonDebugger.DebugSession.dispatchRequest (C:\Users\zeus\.vscode\extensions\donjayamanne.python-0.5.5\node_modules\vscode-debugadapter\lib\debugSession.js:421:22)
    at PythonDebugger.ProtocolServer._handleData (C:\Users\zeus\.vscode\extensions\donjayamanne.python-0.5.5\node_modules\vscode-debugadapter\lib\protocol.js:104:38)
    at Socket.<anonymous> (C:\Users\zeus\.vscode\extensions\donjayamanne.python-0.5.5\node_modules\vscode-debugadapter\lib\protocol.js:24:60)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at Pipe.onread (net.js:543:20)onError @ index.js:22(anonymous function) @ index.js:91emitOne @ events.js:96emit @ events.js:188window.onerror @ C:\Program Files (x86)\Microsoft VS Code\resources\electron.asar\renderer\init.js:120i.globals.onerror @ errorTelemetry.ts:64
index.js:25 Error: TypeError: Cannot read property 'Threads' of undefined
    at PythonDebugger.threadsRequest (C:\Users\zeus\.vscode\extensions\donjayamanne.python-0.5.5\out\client\debugger\Main.js:321:27)
    at PythonDebugger.DebugSession.dispatchRequest (C:\Users\zeus\.vscode\extensions\donjayamanne.python-0.5.5\node_modules\vscode-debugadapter\lib\debugSession.js:421:22)
    at PythonDebugger.ProtocolServer._handleData (C:\Users\zeus\.vscode\extensions\donjayamanne.python-0.5.5\node_modules\vscode-debugadapter\lib\protocol.js:104:38)
    at Socket.<anonymous> (C:\Users\zeus\.vscode\extensions\donjayamanne.python-0.5.5\node_modules\vscode-debugadapter\lib\protocol.js:24:60)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at Pipe.onread (net.js:543:20)
    at file:///C:/Program Files (x86)/Microsoft VS Code/resources/app/out/vs/workbench/electron-browser/workbench.main.js:90:21136
    at Object.v [as _notify] (file:///C:/Program Files (x86)/Microsoft VS Code/resources/app/out/vs/workbench/electron-browser/workbench.main.js:32:13263)
    at Object.enter (file:///C:/Program Files (x86)/Microsoft VS Code/resources/app/out/vs/workbench/electron-browser/workbench.main.js:32:16768)
    at _run (file:///C:/Program Files (x86)/Microsoft VS Code/resources/app/out/vs/workbench/electron-browser/workbench.main.js:32:18089)
    at _error (file:///C:/Program Files (x86)/Microsoft VS Code/resources/app/out/vs/workbench/electron-browser/workbench.main.js:32:17606)
    at file:///C:/Program Files (x86)/Microsoft VS Code/resources/app/out/vs/workbench/electron-browser/workbench.main.js:85:1178
    at _.e.dispatch (file:///C:/Program Files (x86)/Microsoft VS Code/resources/app/out/vs/workbench/electron-browser/workbench.main.js:85:2523)
    at _.e.handleData (file:///C:/Program Files (x86)/Microsoft VS Code/resources/app/out/vs/workbench/electron-browser/workbench.main.js:85:2035)
    at Socket.<anonymous> (file:///C:/Program Files (x86)/Microsoft VS Code/resources/app/out/vs/workbench/electron-browser/workbench.main.js:85:1038)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)

Actual behavior

Debug adapter process has terminated unexpectedly

Expected behavior

Attach to simple process

Steps to reproduce:

  • Simple program:
#!/usr/bin/env python
import ptvsd
ptvsd.enable_attach("my_secret", address=('0.0.0.0', 4000))
ptvsd.wait_for_attach()
t = 1+1
  • Run
  • Run Debug, failed

Other

Connected from MS VS 2015 successfully.
Can't connect from VSC with 'attach' option. 'Launch' works fine.

Copied from original issue: DonJayamanne/pythonVSCode#626

From @zeus on February 20, 2017 20:51

VS Code version: 1.9.1
Python Extension version: 0.5.8

Nothig changed. Is there any ideas where to dig into?

From @dholth on March 6, 2017 22:2

I have also had trouble remote debugging a vagrant vm. Even after opening and forwarding the port in the firewall. Sometimes it works consistently and sometimes it fails. It seems to work more reliably when remote debugging a program running on the same machine.

Experiments

ptvsd.enable_attach(None, ...) # no secret. set secret: "" in launch.json

The first time I removed the secret, it worked. I thought I had fixed it, but then it stopped working again.

time.sleep(1); ptvsd.wait_for_attach() # could there be a race condition?

print secret # in ptvsd/attach_server.py somehow, this also fixed the problem for a little while.

There was something in attach_server suggesting the secret might confuse things when the remote process is behind a NAT (debugger connects to 127.0.0.1, but debugged sees connection from 10.)...

Mysterious.

From @zeus on March 6, 2017 22:29

Just upgraded to extension 0.5.9 and vsc 1.10.1

Now i've got this in console:

Unhandled exception in thread started by <bound method DebuggerLoop.loop of <ptvsd.visualstudio_py_debugger.DebuggerLoop object at 0x0000000003826898>>
PS C:\Users\zeus\OneDrive\coding\big_fat\ostankino_crm> python .\vscdebug_test.py
Unhandled exception in thread started by
sys.excepthook is missing
lost sys.stderr

From @itowlson on March 10, 2017 3:19

I am seeing this issue when I specify a secret in enable_attach. Without a secret, the debugger attaches to the remote server and all is well. With a secret, I get the Debug adapter process has terminated unexpectedly error.

I don't see the console log messages other users have mentioned. However, I am using Kubernetes port forwarding to connect localhost:3000 to the remote port 3000, and Kubernetes reports the following error, which reads like the target process is rejecting the request with the secret.

E0310 15:38:12.157798   12360 portforward.go:314] error copying from local connection
to remote stream: read tcp4 127.0.0.1:3000->127.0.0.1:60994: wsarecv: An existing
connection was forcibly closed by the remote host.

From @itowlson on March 10, 2017 3:33

Per @dholth's comment, I'm on a corporate network (and connecting via the port forwarder), so this could be related. However, it works when I run the PTVS debugger from within Visual Studio 2015, so it's not solely a network configuration issue.

From @GiuseppeLaurenza on May 1, 2017 10:34

i do not know if it is better to open another issue than write here, but for now I prefer to extend this one.
I have the same problem with pool.map instruction

from multiprocessing import Pool, cpu_count
def foo(arg):
    print arg
if __name__ == '__main__':
    list_files = ["a", "b", "c"]
    pool = Pool(processes=(cpu_count() - 1))
    pool.map(foo, list_files)```

using debugger of other IDE (like pycharm) and debug in terminal with pdb, I have not any problem

From @WasabiFan on May 2, 2017 4:43

I am seeing the same issue. The log is available here. This seems to happen regardless of whether I have a secret configured.

From @zWaR on May 18, 2017 21:26

I am also getting this problem. The log in the Console is:

Debug adapter process has terminated unexpectedly
e.doShow @ messageService.ts:125
e.show @ messageService.ts:104
g.onServerExit @ rawDebugSession.ts:517
(anonymous) @ rawDebugSession.ts:431
emitTwo @ events.js:111
emit @ events.js:191
__dirname.ChildProcess._handle.onexit @ internal/child_process.js:215

From @zWaR on May 19, 2017 17:52

In the python output panel I had the following error:

----------Generating Tags----------
ctags --options=/Users/nojvek/.vscode/extensions/donjayamanne.python-0.5.9/resources/ctagOptions --languages=Python --exclude=/site-packages/ -o /Users/nojvek/mp/analytics/.vscode/tags .
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ctags: illegal option -- -
usage: ctags [-BFadtuwvx] [-f tagsfile] file ...

I was able to fix this by installing ctags with homebrew:
brew install ctags

However this does not stop debugger from crashing when attaching to a remote end.

From @bpartridge on May 25, 2017 0:39

Also getting this problem on Mac. It seems this.pythonProcess is being cleared, but other callbacks are assuming it exists, thus the "Threads of undefined" errors. The root cause of the early clearing may be related to this type of error:

messageService.ts:125 There was an error in starting the debug server. Error = {"code":"ETIMEDOUT","errno":"ETIMEDOUT","syscall":"connect","address":"127.0.0.1","port":8030}

Interestingly, telnetting into that port also fails to receive anything. Is there documentation on the raw protocol? It's possible that something's broken on the PTVSD side, or we're connecting wrong somehow...

From @etchen99 on May 28, 2017 19:20

I am also running into this issue on OSX. extension version 0.6.4. I get the ctags output message in the Python output window and the messageService error on the Console, but nothing all that useful.

From @Kenuat on June 1, 2017 13:24

Same problem here, Windows 8.1 latest versions of Code and extension, verbose output:

Debug adapter process has terminated unexpectedly
e.doShow @ messageService.ts:125
e.show @ messageService.ts:104
g.onServerExit @ rawDebugSession.ts:517
(anonymous) @ rawDebugSession.ts:431
emitTwo @ events.js:111
emit @ events.js:191
__dirname.ChildProcess._handle.onexit @ internal/child_process.js:215

From @mostafaeweda on August 16, 2017 16:28

I'm hitting the same issue

Please ensure you are using version 3.0.0 of PTVSD

From @mostafaeweda on August 16, 2017 23:0

@DonJayamanne I'm using version 3.0.0 and still hitting various attach issues (including this) - will include stack traces when I hit it again.

From @marcosfede on August 24, 2017 3:42

Using version 3.0.0 of ptvsd, trying to connect shows Debug adapter process has terminated unexpectedly
my launch.json is

        {
            "name": "Attach Docker (Remote Debug)",
            "type": "python",
            "request": "attach",
            "localRoot": "${workspaceRoot}",
            "remoteRoot": "/code",
            "port": 3000,
            "secret": "",
            "host": "localhost"
        }

From @royrwood on September 3, 2017 12:33

I'm also having problems, even with v3.0.0 of ptvsd.

I can connect to my remote Python app, but stepping through the code is erratic. Breakpoints are skipped, and single-stepping results in multiple lines executed.

Eventually the remote process dies and prints this to console:

Unhandled exception in thread started by 
sys.excepthook is missing
lost sys.stderr

This is running on an Ubuntu 16.10 system, VS 1.15.1, ptvsd 3.0.0.

Visual Studio Code version:

Version 1.15.1
Commit 41abd21afdf7424c89319ee7cb0445cc6f376959
Date 2017-08-16T17:16:27.618Z
Shell 1.6.6
Renderer 56.0.2924.87
Node 7.4.0
Architecture x64

launch.json:

    {
        "name": "Attach (localhost 1337)",
        "type": "python",
        "request": "attach",
        "localRoot": "${workspaceRoot}",
        "remoteRoot": "/home/rrwood/Python/debug_test",
        "port": 1337,
        "secret": "secret",
        "host": "localhost"
    }

Sample code:

from future import print_function
import ptvsd
print('Waiting for remote debugger connection...')
ptvsd.enable_attach('secret', address=('0.0.0.0', 1337))
ptvsd.wait_for_attach()
print('Remote debugger connected.')

print('Hello, world.')

max_val = 5

for i in range(1, max_val):
print('i = ', i)

Output:

$ python count.py
Waiting for remote debugger connection...
Remote debugger connected.
Hello, world.
i = 1
i = 2
i = 3
i = 4
Unhandled exception in thread started by
sys.excepthook is missing
lost sys.stderr

I had to down grade to ptvsd 2.2.0 to make the remote debugging to work from a host machine into a docker container. It only works with that version and not 3.0.0 or higher.

@ddehghan
Would you be able too provide some sample code (GitHub repo) that we could use to replicate this issue. Please do provide all information such as python, os versions, etc

Guys, same error here. But, manually removing Python extension and reinstall it solve the problem... Unfortunately did not keep old files...
VS Code: 1.21.1
Python 3.6.3
Windows 10 Pro v.1709

same problem here, latest vscode-python

Getting this same error as well with the simplest of attempts. Debugger never connects (server waits forever)
and I get the same error from VSC "Debug adapter process has terminated unexpectedly"
OS: Windows 10 Pro
VSC Version: 1.22.1
Python 2.7.14
Python Extension: 2018.3.1
ptvsd: 3.0.0

launch.json:

   {
            "name": "Python: Attach",
            "type": "python",
            "request": "attach",
            "localRoot": "${workspaceFolder}",
            "remoteRoot": "${workspaceFolder}",
            "port": 3000,
            "secret": "",
            "host": "localhost"
        },

@gnatty7

Getting this same error as well with the simplest of attempts

@DonJayamanne
Thanks for the instructions. I ran through them and have the following results:
I was able to get the remote debugging to work once, and only once when using Step 4, Option 1 to execute sample.py
I created a virtual environment for Python 2.7.14, and set it as the interpreter in Visual Studio Code
Refer to these screenshots:
image

image

Note
One thing I had to do was change my ExecutionPolicy so the activate.ps1 script would properly run in the shell (see https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-6&viewFallbackFrom=powershell-Microsoft.PowerShell.Core)

Like I said, I was only able to get it to work just once. When I ran the sample.py from a virtualenv run in a normal command shell (cmd.exe), it did not work, and gave me the same error.

When I couldn't run it again using the one method that did work, I tried uninstalling and reinstalling the Python plugin, and trying again with the exact same settings, but to no avail.

Here's a netstat run for that first screenshot

C:\Program Files (x86)\Microsoft Visual Studio 14.0>netstat -an -p tcp | find "3000"
  TCP    0.0.0.0:3000           0.0.0.0:0              LISTENING

@gnatty7

Please could you try this once again with the new debugger (though its still experimental)

  • Please could you ensure you have the latest version of the python extension installed
  • Uninstall ptvsd from the environment
  • Install the latest pre-release version (pip install ptvsd --pre)
  • Change the line from ptvsd.enable_attach("my_secret", address) to ptvsd.enable_attach(address)
  • Finally change the "type":"python" in launch.json to "type":"pythonExperimental"
  • Try debugging

Hi @DonJayamanne,

I seem to have the same problem. I get the error message "Debug adapter process has terminated unexpectedly".

I have tried using the experimental branch as you suggested. I don't see any errors or messages at all but the debugger does not connect.

Please let me know what information I can provide to help you debug the issue.

Thanks!

@nrempel
Please could you try a very simple form a remote debugging

Note: With the above repo, you'll be using the older version of the debugger and PTVSD version 3.0.0
Let me know how that goes.

Hello,

I tried those steps and was successful with both.

My use case is actually django in a docker container so I tested with this project: https://github.com/DonJayamanne/vscode-python-samples/tree/master/remote-debugging-docker-django

This project seems to connect fine so I will look at differences between my project and this one such as python versions etc.

One thing that doesn't seem to work on the remote-debugging-docker-django is breakpoints. They all show up as 'unverified breakpoint'.

Thank you. If I'm able to reproduce my issue with a different python version or something I'll open a new issue.

One thing that doesn't seem to work on the remote-debugging-docker-django is breakpoints. They all show up as 'unverified breakpoint'.

We're currently working on a new (improved) debugger. Currently referred to as experimental debugger. This issue of unverified breakpoint should be fine in this experimental debugger.

However, lets not start using the experimental debugger just yet. Lets keep things simple and identify what's wrong with your code (settings), then move onto other issues.

@nrempel and thanks for getting back.

@DonJayamanne thanks very much for the quick reply.

I created a new issue since it seemed a little off topic for this thread: #1600

Let me know if you want to continue in this thread and I'll copy over the information to here.

I was able to get the debugger running in my main project by using the django development server instead of gunicorn. But it suffered from the same problem of unverified breakpoints. I'm hoping it is the same issue as in your example repository since that is a much simpler project than the one I'm working on.

Hi @DonJayamanne
So, I've tried the steps and I believe I'm stuck with a possible incompatibility with the experimental PTVSD and how my remote Python interpreter works.

When I try sourcing the python script, or loading the .py file as a plugin, I get this type of error:

# Error: line 0: AttributeError: file C:\Program Files\Autodesk\Maya2017\Python\lib\site-packages\ptvsd\pydevd_hooks.py line 67: 'module' object has no attribute '__file__' # 

This is the same error as I get when trying to use ptvsd directly inside of an interactive session:

C:\WINDOWS\system32>pip --version
pip 10.0.1 from c:\python27\lib\site-packages\pip (python 2.7)

C:\WINDOWS\system32>pip install ptvsd --pre
Requirement already satisfied: ptvsd in c:\python27\lib\site-packages (4.1.1a1)

C:\WINDOWS\system32>python
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ptvsd
warning: Debugger speedups using cython not found. Run '"C:\Python27\python.exe" "C:\Python27\lib\site-packages\ptvsd\py
devd\setup_cython.py" build_ext --inplace' to build.
>>> ptvsd.enable_attach()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\ptvsd\attach_server.py", line 74, in enable_attach
    ptvsd_enable_attach(address, redirect_output, on_attach=_attached.set)
  File "C:\Python27\lib\site-packages\ptvsd\_main.py", line 127, in enable_attach
    **kwargs)
  File "C:\Python27\lib\site-packages\ptvsd\pydevd_hooks.py", line 67, in install
    if __main__ is not pydevd and __main__.__file__ == pydevd.__file__:
AttributeError: 'module' object has no attribute '__file__'
>>> ^Z


C:\WINDOWS\system32>vim C:\temp.py

C:\WINDOWS\system32>type C:\temp.py
import ptvsd
ptvsd.enable_attach()

print("Got here...")

C:\WINDOWS\system32>python C:\temp.py
warning: Debugger speedups using cython not found. Run '"C:\Python27\python.exe" "C:\Python27\lib\site-packages\ptvsd\pydevd\setup_cython.py" build_ext --inplace' to build.
Got here...

C:\WINDOWS\system32>

So, at this point, I'm unable to test remote debugging using the experimental debugger.

Hi @DonJayamanne
So, I did try the traditional approach of just executing a Python script on the command-line, (not using Maya) and I was able to get the debugger to properly attach, and hit breakpoints.

It was a bit flaky though. Using ptvsd_wait_for_attach() would often time-out, and even when it did work, it didn't always stop at the first breakpoint set in the source file.

However, it seems like PTVSD needs some work to support an execution environment like Maya's embedded interpreter.

@gnatty7

  • You can only use ptvsd.enable_attach from within a script file (not the repl)

  • We've fixed a few issues related to remote debugging in the experimental debugger. Please could you try installing the latest version using python -m pip install git+https://github.com/Microsoft/ptvsd/

    • Note, we haven't released a new version with the fixes, hence the request to pip install directly from source.
  • Then try the following:

  • Create a python file named temp.py:

import time
import ptvsd

ptvsd.enable_attach()
time.sleep(10)

print("Got here...")
print("HELLO")
  • The time.sleep is necessary to work around a known bug.
  • Open VS Code, add a break point to the print statements.
  • Run the above file python temp.py
  • Attach from VS Code with the following settings in launch.json within 5 seconds or increase the time.sleep from 5s to 10s:
        {
            "name": "Attach",
            "type": "pythonExperimental",
            "request": "attach",
            "port": 5678,
            "host": "localhost"
        },

Hi @DonJayamanne. It's been a few weeks, so I apologize for not getting back to this sooner. I was wondering what the status of the experimental debugger is. E.g. has been been released as an official wheel yet, or is the latest version still requiring an explicit github URL to use?

@gnatty7 you can install it using pip install ptvsd --upgrade --pre as it's still listed as an alpha.

Thanks @brettcannon . Any news on whether an embedded Python environment will work with the debugger, like in Maya? This is something that PyCharm supports for example.

@gnatty7 yes, it should work.

Hi @brettcannon, @DonJayamanne
I finally got a chance to try this out today, and it works! I even tried it in Maya, and I'm able to step through the code, inspect variables, and even use the interactive debugger, all within VS Code.
Many thanks for getting this working. Looking forward to this moving out of alpha.

It should be fixed, please test the latest development version of the extension. Instructions can be found here https://github.com/Microsoft/vscode-python/blob/master/CONTRIBUTING.md#development-build

If not, please feel free to re-open or comment on this issue and I'll re-open it.

I've had no luck getting the python debugger to work at all. Details are here https://stackoverflow.com/questions/52826127/visual-studio-code-python-debugger-seg-fault .