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

Cannot use debugpy with runserver_plus #1392

Closed
sauravkhakurel opened this issue Sep 21, 2023 · 2 comments
Closed

Cannot use debugpy with runserver_plus #1392

sauravkhakurel opened this issue Sep 21, 2023 · 2 comments

Comments

@sauravkhakurel
Copy link

sauravkhakurel commented Sep 21, 2023

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

Environment data

  • debugpy version: 1.8.0
  • OS and version: Ubuntu 22.0 LTS
  • Python version (& distribution if applicable, e.g. Anaconda): 3.10.12
  • Using VS Code or Visual Studio: VS Code
  • Using cookiecutter-django for project setup

Actual behavior

Once the debugpy listen assigns the port, the runserver_plus autoloads and error message is shown regarding port already in use.

Expected behavior

Even if the server reloads debugpy should check if the port is already in use and if its in use by debugpy it should be able to be reused again so that the server does not crash.

Steps to reproduce:

  1. Setup django project using cookiecutter-django using docker y as option

  2. After project setup, inorder to attach debugger, modiy the manage.py file to include the following:
    if os.environ.get("DEBUG"):
    import debugpy

    debugpy.listen(("0.0.0.0", 6087))

  3. The runserver_plus command is executed from the /start script of the compose file for django.

  4. On the first instance the port is assigned to debugpy but later the runserver_plus auto reloads and shows error of port already in use.

Setting runserver_plus with --no-reload arg is not the solution that I am looking for. Is there any other way this can be achieved?
Regards

@int19h
Copy link
Contributor

int19h commented Sep 22, 2023

The port can't be reused because the parent process is still running, so connecting to that port would debug it. This would be better handled by only attaching the debugger to the child process - i.e. only doing listen() in the auto-reloaded process. Is that possible to detect?

@sauravkhakurel
Copy link
Author

sauravkhakurel commented Sep 24, 2023

The port can't be reused because the parent process is still running, so connecting to that port would debug it. This would be better handled by only attaching the debugger to the child process - i.e. only doing listen() in the auto-reloaded process. Is that possible to detect?

Thanks for the insight. I was able to connect the debugger with this workaround in my manage.py

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")

    from django.conf import settings

    if settings.DEBUG:
        if os.environ.get("RUN_MAIN") or os.environ.get("WERKZEUG_RUN_MAIN"):
            import debugpy

            debugpy.listen(("0.0.0.0", 6087))
            print("Debugger Listening!")

I tried by only doing listen() on the child process. It works but uncaught exceptions are raised. For now, the above code is working fine with my current project setup. And os.fork was not available in Windows causing doing listen() on child process to be platform specific. Thanks for the help.

@int19h int19h closed this as completed Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants