Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide APIs to stop listening / stop debugger #870

Closed
heartacker opened this issue Mar 12, 2022 · 17 comments
Closed

Provide APIs to stop listening / stop debugger #870

heartacker opened this issue Mar 12, 2022 · 17 comments
Labels
enhancement New feature or request

Comments

@heartacker
Copy link

heartacker commented Mar 12, 2022

Before creating a new issue, please check the FAQ to see if your question is answered there.

Environment data

  • debugpy version: 1.5.1
  • OS and version: win 10 1067
  • Python version (& distribution if applicable, e.g. Anaconda): Python3.10.0 (tags/v3.10.0:b494f59, Oct 4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)]
  • Using VS Code or Visual Studio: VSCode

Actual behavior

connect success

Expected behavior

image

Steps to reproduce:

  1. vscode launch.json
        {
            "name": "Py:13579",
            "type": "python",
            "request": "attach",
            "port": 13578,
            "host": "localhost",
            "justMyCode": false,
        },
  1. python file
# import ptvsd
import debugpy
if sets.PY_DEBUG_MODE == 2:
    # ptvsd.enable_attach(('0.0.0.0', 13579))
    # print("debug with PTVSD")
    # ptvsd.wait_for_attach(15)
    # print('attach success')
    # ptvsd.break_into_debugger()

    # use debugpy
    debugpy.listen(("0.0.0.0", 13579))
    print("debug with debugpy")
    debugpy.wait_for_client(15)
    print('attach success')
    debugpy.breakpoint()
  1. run the python file and then attach
Message:	timed out waiting for adapter to connect
TypeName:	timed out waiting for adapter to connect
StackTrc:	  File "D:\Program Files\Python310\lib\site-packages\debugpy\server\api.py", line 232, in listen
    raise RuntimeError("timed out waiting for adapter to connect")
  File "D:\Program Files\Python310\lib\site-packages\debugpy\server\api.py", line 141, in debug
    return func(address, settrace_kwargs, **kwargs)
  File "D:\Program Files\Python310\lib\site-packages\debugpy\server\api.py", line 143, in debug
    log.reraise_exception("{0}() failed:", func.__name__, level="info")
  File "D:\Program Files\Python310\lib\site-packages\debugpy\__init__.py", line 113, in listen
    return api.listen(address)
  File "F:\A......\script\AC784x\pll_demo.py", line 23, in <module>
    debugpy.listen(("0.0.0.0", 13579))
@heartacker heartacker changed the title porting for ptvsd fail porting from ptvsd fail Mar 12, 2022
@int19h
Copy link
Contributor

int19h commented Mar 14, 2022

Can you add this line before any other calls to debugpy:

debugpy.log_to(r"C:\Logs")  # adjust path as needed

and then share the debugpy*.log files that it'll generate in that folder?

@heartacker
Copy link
Author

I+00001.375: Windows-10-10.0.14393 AMD64
             CPython 3.10.0 (64-bit)
             debugpy 1.5.1

I+00001.406: Initial environment:
             
             System paths:
                 sys.prefix: D:\Program Files\Python310(F:\Program Files\Python310)
                 sys.base_prefix: D:\Program Files\Python310(F:\Program Files\Python310)
                 sys.real_prefix: <missing>
                 site.getsitepackages(): D:\Program Files\Python310(F:\Program Files\Python310)
                                         D:\Program Files\Python310\lib\site-packages(F:\Program Files\Python310\Lib\site-packages)
                 site.getusersitepackages(): C:\Users\ATC6194\AppData\Roaming\Python\Python310\site-packages
                 sys.path (site-packages): D:\Program Files\Python310\lib\site-packages(F:\Program Files\Python310\Lib\site-packages)
                 sysconfig.get_path('stdlib'): D:\Program Files\Python310\Lib(F:\Program Files\Python310\Lib)
                 sysconfig.get_path('platstdlib'): D:\Program Files\Python310\Lib(F:\Program Files\Python310\Lib)
                 sysconfig.get_path('purelib'): D:\Program Files\Python310\Lib\site-packages(F:\Program Files\Python310\Lib\site-packages)
                 sysconfig.get_path('platlib'): D:\Program Files\Python310\Lib\site-packages(F:\Program Files\Python310\Lib\site-packages)
                 sysconfig.get_path('include'): D:\Program Files\Python310\Include(F:\Program Files\Python310\include)
                 sysconfig.get_path('scripts'): D:\Program Files\Python310\Scripts(F:\Program Files\Python310\Scripts)
                 sysconfig.get_path('data'): D:\Program Files\Python310(F:\Program Files\Python310)
                 os.__file__: D:\Program Files\Python310\Lib\os.py(F:\Program Files\Python310\Lib\os.py)
                 threading.__file__: D:\Program Files\Python310\Lib\threading.py(F:\Program Files\Python310\Lib\threading.py)

