microsoft / ptvsd

Python debugger package for use with Visual Studio and Visual Studio Code.
Other
550 stars 68 forks source link

KeyError: 'PTVSD_LAUNCHER_PORT' happened when debug with "sudo" #2081

Closed cranechu closed 4 years ago

cranechu commented 4 years ago

Environment data

Actual behavior

I tried to debug a python file with "sudo" by modifying launch.json, but I got the error message below. I tried it twice.

laptop:~/repo_debug_sudo_pytest▶ sudo /bin/python /home/cranechu/.vscode/extensions/ms-python.python-2020.2.62710/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/launcher /home/cranechu/repo_debug_sudo_pytest/test.py 
E+00000.031: Error parsing 'PTVSD_LAUNCHER_PORT':

             Traceback (most recent call last):
               File "/home/cranechu/.vscode/extensions/ms-python.python-2020.2.62710/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/launcher/__main__.py", line 36, in option
                 return type(os.environ.pop(name, *args))
               File "/usr/lib64/python3.7/_collections_abc.py", line 795, in pop
                 value = self[key]
               File "/usr/lib64/python3.7/os.py", line 679, in __getitem__
                 raise KeyError(key) from None
             KeyError: 'PTVSD_LAUNCHER_PORT'

             Stack where logged:
               File "/usr/lib64/python3.7/runpy.py", line 193, in _run_module_as_main
                 "__main__", mod_spec)
               File "/usr/lib64/python3.7/runpy.py", line 85, in _run_code
                 exec(code, run_globals)
               File "/home/cranechu/.vscode/extensions/ms-python.python-2020.2.62710/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/launcher/__main__.py", line 80, in <module>
                 main()
               File "/home/cranechu/.vscode/extensions/ms-python.python-2020.2.62710/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/launcher/__main__.py", line 40, in main
                 launcher_port = option("PTVSD_LAUNCHER_PORT", int)
               File "/home/cranechu/.vscode/extensions/ms-python.python-2020.2.62710/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/launcher/__main__.py", line 38, in option
                 raise log.exception("Error parsing {0!r}:", name)

Traceback (most recent call last):
  File "/usr/lib64/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib64/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/cranechu/.vscode/extensions/ms-python.python-2020.2.62710/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/launcher/__main__.py", line 80, in <module>
    main()
  File "/home/cranechu/.vscode/extensions/ms-python.python-2020.2.62710/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/launcher/__main__.py", line 40, in main
    launcher_port = option("PTVSD_LAUNCHER_PORT", int)
  File "/home/cranechu/.vscode/extensions/ms-python.python-2020.2.62710/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/launcher/__main__.py", line 38, in option
    raise log.exception("Error parsing {0!r}:", name)
  File "/home/cranechu/.vscode/extensions/ms-python.python-2020.2.62710/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/launcher/__main__.py", line 36, in option
    return type(os.environ.pop(name, *args))
  File "/usr/lib64/python3.7/_collections_abc.py", line 795, in pop
    value = self[key]
  File "/usr/lib64/python3.7/os.py", line 679, in __getitem__
    raise KeyError(key) from None
KeyError: 'PTVSD_LAUNCHER_PORT'

laptop:~/repo_debug_sudo_pytest▶ env PTVSD_LAUNCHER_PORT=37493 sudo /bin/python /home/cranechu/.vscode/extensions/ms-python.python-2020.2.62710/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/launcher /home/cranechu/repo_debug_sudo_pytest/test.py 
E+00000.042: Error parsing 'PTVSD_LAUNCHER_PORT':

             Traceback (most recent call last):
               File "/home/cranechu/.vscode/extensions/ms-python.python-2020.2.62710/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/launcher/__main__.py", line 36, in option
                 return type(os.environ.pop(name, *args))
               File "/usr/lib64/python3.7/_collections_abc.py", line 795, in pop
                 value = self[key]
               File "/usr/lib64/python3.7/os.py", line 679, in __getitem__
                 raise KeyError(key) from None
             KeyError: 'PTVSD_LAUNCHER_PORT'

             Stack where logged:
               File "/usr/lib64/python3.7/runpy.py", line 193, in _run_module_as_main
                 "__main__", mod_spec)
               File "/usr/lib64/python3.7/runpy.py", line 85, in _run_code
                 exec(code, run_globals)
               File "/home/cranechu/.vscode/extensions/ms-python.python-2020.2.62710/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/launcher/__main__.py", line 80, in <module>
                 main()
               File "/home/cranechu/.vscode/extensions/ms-python.python-2020.2.62710/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/launcher/__main__.py", line 40, in main
                 launcher_port = option("PTVSD_LAUNCHER_PORT", int)
               File "/home/cranechu/.vscode/extensions/ms-python.python-2020.2.62710/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/launcher/__main__.py", line 38, in option
                 raise log.exception("Error parsing {0!r}:", name)

Traceback (most recent call last):
  File "/usr/lib64/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib64/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/cranechu/.vscode/extensions/ms-python.python-2020.2.62710/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/launcher/__main__.py", line 80, in <module>
    main()
  File "/home/cranechu/.vscode/extensions/ms-python.python-2020.2.62710/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/launcher/__main__.py", line 40, in main
    launcher_port = option("PTVSD_LAUNCHER_PORT", int)
  File "/home/cranechu/.vscode/extensions/ms-python.python-2020.2.62710/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/launcher/__main__.py", line 38, in option
    raise log.exception("Error parsing {0!r}:", name)
  File "/home/cranechu/.vscode/extensions/ms-python.python-2020.2.62710/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/launcher/__main__.py", line 36, in option
    return type(os.environ.pop(name, *args))
  File "/usr/lib64/python3.7/_collections_abc.py", line 795, in pop
    value = self[key]
  File "/usr/lib64/python3.7/os.py", line 679, in __getitem__
    raise KeyError(key) from None
KeyError: 'PTVSD_LAUNCHER_PORT'

laptop:~/repo_debug_sudo_pytest▶ 

Expected behavior

Debug should work with sudo enabled.

Steps to reproduce:

  1. git clone https://github.com/cranechu/debug_python_with_sudo
  2. cd debug_python_with_sudo
  3. code .
  4. open test.py
  5. "F5" to start debug

Here is the copy of launch.json in this minimized repository. "sudo" is manually added.

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal", 
            "sudo": true
        }
    ]
}
int19h commented 4 years ago

I think this is happening because sudo does not propagate the environment by default, as a matter of security. It needs to be sudo -E, and the user must have the appropriate permission.

The real problem, though, is that the adapter uses sudo when spawning launcher it all. It doesn't actually need to do that, because the launcher will do it when it spawns the debuggee.

SilverJan commented 4 years ago

@int19h when will this be available in VS Code (on Ubuntu)?

int19h commented 4 years ago

It will be available in an insider build once https://github.com/microsoft/vscode-python/pull/10290 is merged.