Search code examples
pythondockervisual-studio-codedocker-composeodoo

Odoo Remote Attach VSCode not hitting breakpoints


I'm trying to debug Odoo with no success. Docker compose starts the environment for Odoo so it's already built. This is my launch file:

{
    "version": "0.2.0",
    "env": {
        "GEVENT_SUPPORT": "True"
    },
    "configurations": [
        {
            "name": "Debug",
            "type": "python",
            "request": "attach",
            "connect": {
                "host": "127.0.0.1",
                "port": 5678
            },
            "pathMappings": [
                {
                    "localRoot": "/local_folder/odoo",
                    "remoteRoot": "/remote_folder/odoo",
                }
            ],
            "justMyCode": true
        }
    ]
} 

In the Dockerfile I built the image using RUN pip3 install debugpy.

This is the command entry for the compose file:

ports:
  - "8069:8069"
  - "5678:5678"
expose:
  - 8069
  - 5678    
command: "python3 -m debugpy --listen  0.0.0.0:5678  --wait-for-client  /path_to_odoo/odoo-bin args"

So the container is waiting for me to start debugging as normal but when I try an specific action the debugger does not stop in any breakpoint. The breakpoints are red so vscode does see the remote file. Should the address still be localhost:8069? Using netstat shows that 8069 and 5678 are listening but can't make the debugger work. The debugpy version in the container is 1.8 but already used 1.6 with no success. Any idea what the problem could be?


Solution

  • I managed to make it "work". It's a bug already reported here:

    https://github.com/microsoft/debugpy/issues/1206

    Breakpoints hit when I remove GEVENT_SUPPORT=True or set it to False, but as soon as I do that I get spammed:

    It seems that the gevent monkey-patching is being used. Please set an environment variable with: GEVENT_SUPPORT=True to enable gevent support in the debugger.

    So the current solution to be able to use vscode debugger is to comment or remove GEVENT_SUPPORT and create a custom entrypoint so you won't get the spam message mentioned.

    Create a odoo_custom.py file:

    #!/usr/bin/env python3
    
    import sys
    sys.path.append('/path/to/project_folder/framework')
    
    # enable gevent support in the debugger
    
    __import__('os').environ['GEVENT_SUPPORT'] = 'true'
    
    # set server timezone in UTC before time module imported
    __import__('os').environ['TZ'] = 'UTC'
    
    # import and run odoo-bin
    if __name__ == "__main__":
        with open("/path/to/project_folder/framework/odoo-bin") as f:
            code = compile(f.read(), "odoo-bin", 'exec')
            exec(code)
    

    Finally add this in your compose:

    "python3 -m debugpy --listen  0.0.0.0:5678  --wait-for-client  /project_folder/custom.py args" 
    

    Two days ago, Jan 3 2024, the issue was closed and they gonna fix it for python 3.12+, older python versions will still have to use this solution.