GoogleContainerTools / skaffold

Easy and Repeatable Kubernetes Development
https://skaffold.dev/
Apache License 2.0
14.96k stars 1.62k forks source link

Skaffold debug breakpoints not hitting in Python with uvicorn --reload #8251

Open usrbinsam opened 1 year ago

usrbinsam commented 1 year ago

I am developing in FastAPI + uvicorn. When using skaffold dev with file sync mode, I enable the uvicorn hot reloader. This works as intended. When switching to skaffold debug, I have to disable the uvicorn reloader in order for breakpoints to hit.

In order to work around this, I have to modify my Dockerfile whenever I want to use skaffold debug. I think I can also move the uvicorn options to main.py and only set reload=True when not running under a debugger.

FWIW, when PyCharm invokes pydevd directly, it sets the --multiprocess argument and uvicorn's reload works. Here is PyCharm's Python Debug:

C:\Users\minic\AppData\Local\Programs\Python\Python310\python.exe C:/Users/minic/AppData/Local/JetBrains/Toolbox/apps/PyCharm-P/ch-0/223.7571.203/plugins/python/helpers/pydev/pydevd.py --module --multiprocess --client 127.0.0.1 --port 51724 --file uvicorn app:app --reload --port 5001 --host 0.0.0.0 --use-colors 

Expected behavior

Breakpoints should hit in PyCharm as documented.

Actual behavior

Breakpoint never hits. Manually pausing the debugger breaks in the watchfiles module, main.py.

Information

---
apiVersion: skaffold/v4beta1
kind: Config
metadata:
  name: apps
build:
  local:
    # tryImportMissing: true
    concurrency: 0
  artifacts:
    - image: fastapi-debug-test
      sync:
        infer:
          - main.py
  platforms:
    - linux/amd64

manifests:
  rawYaml:
    - ./deployment.yaml

deploy:
  kubectl: {}

portForward:
  - resourceName: fastapi-deployment
    resourceType: deployment
    port: 8080

Steps to reproduce the behavior

See README in https://github.com/usrbinsam/skaffold-debug-issue

Kludex commented 1 year ago

This is not a bug. Don't use --reload flag from uvicorn with Skaffold. Use Skaffold hot-reload capabilities.