Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Flask app reload raises exception on repeat calls to enable_attach #1152

Closed
json2d opened this issue Feb 14, 2019 · 2 comments
Closed

Flask app reload raises exception on repeat calls to enable_attach #1152

json2d opened this issue Feb 14, 2019 · 2 comments

Comments

@json2d
Copy link

json2d commented Feb 14, 2019

I'm trying to integrate ptvsd with a flask app that has reloading feature enabled (as in it restarts when files are changed). This causes the ptvsd boilerplate code to be re-ran each time a reload happens, which results in a raised exception on repeat calls to ptvsd.enable_attach:

Environment data

  • PTVSD version: 4.2.3
  • OS and version: Windows 10 Pro 1703
  • Python version (& distribution if applicable, e.g. Anaconda): 3.6.8
  • Using VS Code or Visual Studio: VS Code

Actual behavior

raises exception on repeat calls to ptvsd.enable_attach:

OSError(10048, 'Only one usage of each socket address (protocol/network address/port) is normally permitted', None, 10048, None)

afterwards attaching to debugger works but does not work properly (breakpoints stop getting caught)

Expected behavior

handle reloading gracefully.

Steps to reproduce:

check out this repo which demos this issue

  1. git clone https://github.com/bitstrider/ptvsd-flask-reload.git
  2. pip install -r requirements.txt
  3. open VS code and add project to workspace
  4. ./run.sh
  5. wait for error to print to terminal
@karthiknadig
Copy link
Member

@bitstrider You will need #1153 for this to work correctly. But, there are some changes to how you launch flask using ptvsd.

  1. you don't need to call ptvsd.enable_attach if you are already using ptvsd to launch flask module in run.sh.
    app.py:
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, VSCode!'
  1. Use --multiprocess flag and --wait flags. --multiprocess flag enables subprocess tracking in ptvsd. You will need wait because, flask may start the child process before you can connect to the main process. VSC will not be able to connect to already started sub-process. Running the debugger with wait will block flask from running until you connect to the debugger.
    -m ptvsd --host localhost --port 5678 --multiprocess --wait -m flask run --host=localhost --no-debugger

  2. Use this as attach configuration in VSC:

        {
            "name": "Flask (remote)",
            "type": "python",
            "request": "attach",
            "port": 5678,
            "host": "localhost",
            "jinja": true, // for template debugging
            "subProcess": true, // for sub-process debugging
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "."
                },
            ]
        }

That will setup VSC to automatically attach to child process.
image

This is with the fix in #1153.

@json2d
Copy link
Author

json2d commented Feb 15, 2019

hey @karthiknadig thanks for all that, looking forward to the release 💯

  1. You will need wait because, flask may start the child process before you can connect to the main process. VSC will not be able to connect to already started sub-process.

good to know in general

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants