remote debug in vscode using debugpy not work #13102

Closed
opened 2026-02-21 19:10:34 -05:00 by yindo · 3 comments
Owner

Originally created by @cuckootan on GitHub (Apr 17, 2025).

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

0.15.3

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

I start a dify api server in a shell using cmd as follows:

python3 -m debugpy --listen 5678 -m flask run --host 0.0.0.0 --port=5001 --debug --no-debugger --no-reload

and then I want to connect debug port above in my vscode. The launch.json configuration is:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach",
            "type": "debugpy",
            "request": "attach",
            "connect": {
                "host": "localhost",
                "port": 5678,
            },
            "justMyCode": false,            
        }
    ]
}

I am sure the connection is successful. Then I add some breakpoints in one HTTP api. But when I send http request to that api, it can't stop in those breakpoints in vscode.

Well I try to inspect the code in api/app.py, and seem to find some logic error:

# create app
if is_db_command():
    from app_factory import create_migrations_app

    app = create_migrations_app()
else:
    # It seems that JetBrains Python debugger does not work well with gevent,
    # so we need to disable gevent in debug mode.
    # If you are using debugpy and set GEVENT_SUPPORT=True, you can debug with gevent.
    if (flask_debug := os.environ.get("FLASK_DEBUG", "0")) and flask_debug.lower() in {
        "false",
        "0",
        "no",
    }:
        print("Using gevent")
        from gevent import monkey  # type: ignore

        # gevent
        monkey.patch_all()

        from grpc.experimental import gevent as grpc_gevent  # type: ignore

        # grpc gevent
        grpc_gevent.init_gevent()

        import psycogreen.gevent  # type: ignore

        psycogreen.gevent.patch_psycopg()

    from app_factory import create_app

    app = create_app()
    celery = app.extensions["celery"]

I think it should be:

if (flask_debug := os.environ.get("FLASK_DEBUG", "0")) and flask_debug.lower() in {
        "true",
        "1",
        "yes",
    }:

Meanwhile, I find FLASK_DEBUG will be set to 1 by flask when exec cmd using --debug flag like above.

I'm not sure, look forward to reply~

✔️ Expected Behavior

exec cmd in one shell:

python3 -m debugpy --listen 5678 -m flask run --host 0.0.0.0 --port=5001 --debug --no-debugger --no-reload

and then I can debug service above by connecting the debug port in my vscode successfully.

Actual Behavior

No response

Originally created by @cuckootan on GitHub (Apr 17, 2025). ### Self Checks - [x] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [x] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [x] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [x] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [x] Please do not modify this template :) and fill in all the required fields. ### Dify version 0.15.3 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce I start a dify api server in a shell using cmd as follows: ```shell python3 -m debugpy --listen 5678 -m flask run --host 0.0.0.0 --port=5001 --debug --no-debugger --no-reload ``` and then I want to connect debug port above in my vscode. The launch.json configuration is: ``` { "version": "0.2.0", "configurations": [ { "name": "Attach", "type": "debugpy", "request": "attach", "connect": { "host": "localhost", "port": 5678, }, "justMyCode": false, } ] } ``` I am sure the connection is successful. Then I add some breakpoints in one HTTP api. But when I send http request to that api, it can't stop in those breakpoints in vscode. Well I try to inspect the code in api/app.py, and seem to find some logic error: ``` # create app if is_db_command(): from app_factory import create_migrations_app app = create_migrations_app() else: # It seems that JetBrains Python debugger does not work well with gevent, # so we need to disable gevent in debug mode. # If you are using debugpy and set GEVENT_SUPPORT=True, you can debug with gevent. if (flask_debug := os.environ.get("FLASK_DEBUG", "0")) and flask_debug.lower() in { "false", "0", "no", }: print("Using gevent") from gevent import monkey # type: ignore # gevent monkey.patch_all() from grpc.experimental import gevent as grpc_gevent # type: ignore # grpc gevent grpc_gevent.init_gevent() import psycogreen.gevent # type: ignore psycogreen.gevent.patch_psycopg() from app_factory import create_app app = create_app() celery = app.extensions["celery"] ``` I think it should be: ``` if (flask_debug := os.environ.get("FLASK_DEBUG", "0")) and flask_debug.lower() in { "true", "1", "yes", }: ``` Meanwhile, I find FLASK_DEBUG will be set to 1 by flask when exec cmd using --debug flag like above. I'm not sure, look forward to reply~ ### ✔️ Expected Behavior exec cmd in one shell: ```shell python3 -m debugpy --listen 5678 -m flask run --host 0.0.0.0 --port=5001 --debug --no-debugger --no-reload ``` and then I can debug service above by connecting the debug port in my vscode successfully. ### ❌ Actual Behavior _No response_
yindo added the stale label 2026-02-21 19:10:34 -05:00
yindo closed this issue 2026-02-21 19:10:34 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Apr 17, 2025):

Hello @cuckootan! I'm Dosu, a bot here to help you solve bugs, answer questions, and assist you in becoming a contributor. I'm ready to help while you wait for a human maintainer.

To address the issue of breakpoints not being hit in VSCode when using remote debugging with debugpy for your Flask application, you should ensure that the FLASK_DEBUG environment variable is set correctly. The logic in your api/app.py file should indeed check for "true", "1", or "yes" to disable gevent when debugging, as gevent can interfere with the debugger.

Here's what you can do:

  1. Modify the logic in your api/app.py file to correctly check for the FLASK_DEBUG environment variable:

    if (flask_debug := os.environ.get("FLASK_DEBUG", "0")) and flask_debug.lower() in {
        "true",
        "1",
        "yes",
    }:
        # Disable gevent for debugging
    
  2. Ensure that you set the FLASK_DEBUG environment variable to "1" or "true" when starting your Flask application. Since you are using the --debug flag, Flask should automatically set FLASK_DEBUG to "1".

  3. Make sure your launch.json configuration in VSCode is set up correctly, as you have already done:

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Attach",
                "type": "debugpy",
                "request": "attach",
                "connect": {
                    "host": "localhost",
                    "port": 5678
                },
                "justMyCode": false
            }
        ]
    }
    

By ensuring the correct logic for FLASK_DEBUG and using the appropriate VSCode configuration, you should be able to hit breakpoints successfully when debugging your Flask application remotely [1].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other

@dosubot[bot] commented on GitHub (Apr 17, 2025): <!-- Greeting --> Hello @cuckootan! I'm [Dosu](https://dosu.dev), a bot here to help you solve bugs, answer questions, and assist you in becoming a contributor. I'm ready to help while you wait for a human maintainer. <!-- Answer --> To address the issue of breakpoints not being hit in VSCode when using remote debugging with debugpy for your Flask application, you should ensure that the `FLASK_DEBUG` environment variable is set correctly. The logic in your `api/app.py` file should indeed check for "true", "1", or "yes" to disable gevent when debugging, as gevent can interfere with the debugger. Here's what you can do: 1. Modify the logic in your `api/app.py` file to correctly check for the `FLASK_DEBUG` environment variable: ```python if (flask_debug := os.environ.get("FLASK_DEBUG", "0")) and flask_debug.lower() in { "true", "1", "yes", }: # Disable gevent for debugging ``` 2. Ensure that you set the `FLASK_DEBUG` environment variable to "1" or "true" when starting your Flask application. Since you are using the `--debug` flag, Flask should automatically set `FLASK_DEBUG` to "1". 3. Make sure your `launch.json` configuration in VSCode is set up correctly, as you have already done: ```json { "version": "0.2.0", "configurations": [ { "name": "Attach", "type": "debugpy", "request": "attach", "connect": { "host": "localhost", "port": 5678 }, "justMyCode": false } ] } ``` By ensuring the correct logic for `FLASK_DEBUG` and using the appropriate VSCode configuration, you should be able to hit breakpoints successfully when debugging your Flask application remotely <sup>[[1]](https://github.com/langgenius/dify/blob/main/api/app.py)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.* --- <sup>Help Dosu learn! Give it feedback: [Great Response](https://app.dosu.dev/response-feedback/cbed1d3c-9ece-44f6-aa37-a91593a9dc10?feedback_type=great_response) | [Irrelevant Answer](https://app.dosu.dev/response-feedback/cbed1d3c-9ece-44f6-aa37-a91593a9dc10?feedback_type=irrelevant_answer) | [Incorrect Sources](https://app.dosu.dev/response-feedback/cbed1d3c-9ece-44f6-aa37-a91593a9dc10?feedback_type=incorrect_sources) | [Too Verbose](https://app.dosu.dev/response-feedback/cbed1d3c-9ece-44f6-aa37-a91593a9dc10?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/cbed1d3c-9ece-44f6-aa37-a91593a9dc10?feedback_type=hallucination) | [Bug Report](https://app.dosu.dev/response-feedback/cbed1d3c-9ece-44f6-aa37-a91593a9dc10?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/cbed1d3c-9ece-44f6-aa37-a91593a9dc10?feedback_type=other)</sup>
Author
Owner

@cuckootan commented on GitHub (Apr 22, 2025):

It seems that remote debug is available only when set FLASK_DEBUG to 1 and enable GEVENT.

@cuckootan commented on GitHub (Apr 22, 2025): It seems that remote debug is available only when set FLASK_DEBUG to 1 and enable GEVENT.
Author
Owner

@dosubot[bot] commented on GitHub (Aug 18, 2025):

Hi, @cuckootan. I'm Dosu, and I'm helping the Dify team manage their backlog and am marking this issue as stale.

Issue Summary:

  • You reported difficulty with remote debugging the Dify API server using debugpy in VSCode, where breakpoints in HTTP API code were not hit.
  • The issue appears related to the interaction between gevent and Flask's debug mode in api/app.py.
  • I suggested setting the FLASK_DEBUG environment variable to "1" or "true" to disable gevent during debugging, as gevent can interfere with breakpoints.
  • You observed that remote debugging only works when FLASK_DEBUG is set to 1 and gevent remains enabled, indicating a complex interaction.
  • The issue remains unresolved with no definitive fix or workaround confirmed.

Next Steps:

  • Please let me know if this issue is still relevant with the latest version of Dify by commenting here to keep the discussion open.
  • Otherwise, this issue will be automatically closed in 15 days.

Thank you for your understanding and contribution!

@dosubot[bot] commented on GitHub (Aug 18, 2025): Hi, @cuckootan. I'm [Dosu](https://dosu.dev), and I'm helping the Dify team manage their backlog and am marking this issue as stale. **Issue Summary:** - You reported difficulty with remote debugging the Dify API server using debugpy in VSCode, where breakpoints in HTTP API code were not hit. - The issue appears related to the interaction between gevent and Flask's debug mode in `api/app.py`. - I suggested setting the `FLASK_DEBUG` environment variable to "1" or "true" to disable gevent during debugging, as gevent can interfere with breakpoints. - You observed that remote debugging only works when `FLASK_DEBUG` is set to 1 and gevent remains enabled, indicating a complex interaction. - The issue remains unresolved with no definitive fix or workaround confirmed. **Next Steps:** - Please let me know if this issue is still relevant with the latest version of Dify by commenting here to keep the discussion open. - Otherwise, this issue will be automatically closed in 15 days. Thank you for your understanding and contribution!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#13102