D+00001.406: configure(None, {'subProcess': True})

D+00001.406: listen(('127.0.0.1', 13579), **{})

I+00001.406: Initial debug configuration: {
                 "qt": "none",
                 "subProcess": true,
                 "python": "F:\\ATCProjectFiles\\AutoTool_SVN\\atcgui\\ATCRelease\\MCU.sln.Release\\bin\\ATC.MCU.GUI.exe"
             }

I+00001.406: Waiting for adapter endpoints on 127.0.0.1:1603...

I+00001.406: debugpy.listen() spawning adapter: [
                 "F:\\ATCProjectFiles\\AutoTool_SVN\\atcgui\\ATCRelease\\MCU.sln.Release\\bin\\ATC.MCU.GUI.exe",
                 "D:\\Program Files\\Python310\\lib\\site-packages\\debugpy\\adapter",
                 "--for-server",
                 "1603",
                 "--host",
                 "127.0.0.1",
                 "--port",
                 "13579",
                 "--server-access-token",
                 "c87f29bab13eb30681cce6427d880c7d2436bd7a6c666b3ff7b2187f481e8a3c",
                 "--log-dir",
                 ".\\ABCD.log"
             ]

I+00011.485: Timed out waiting for adapter to connect:
             
             Traceback (most recent call last):
               File "D:\Program Files\Python310\lib\site-packages\debugpy\server\api.py", line 218, in listen
                 sock, _ = endpoints_listener.accept()
               File "D:\Program Files\Python310\Lib\socket.py", line 293, in accept
                 fd, addr = self._accept()
             TimeoutError: timed out
             
             Stack where logged:
               File "F:\ATCProjectFiles\AutoTool_SVN\atcgui\ATCRelease\MCU.sln.Release\script\AC784x\hfosc_trim.py", line 27, in <module>
                 debugpy.listen(13579)
               File "D:\Program Files\Python310\lib\site-packages\debugpy\__init__.py", line 113, in listen
                 return api.listen(address)
               File "D:\Program Files\Python310\lib\site-packages\debugpy\server\api.py", line 141, in debug
                 return func(address, settrace_kwargs, **kwargs)
               File "D:\Program Files\Python310\lib\site-packages\debugpy\server\api.py", line 229, in listen
                 log.swallow_exception(
               File "D:\Program Files\Python310\lib\site-packages\debugpy\common\log.py", line 218, in swallow_exception
                 _exception(format_string, *args, **kwargs)
             

I+00011.485: listen() failed:
             
             Traceback (most recent call last):
               File "D:\Program Files\Python310\lib\site-packages\debugpy\server\api.py", line 218, in listen
                 sock, _ = endpoints_listener.accept()
               File "D:\Program Files\Python310\Lib\socket.py", line 293, in accept
                 fd, addr = self._accept()
             TimeoutError: timed out
             
             During handling of the above exception, another exception occurred:
             
             Traceback (most recent call last):
               File "D:\Program Files\Python310\lib\site-packages\debugpy\server\api.py", line 141, in debug
                 return func(address, settrace_kwargs, **kwargs)
               File "D:\Program Files\Python310\lib\site-packages\debugpy\server\api.py", line 232, in listen
                 raise RuntimeError("timed out waiting for adapter to connect")
             RuntimeError: timed out waiting for adapter to connect
             
             Stack where logged:
               File "F:\ATCProjectFiles\AutoTool_SVN\atcgui\ATCRelease\MCU.sln.Release\script\AC784x\hfosc_trim.py", line 27, in <module>
                 debugpy.listen(13579)
               File "D:\Program Files\Python310\lib\site-packages\debugpy\__init__.py", line 113, in listen
                 return api.listen(address)
               File "D:\Program Files\Python310\lib\site-packages\debugpy\server\api.py", line 143, in debug
                 log.reraise_exception("{0}() failed:", func.__name__, level="info")
               File "D:\Program Files\Python310\lib\site-packages\debugpy\common\log.py", line 226, in reraise_exception
                 _exception(format_string, *args, **kwargs)
             


@int19h
Copy link
Contributor

int19h commented Mar 15, 2022

Hm, is that the only file that was there? It's supposed to create several, one for each component of the debugger - I would be particularly interested in the one named debugpy.adapter...log if it's there.

@heartacker
Copy link
Author

no, there is only one log

@heartacker
Copy link
Author

heartacker commented Mar 15, 2022

顺便说一句,我正在使用pythonnet ,python代码是从c#代码

@int19h
Copy link
Contributor

int19h commented Mar 15, 2022

Ohh, I see the reason now. Because you're hosting Python inside of your own app, sys.executable points to your app's binary rather than the Python interpreter - and debugpy needs access to the Python interpreter in order to spawn background processes.

The workaround is to explicitly specify the interpreter by using debugpy.configure before listen:

debugpy.configure(python=r'c:\python310\python.exe')  # adjust as needed

@heartacker
Copy link
Author

heartacker commented Mar 16, 2022

great, but I can not attach twice.

Message:	Can't listen for client connections: [WinError 10048] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。
TypeName:	Can't listen for client connections: [WinError 10048] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。
StackTrc:	  File "D:\Program Files\Python310\lib\site-packages\debugpy\server\api.py", line 243, in listen
    raise RuntimeError(str(endpoints["error"]))
  File "D:\Program Files\Python310\lib\site-packages\debugpy\server\api.py", line 141, in debug
    return func(address, settrace_kwargs, **kwargs)
  File "D:\Program Files\Python310\lib\site-packages\debugpy\server\api.py", line 143, in debug
    log.reraise_exception("{0}() failed:", func.__name__, level="info")
  File "D:\Program Files\Python310\lib\site-packages\debugpy\__init__.py", line 113, in listen
    return api.listen(address)
  File "F:\ATCProjectFiles\AutoTool_SVN\atcgui\ATCRelease\MCU.sln.Release\script\AC784x\hfosc_trim.py", line 27, in <module>
    debugpy.listen(13579)
   at ATC.Studio.TpmsRunner.CodeRunnerV1_0.PyCodeExcAndDataDeal(PyModule ps, RunFlagType rflg, String code, String filepath, TestSettings tsst, PyDict locals) in F:\ATCProjectFiles\AutoTool_SVN\atcgui\ATC.Studio\TpmsRunner\CodeRunnerV1_0.cs:line 45
   at ATC.Studio.CodeStudio.ExecuteCode(String codestr, TestSettings settings, RunFlagType rflg, String filepath, Dictionary`2 psValue, PyDict locals) in F:\ATCProjectFiles\AutoTool_SVN\atcgui\ATC.Studio\CodeStudio.xaml.cs:line 639

@int19h
Copy link
Contributor

int19h commented Mar 16, 2022

If I understand the error message correctly, you're trying to call listen() twice with the same port number. Since the first listen already installed a debug server on that port, the second one fails. You need to ensure that listen() is only run once during the lifetime of your process - or, more precisely, during the lifetime of the Python interpreter instance on which this code is running.

@heartacker
Copy link
Author

hi @int19h, is there any api like debugpy.exit() or debugpy.unlisten() for us to exit the debug/trace

@fabioz
Copy link
Collaborator

fabioz commented Apr 1, 2022

We don't currently have an api to stop listening a previous debugpy.listen() or exiting the debugger in debugpy (those would be 2 new APIs).

@fabioz fabioz added enhancement New feature or request and removed waiting for response labels Apr 1, 2022
@fabioz fabioz changed the title porting from ptvsd fail Provide APIs to stop listening / step debugger Apr 1, 2022
@fabioz fabioz changed the title Provide APIs to stop listening / step debugger Provide APIs to stop listening / stop debugger Apr 1, 2022
@int19h
Copy link
Contributor

int19h commented Apr 1, 2022

Note that any new APIs won't help in this particular case, since @heartacker is on Python 2.7, which we no longer support in new debugpy releases.

One other possibility that remains is to use debugpy.connect() (and the corresponding launch.json with "listen").

@heartacker
Copy link
Author

no, I am using python 3.10, but we hosted python in my own c# app. when my code run to end, the c#-host-app still alivea and will not close the listening, so that we couldn't listen again by running the debugpy.listen(),

thus, we want some api to stop the listening at the end of my python code hosted in c#-app

@heartacker
Copy link
Author

hi, is there any one can help me, I really need some api to stop the listening because we don't need to exit the host app which embed the python engine after finishing my py script

@heartacker
Copy link
Author

maybe it will be better
: if the host listen twice or more with same address and port, debugpy should allow and consider it as Effective action

please

@int19h
Copy link
Contributor

int19h commented Jun 1, 2022

This makes sense. We can remember the host/port; for that matter, it might be convenient to expose it in the public API also, on top of returning it from listen().

@heartacker
Copy link
Author

great, thanks and hope that we could earn this ASAP 👯

@int19h
Copy link
Contributor

int19h commented Feb 1, 2023

Closing this one as there is a separate issue to track repeated listen() calls (#460) and another one for the API (#297)

@int19h int19h closed this as completed Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants