Sanic: Can not enable debug when start sanic app in VSCode.

Created on 20 May 2019  Â·  4Comments  Â·  Source: sanic-org/sanic

Describe the bug
If set debug to true, it will start failed in VSCode.

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Dingpan",
            "type": "python",
            "request": "launch",
            "module": "dingpan.app"
        }
    ]
}

Terminal

(venv) ➜  server cd /Users/jagger/projects/dingpan/server ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Users/jagger/projects/dingpan/server/venv/bin/python /Users/jagger/.vscode/extensions/ms-python.python-2019.4.12954/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 50489 -m dingpan.app 
[2019-05-20 13:14:14 +0800] [2008] [DEBUG] 

                 Sanic
         Build Fast. Run Fast.


[2019-05-20 13:14:14 +0800] [2008] [INFO] Goin' Fast @ http://0.0.0.0:8000
E00000.470: Exception escaped from start_client

            Traceback (most recent call last):
              File "/Users/jagger/.vscode/extensions/ms-python.python-2019.4.12954/pythonFiles/lib/python/ptvsd/log.py", line 110, in g
                return f(*args, **kwargs)
              File "/Users/jagger/.vscode/extensions/ms-python.python-2019.4.12954/pythonFiles/lib/python/ptvsd/pydevd_hooks.py", line 74, in start_client
                sock, start_session = daemon.start_client((host, port))
              File "/Users/jagger/.vscode/extensions/ms-python.python-2019.4.12954/pythonFiles/lib/python/ptvsd/daemon.py", line 214, in start_client
                with self.started():
              File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py", line 112, in __enter__
                return next(self.gen)
              File "/Users/jagger/.vscode/extensions/ms-python.python-2019.4.12954/pythonFiles/lib/python/ptvsd/daemon.py", line 110, in started
                self.start()
              File "/Users/jagger/.vscode/extensions/ms-python.python-2019.4.12954/pythonFiles/lib/python/ptvsd/daemon.py", line 145, in start
                raise RuntimeError('already started')
            RuntimeError: already started


[2019-05-20 13:14:14 +0800] [2014] [ERROR] Experienced exception while trying to serve
Traceback (most recent call last):
  File "/Users/jagger/projects/dingpan/server/venv/lib/python3.7/site-packages/sanic/app.py", line 1094, in run
    reloader_helpers.watchdog(2)
  File "/Users/jagger/projects/dingpan/server/venv/lib/python3.7/site-packages/sanic/reloader_helpers.py", line 142, in watchdog
    worker_process = restart_with_reloader()
  File "/Users/jagger/projects/dingpan/server/venv/lib/python3.7/site-packages/sanic/reloader_helpers.py", line 65, in restart_with_reloader
    worker_process.start()
  File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/process.py", line 112, in start
    self._popen = self._Popen(self)
  File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/context.py", line 277, in _Popen
    return Popen(process_obj)
  File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/popen_fork.py", line 20, in __init__
    self._launch(process_obj)
  File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/popen_fork.py", line 70, in _launch
    self.pid = os.fork()
  File "/Users/jagger/.vscode/extensions/ms-python.python-2019.4.12954/pythonFiles/lib/python/ptvsd/_vendored/pydevd/_pydev_bundle/pydev_monkey.py", line 536, in new_fork
    _on_forked_process()
  File "/Users/jagger/.vscode/extensions/ms-python.python-2019.4.12954/pythonFiles/lib/python/ptvsd/_vendored/pydevd/_pydev_bundle/pydev_monkey.py", line 58, in _on_forked_process
    pydevd.settrace_forked()
  File "/Users/jagger/.vscode/extensions/ms-python.python-2019.4.12954/pythonFiles/lib/python/ptvsd/_vendored/pydevd/pydevd.py", line 2085, in settrace_forked
    patch_multiprocessing=True,
  File "/Users/jagger/.vscode/extensions/ms-python.python-2019.4.12954/pythonFiles/lib/python/ptvsd/_vendored/pydevd/pydevd.py", line 1844, in settrace
    stop_at_frame,
  File "/Users/jagger/.vscode/extensions/ms-python.python-2019.4.12954/pythonFiles/lib/python/ptvsd/_vendored/pydevd/pydevd.py", line 1894, in _locked_settrace
    debugger.connect(host, port)  # Note: connect can raise error.
  File "/Users/jagger/.vscode/extensions/ms-python.python-2019.4.12954/pythonFiles/lib/python/ptvsd/_vendored/pydevd/pydevd.py", line 855, in connect
    s = start_client(host, port)
  File "/Users/jagger/.vscode/extensions/ms-python.python-2019.4.12954/pythonFiles/lib/python/ptvsd/pydevd_hooks.py", line 132, in <lambda>
    _start_client = (lambda h, p: start_client(daemon, h, p))
  File "/Users/jagger/.vscode/extensions/ms-python.python-2019.4.12954/pythonFiles/lib/python/ptvsd/log.py", line 110, in g
    return f(*args, **kwargs)
  File "/Users/jagger/.vscode/extensions/ms-python.python-2019.4.12954/pythonFiles/lib/python/ptvsd/pydevd_hooks.py", line 74, in start_client
    sock, start_session = daemon.start_client((host, port))
  File "/Users/jagger/.vscode/extensions/ms-python.python-2019.4.12954/pythonFiles/lib/python/ptvsd/daemon.py", line 214, in start_client
    with self.started():
  File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "/Users/jagger/.vscode/extensions/ms-python.python-2019.4.12954/pythonFiles/lib/python/ptvsd/daemon.py", line 110, in started
    self.start()
  File "/Users/jagger/.vscode/extensions/ms-python.python-2019.4.12954/pythonFiles/lib/python/ptvsd/daemon.py", line 145, in start
    raise RuntimeError('already started')
RuntimeError: already started--- Logging error ---
Traceback (most recent call last):


During handling of the above exception, another exception occurred:



During handling of the above exception, another exception occurred:


(venv) ➜  server 

Code snippet
dingpan/app.py

from sanic import Sanic
from sanic.log import logger
from sanic.response import json

from . import config

app = Sanic('Dingpan', load_env=False)
app.config.from_object(config)
app.config.load_environment_vars(prefix='DP_')


@app.route('/')
async def test(request):
    logger.info('Here is your log')
    return json({'hello': 'world'})

if __name__ == '__main__':
    app.run(host=app.config['HOST'],
            port=app.config['PORT'],
            debug=app.config['DEBUG'],
            access_log=app.config['ACCESS_LOG'])

dingpan.config.py

HOST = '0.0.0.0'
PORT = 8000
DEBUG = True
ACCESS_LOG = True

Expected behavior
I want use VSCode to debug python code, and the auto reload feature of Sanic in debug mode.

Environment (please complete the following information):

  • PTVSD version: 2019.4.12954
  • OS and version: macOS 10.14.4
  • Python version (& distribution if applicable, e.g. Anaconda): 3.7.1
  • Sanic version: 19.3

Most helpful comment

This is how I managed to run within vscode debugger:

Instead of starting with app.run(**run_args), you add the app to an event loop and run it "forever":

    loop = asyncio.new_event_loop()
    loop.create_task(app.create_server(**run_args, return_asyncio_server=True))
    loop.run_forever()

All 4 comments

It seems the reason that the auto reload feature is conflict with VSCode's debug feature, disable auto reload will make them work together.

Thanks for the update. I'm going to close because I know we don't want to go down the path of debugging IDEs.

It seems the reason that the auto reload feature is conflict with VSCode's debug feature, disable auto reload will make them work together.

Disabling auto-reload makes no difference for me

This is how I managed to run within vscode debugger:

Instead of starting with app.run(**run_args), you add the app to an event loop and run it "forever":

    loop = asyncio.new_event_loop()
    loop.create_task(app.create_server(**run_args, return_asyncio_server=True))
    loop.run_forever()
Was this page helpful?
0 / 5 - 0 ratings