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

On Linux, attaching debugger to a running process causes the process to segfault #882

Closed
mosbasik opened this issue Mar 30, 2022 · 29 comments
Labels
bug Something isn't working

Comments

@mosbasik
Copy link

mosbasik commented Mar 30, 2022

Issue Type: Bug

Behaviour

Expected vs. Actual

I ran a simple python script that alternates between print() and sleep() forever, then I tried to attach the debugger to it. I expected to gain control of the process with the debugger. But actually, the process segfaulted.

Steps to reproduce:

I'm using Python 3.9 in a virtualenv on Arch Linux.

I disable all of my extensions from the Extensions tab using "Disable all installed extensions", and then re-enable only the Python extension to get the debugger.

I run the following Python script:

test.py:

import itertools
from time import sleep

def main():
    for i in itertools.count():
        print( f'Hello! ({i})' )
        sleep( 2 )

if __name__ == '__main__':
    main()

And then I try to attach to it using the following debugger setup:

launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name":"Python: Attach using Process Id",
            "type":"python",
            "request":"attach",
            "processId":"${command:pickProcess}",
            "logToFile": true,
            "console": "internalConsole",
            "env": {
                "PTVSD_DEBUG": "1",
            }
        },
    ]
}

Here's what the ouput of the script looks like:

$ python -u test.py
Hello! (0)
Hello! (1)
Hello! (2)
Hello! (3)
Hello! (4)   // once this is is printed, I attempt to attach
The threading module was not imported by user code in the main thread. The debugger will attempt to work around https://bugs.python.org/issue37416.
[1]    469716 segmentation fault (core dumped)  python -u test.py

Diagnostic data

  • Python version (& distribution if applicable, e.g. Anaconda): 3.9.12
  • Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): VirtualEnvWrapper
  • Value of the python.languageServer setting: Pylance
User Settings


defaultLS: {"defaultLSType":"Pylance"}

downloadLanguageServer: true

envFile: "<placeholder>"

venvPath: "<placeholder>"

venvFolders: "<placeholder>"

condaPath: "<placeholder>"

pipenvPath: "<placeholder>"

poetryPath: "<placeholder>"

languageServer: "Pylance"

linting
• enabled: false
• cwd: "<placeholder>"
• Flake8Args: "<placeholder>"
• flake8Enabled: false
• flake8Path: "<placeholder>"
• lintOnSave: true
• banditArgs: "<placeholder>"
• banditEnabled: false
• banditPath: "<placeholder>"
• mypyArgs: "<placeholder>"
• mypyEnabled: true
• mypyPath: "<placeholder>"
• pycodestyleArgs: "<placeholder>"
• pycodestyleEnabled: false
• pycodestylePath: "<placeholder>"
• prospectorArgs: "<placeholder>"
• prospectorEnabled: false
• prospectorPath: "<placeholder>"
• pydocstyleArgs: "<placeholder>"
• pydocstyleEnabled: false
• pydocstylePath: "<placeholder>"
• pylamaArgs: "<placeholder>"
• pylamaEnabled: false
• pylamaPath: "<placeholder>"
• pylintArgs: "<placeholder>"
• pylintPath: "<placeholder>"

sortImports
• args: "<placeholder>"
• path: "<placeholder>"

formatting
• autopep8Args: "<placeholder>"
• autopep8Path: "<placeholder>"
• provider: "none"
• blackArgs: "<placeholder>"
• blackPath: "<placeholder>"
• yapfArgs: "<placeholder>"
• yapfPath: "<placeholder>"

testing
• cwd: "<placeholder>"
• debugPort: 3000
• nosetestArgs: "<placeholder>"
• nosetestsEnabled: undefined
• nosetestPath: "<placeholder>"
• promptToConfigure: true
• pytestArgs: "<placeholder>"
• pytestEnabled: false
• pytestPath: "<placeholder>"
• unittestArgs: "<placeholder>"
• unittestEnabled: false
• autoTestDiscoverOnSaveEnabled: true

terminal
• activateEnvironment: true
• executeInFileDir: "<placeholder>"
• launchArgs: "<placeholder>"

experiments
• enabled: true
• optInto: []
• optOutFrom: []

insidersChannel: "off"

tensorBoard
• logDirectory: "<placeholder>"

Extension version: 2022.2.1924087327
VS Code version: Code - OSS 1.65.2 (c722ca6c7eed3d7987c0d5c3df5c45f6b15e77d1, 2022-03-10T19:23:12.185Z)
OS version: Linux x64 5.16.16-arch1-1
Restricted Mode: No

System Info
Item Value
CPUs Intel(R) Core(TM) i7-3520M CPU @ 2.90GHz (4 x 3389)
GPU Status 2d_canvas: enabled
gpu_compositing: enabled
multiple_raster_threads: enabled_on
oop_rasterization: disabled_off
opengl: enabled_on
rasterization: disabled_software
skia_renderer: enabled_on
video_decode: disabled_software
vulkan: disabled_off
webgl: enabled
webgl2: enabled
Load (avg) 1, 2, 2
Memory (System) 15.53GB (1.03GB free)
Process Argv --unity-launch
Screen Reader no
VM 0%
DESKTOP_SESSION undefined
XDG_CURRENT_DESKTOP undefined
XDG_SESSION_DESKTOP undefined
XDG_SESSION_TYPE undefined
@mosbasik
Copy link
Author

mosbasik commented Mar 30, 2022

My understanding right now is that the "The debugger will attempt to work around..." message may be unrelated.

I was concerned that it was related, and did a bit of searching, but based on fabioz/ptvsd@e0b2c5e (which was written to resolve microsoft/ptvsd#1542, which was linked from https://bugs.python.org/issue37416, which was linked from the warning) it appears that if the workaround had failed, there would probably be more errors, like "Issue found when debugger was trying to work around..." or "Error patching main thread id."

Of course, maybe the segfault is happening early enough that those other errors aren't being sent. 🤷‍♂️

Also, this person seems to also have been finding it impossible to successfully attach to a very similar looking test script using Ubuntu/WSL/Windows: #102 (comment) Interestingly, they don't have a segfault - and I don't get any reaction inside vscode like their screenshot.

@mosbasik
Copy link
Author

Found microsoft/vscode-python#3528, in which OP stopped getting their segfault after building a clean conda enviroment. Realized I could do more to isolate the problem. So:

I built a clean venv:

$ which python
/home/phenry/.virtualenvs/test_attaching_debugger/bin/python
$ pip list
Package    Version
---------- -------
pip        22.0.4
setuptools 60.9.3
wheel      0.37.1

I removed my OSS build of VSCode (Arch's community/code package) and replaced it with the official MS binary (Arch's aur/visual-studio-code-bin package).

I installed only the Python and Pylance extensions:
image

I made sure that VSCode was using the new venv:
image

And I used the default "Python: Attach using Process ID" config that was generated for me on first run:
image

I'm still getting the segfault.

Below is the diagnostic data generated by this new environment:

Diagnostic data

  • Python version (& distribution if applicable, e.g. Anaconda): 3.9.12
  • Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): VirtualEnvWrapper
  • Value of the python.languageServer setting: Default
User Settings


defaultLS: {"defaultLSType":"Pylance"}

downloadLanguageServer: true

envFile: "<placeholder>"

venvPath: "<placeholder>"

venvFolders: "<placeholder>"

condaPath: "<placeholder>"

pipenvPath: "<placeholder>"

poetryPath: "<placeholder>"

languageServer: "Pylance"

linting
• enabled: true
• cwd: "<placeholder>"
• Flake8Args: "<placeholder>"
• flake8Enabled: false
• flake8Path: "<placeholder>"
• lintOnSave: true
• banditArgs: "<placeholder>"
• banditEnabled: false
• banditPath: "<placeholder>"
• mypyArgs: "<placeholder>"
• mypyEnabled: false
• mypyPath: "<placeholder>"
• pycodestyleArgs: "<placeholder>"
• pycodestyleEnabled: false
• pycodestylePath: "<placeholder>"
• prospectorArgs: "<placeholder>"
• prospectorEnabled: false
• prospectorPath: "<placeholder>"
• pydocstyleArgs: "<placeholder>"
• pydocstyleEnabled: false
• pydocstylePath: "<placeholder>"
• pylamaArgs: "<placeholder>"
• pylamaEnabled: false
• pylamaPath: "<placeholder>"
• pylintArgs: "<placeholder>"
• pylintPath: "<placeholder>"

sortImports
• args: "<placeholder>"
• path: "<placeholder>"

formatting
• autopep8Args: "<placeholder>"
• autopep8Path: "<placeholder>"
• provider: "autopep8"
• blackArgs: "<placeholder>"
• blackPath: "<placeholder>"
• yapfArgs: "<placeholder>"
• yapfPath: "<placeholder>"

testing
• cwd: "<placeholder>"
• debugPort: 3000
• nosetestArgs: "<placeholder>"
• nosetestsEnabled: undefined
• nosetestPath: "<placeholder>"
• promptToConfigure: true
• pytestArgs: "<placeholder>"
• pytestEnabled: false
• pytestPath: "<placeholder>"
• unittestArgs: "<placeholder>"
• unittestEnabled: false
• autoTestDiscoverOnSaveEnabled: true

terminal
• activateEnvironment: true
• executeInFileDir: "<placeholder>"
• launchArgs: "<placeholder>"

experiments
• enabled: true
• optInto: []
• optOutFrom: []

tensorBoard
• logDirectory: "<placeholder>"

Extension version: 2022.4.0
VS Code version: Code 1.66.0 (e18005f0f1b33c29e81d732535d8c0e47cafb0b5, 2022-03-30T05:50:41.156Z)
OS version: Linux x64 5.16.16-arch1-1
Restricted Mode: No

System Info
Item Value
CPUs Intel(R) Core(TM) i7-3520M CPU @ 2.90GHz (4 x 2722)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: disabled_off
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
oop_rasterization: disabled_off
opengl: enabled_on
rasterization: disabled_software
raw_draw: disabled_off_ok
skia_renderer: enabled_on
video_decode: disabled_software
video_encode: disabled_software
vulkan: disabled_off
webgl: enabled
webgl2: enabled
Load (avg) 1, 2, 2
Memory (System) 15.53GB (0.40GB free)
Process Argv --no-sandbox --unity-launch
Screen Reader no
VM 0%
DESKTOP_SESSION undefined
XDG_CURRENT_DESKTOP undefined
XDG_SESSION_DESKTOP undefined
XDG_SESSION_TYPE undefined
A/B Experiments
vsliv368:30146709
vsreu685:30147344
python383:30185418
vspor879:30202332
vspor708:30202333
vspor363:30204092
pythonvspyl392:30443607
pythontb:30283811
pythonptprofiler:30281270
vshan820:30294714
vstes263:30335439
vscoreces:30445986
pythondataviewer:30285071
vscod805:30301674
pythonvspyt200:30340761
binariesv615:30325510
bridge0708:30335490
bridge0723:30353136
vsaa593cf:30376535
vsc1dst:30438360
pythonvs932:30410667
wslgetstarted:30449410
vsclayoutctrt:30451275
dsvsc009:30452663
pythonvsnew555:30457759
vscscmwlcmc:30438804
vscgsvid1:30447480
pynewfile477:30463512

@karthiknadig karthiknadig transferred this issue from microsoft/vscode-python Mar 31, 2022
@mosbasik
Copy link
Author

mosbasik commented Mar 31, 2022

I went back to using the OSS build because it's my daily driver and I'm using it for work. However, I'm still using the clean venv.

Here are the logs I get when triggering the segfault with faulthandler enabled (which I just learned about from #208 (comment)):

$ PYTHONFAULTHANDLER=1 python -u ~/testing/test_attaching_debugger.py
Results

Hello! (0)
Hello! (1)
Hello! (2)
Hello! (3)
Hello! (4)
The threading module was not imported by user code in the main thread. The debugger will attempt to work around https://bugs.python.org/issue37416.
Fatal Python error: Segmentation fault

Thread 0x00007f53d6ffd640 (most recent call first):
  File "/usr/lib/python3.9/threading.py", line 316 in wait
  File "/usr/lib/python3.9/threading.py", line 574 in wait
  File "/home/phenry/.vscode-oss/extensions/ms-python.python-2022.4.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd.py", line 255 in _on_run
  File "/home/phenry/.vscode-oss/extensions/ms-python.python-2022.4.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_daemon_thread.py", line 49 in run
  File "/usr/lib/python3.9/threading.py", line 973 in _bootstrap_inner
  File "/usr/lib/python3.9/threading.py", line 930 in _bootstrap

Thread 0x00007f53d77fe640 (most recent call first):
  File "/usr/lib/python3.9/threading.py", line 316 in wait
  File "/usr/lib/python3.9/threading.py", line 574 in wait
  File "/home/phenry/.vscode-oss/extensions/ms-python.python-2022.4.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd.py", line 209 in _on_run
  File "/home/phenry/.vscode-oss/extensions/ms-python.python-2022.4.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_daemon_thread.py", line 49 in run
  File "/usr/lib/python3.9/threading.py", line 973 in _bootstrap_inner
  File "/usr/lib/python3.9/threading.py", line 930 in _bootstrap

Thread 0x00007f53d7fff640 (most recent call first):
  File "/usr/lib/python3.9/threading.py", line 312 in wait
  File "/usr/lib/python3.9/threading.py", line 574 in wait
  File "/home/phenry/.vscode-oss/extensions/ms-python.python-2022.4.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_timeout.py", line 43 in _on_run
  File "/home/phenry/.vscode-oss/extensions/ms-python.python-2022.4.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_daemon_thread.py", line 49 in run
  File "/usr/lib/python3.9/threading.py", line 973 in _bootstrap_inner
  File "/usr/lib/python3.9/threading.py", line 930 in _bootstrap

Thread 0x00007f53dc990640 (most recent call first):
  File "/home/phenry/.vscode-oss/extensions/ms-python.python-2022.4.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 203 in _read_line
  File "/home/phenry/.vscode-oss/extensions/ms-python.python-2022.4.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 221 in _on_run
  File "/home/phenry/.vscode-oss/extensions/ms-python.python-2022.4.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_daemon_thread.py", line 49 in run
  File "/usr/lib/python3.9/threading.py", line 973 in _bootstrap_inner
  File "/usr/lib/python3.9/threading.py", line 930 in _bootstrap

Thread 0x00007f53dd191640 (most recent call first):
  File "/usr/lib/python3.9/threading.py", line 316 in wait
  File "/usr/lib/python3.9/queue.py", line 180 in get
  File "/home/phenry/.vscode-oss/extensions/ms-python.python-2022.4.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 366 in _on_run
  File "/home/phenry/.vscode-oss/extensions/ms-python.python-2022.4.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_daemon_thread.py", line 49 in run
  File "/usr/lib/python3.9/threading.py", line 973 in _bootstrap_inner
  File "/usr/lib/python3.9/threading.py", line 930 in _bootstrap

Current thread 0x00007f53df0a4740 (most recent call first):
  File "/home/phenry/testing/test_attaching_debugger.py", line 7 in main
  File "/home/phenry/testing/test_attaching_debugger.py", line 10 in <module>
[1]    610820 segmentation fault (core dumped)  PYTHONFAULTHANDLER=1 python -u ~/testing/test_attaching_debugger.py

@mosbasik
Copy link
Author

mosbasik commented Apr 1, 2022

Still digging. Not really sure what I'm doing, or what might be useful to look at.


Here's a summary of the coredump:

$ coredumpctl info
Results

           PID: 834432 (python)
           UID: 1000 (phenry)
           GID: 985 (users)
        Signal: 11 (SEGV)
     Timestamp: Fri 2022-04-01 12:44:13 EDT (22min ago)
  Command Line: python -u /home/phenry/testing/test_attaching_debugger.py
    Executable: /usr/bin/python3.9
 Control Group: /user.slice/user-1000.slice/session-1.scope
          Unit: session-1.scope
         Slice: user-1000.slice
       Session: 1
     Owner UID: 1000 (phenry)
       Boot ID: d66b55cf39fe49d9b0b079206f8b6d17
    Machine ID: 7d48c63c89d042c1996730d18d995480
      Hostname: simone
       Storage: /var/lib/systemd/coredump/core.python.1000.d66b55cf39fe49d9b0b079206f8b6d17.834432.1648831453000000.zst (present)
     Disk Size: 5.0M
       Message: Process 834432 (python) of user 1000 dumped core.
                
                Module linux-vdso.so.1 with build-id 51db189818659c9fea8a9001ec3fc23c6cf4996f
                Module _pickle.cpython-39-x86_64-linux-gnu.so with build-id a2428349df3a6ef0e4865f2d1caccfe8a83d2cb1
                Module libffi.so.8 with build-id f90d8b734f6de9b25faedb8cbfab7054dafc0a42
                Module _ctypes.cpython-39-x86_64-linux-gnu.so with build-id 3c2c5e7bf3e84833be1d6bb6c99ba14ddeceb0e5
                Module termios.cpython-39-x86_64-linux-gnu.so with build-id 21e5445eeb3376576a00b2c073772483486aeab7
                Module fcntl.cpython-39-x86_64-linux-gnu.so with build-id 9c58696ea490050a99329ee1e9588d66a4f50f9e
                Module libpthread.so.0 with build-id 7fa8b52fae071a370ba4ca32bf9490a30aff31c4
                Module liblzma.so.5 with build-id 8b615460aa230708c5183f16bede67aa0437d95e
                Module _lzma.cpython-39-x86_64-linux-gnu.so with build-id 57eb36de86b03ae3b6b97147aabaaab7b74a2794
                Module libbz2.so.1.0 with build-id 919597c477c9b2cb9cdbb7745ed6494ac0e6da60
                Module _bz2.cpython-39-x86_64-linux-gnu.so with build-id f747f6e1a6cae9a6681f8ea221d0af736788ddb4
                Module zlib.cpython-39-x86_64-linux-gnu.so with build-id dacb24c96b986938f446e42d5b63d35fa3b7a0c9
                Module libexpat.so.1 with build-id a05c64fe82308a309e7653685c94ce71735f8a1a
                Module pyexpat.cpython-39-x86_64-linux-gnu.so with build-id 61d1bb9a84b5d572735de5c48963ff34c6e80d4c
                Module libcrypto.so.1.1 with build-id f94a24f9ce8f3f394c3df23f7d436796797d4459
                Module libssl.so.1.1 with build-id 54f796076979dc9b6ff9ac3c39e250db86652924
                Module _ssl.cpython-39-x86_64-linux-gnu.so with build-id 96c15e8f8412480b98b17b6f4c8ab77e220bcf15
                Module _sha512.cpython-39-x86_64-linux-gnu.so with build-id c97a94f36abd49b6dba5e62de26c67d3a09024fc
                Module _random.cpython-39-x86_64-linux-gnu.so with build-id 7535af4316fbf118f5a3ce91e564bacb5bc775a2
                Module _bisect.cpython-39-x86_64-linux-gnu.so with build-id 012cf57c601274fdf82ebc77db0f259b580d0da5
                Module libmpdec.so.3 with build-id d681241acb03166032774dbf20cab2f91e8eca23
                Module _decimal.cpython-39-x86_64-linux-gnu.so with build-id f398b2553a231dd814002c01012b8c50c2822008
                Module _datetime.cpython-39-x86_64-linux-gnu.so with build-id 1212bff7fcf96283da1c4a5f9886e4f15ad5da07
                Module libz.so.1 with build-id 0c1459c56513efd5d53eb3868290e9afee6a6a26
                Module binascii.cpython-39-x86_64-linux-gnu.so with build-id 6f7b6cdbfafc0d80f60c1e28ff6ab5e6b1bd7303
                Module array.cpython-39-x86_64-linux-gnu.so with build-id c0874e4b94455665ab4985bc7dfc298db0376045
                Module _socket.cpython-39-x86_64-linux-gnu.so with build-id d32c41a60ca8e51d03e08831e2b69061b6e1094a
                Module unicodedata.cpython-39-x86_64-linux-gnu.so with build-id 5c150744d35edfa0c2b5bb416368d2094ba82f96
                Module _struct.cpython-39-x86_64-linux-gnu.so with build-id d82596b27da96b456d5c7f600126d6acf59977cb
                Module math.cpython-39-x86_64-linux-gnu.so with build-id dc91fdb9dad0e220e36081c11c7da5c6cb9ea6b1
                Module select.cpython-39-x86_64-linux-gnu.so with build-id 36b46005ad62ead9b0ec101efcc2d7c69827deac
                Module _posixsubprocess.cpython-39-x86_64-linux-gnu.so with build-id 1552ea59e22d1cee2660bbd23e57168bbe032f22
                Module grp.cpython-39-x86_64-linux-gnu.so with build-id 1598ac50d35cad5477d001dbe20d21c6e709efa5
                Module _queue.cpython-39-x86_64-linux-gnu.so with build-id e2ad7013bb532476b4fb24b849bda185d0dff6c4
                Module _opcode.cpython-39-x86_64-linux-gnu.so with build-id 168ccf46b08ff43d2b1254a8d034db9404f35028
                Module _json.cpython-39-x86_64-linux-gnu.so with build-id fbdeb3a7e83f612ae907370249379c442e9ee9aa
                Module libgcc_s.so.1 with build-id 5d817452a709ca3a213341555ddcf446ecee37fa
                Module libstdc++.so.6 with build-id 88ad4eff81a00c684abfe0f863e87434123d8943
                Module _heapq.cpython-39-x86_64-linux-gnu.so with build-id e307e7ad7a62f996948bde1214f3174765120ada
                Module ld-linux-x86-64.so.2 with build-id c09c6f50f6bcec73c64a0b4be77eadb8f7202410
                Module libm.so.6 with build-id 596b63a006a4386dcab30912d2b54a7a61827b07
                Module libc.so.6 with build-id 85766e9d8458b16e9c7ce6e07c712c02b8471dbc
                Module libpython3.9.so.1.0 with build-id f312f726343faf4c07e53df8879e2886eda5d867
                Module python3.9 with build-id e5e9f632bbd89b23b7cd787f0773027af0dc6663
                Stack trace of thread 834432:
                #0  0x00007fd3a4aa134c __pthread_kill_implementation (libc.so.6 + 0x8f34c)
                #1  0x00007fd3a4a544b8 raise (libc.so.6 + 0x424b8)
                #2  0x00007fd3a4a54560 __restore_rt (libc.so.6 + 0x42560)
                #3  0x00007ffd28165cff n/a (n/a + 0x0)
                #4  0x00007fd3a4547f90 n/a (n/a + 0x0)
                #5  0x007ffd28165d6800 n/a (n/a + 0x0)
                ELF object binary architecture: AMD x86-64


And the results of running the backtrace command from a GDB session opened on coredump (never done this before, not sure if I'm doing this right):

$ gdb /bin/python3.9 core.python.1000.d66b55cf39fe49d9b0b079206f8b6d17.834432.1648831453000000
Results

GNU gdb (GDB) 11.2
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /bin/python3.9...
(No debugging symbols found in /bin/python3.9)
[New LWP 834432]
[New LWP 834490]
[New LWP 834493]
[New LWP 834491]
[New LWP 834495]
[New LWP 834494]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
Core was generated by `python -u /home/phenry/testing/test_attaching_debugger.py'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00007fd3a4aa134c in __pthread_kill_implementation () from /usr/lib/libc.so.6
[Current thread is 1 (Thread 0x7fd3a4927740 (LWP 834432))]
(gdb) backtrace
#0  0x00007fd3a4aa134c in __pthread_kill_implementation () from /usr/lib/libc.so.6
#1  0x00007fd3a4a544b8 in raise () from /usr/lib/libc.so.6
#2  <signal handler called>
#3  0x00007ffd28165cff in ?? ()
#4  0x00007fd3a4547f90 in ?? ()
#5  0xcc007ffd28165d68 in ?? ()
#6  0x000056450df78980 in ?? ()
#7  0x00007ffd28165d68 in ?? ()
#8  0x000056450df78980 in ?? ()
#9  0x00007fd3a4384e50 in ?? ()
#10 0x0000000000000001 in ?? ()
#11 0x00007fd3a4d6a14e in ?? () from /usr/lib/libpython3.9.so.1.0
#12 0x00007fd3a4f81f18 in _PyRuntime () from /usr/lib/libpython3.9.so.1.0
#13 0x000056450df78980 in ?? ()
#14 0x00007fd3a4f81f70 in _PyRuntime () from /usr/lib/libpython3.9.so.1.0
#15 0x000056450df772d0 in ?? ()
#16 0x00007fd3a4384f72 in ?? ()
#17 0x00007fd3a45211c8 in ?? ()
#18 0x00007fd3a4cc0392 in ?? () from /usr/lib/libpython3.9.so.1.0
#19 0x0000f32b65752335 in ?? ()
#20 0x00007ffd28165e10 in ?? ()
#21 0x00007fd3a4384f72 in ?? ()
#22 0x00007fd3a45211c8 in ?? ()
#23 0x00007fd3a4db568a in ?? () from /usr/lib/libpython3.9.so.1.0
#24 0x00007fd3a4d6a14e in ?? () from /usr/lib/libpython3.9.so.1.0
#25 0x00007fd3a4c825cc in _PyEval_EvalFrameDefault () from /usr/lib/libpython3.9.so.1.0
#26 0x00007fd3a4c7c6b3 in ?? () from /usr/lib/libpython3.9.so.1.0
#27 0x00007fd3a4c825cc in _PyEval_EvalFrameDefault () from /usr/lib/libpython3.9.so.1.0
#28 0x00007fd3a4dd1344 in ?? () from /usr/lib/libpython3.9.so.1.0
#29 0x00007fd3a4dd225d in PyEval_EvalCode () from /usr/lib/libpython3.9.so.1.0
#30 0x00007fd3a4dd2715 in ?? () from /usr/lib/libpython3.9.so.1.0
#31 0x00007fd3a4e50108 in ?? () from /usr/lib/libpython3.9.so.1.0
#32 0x00007fd3a4e50a3d in ?? () from /usr/lib/libpython3.9.so.1.0
#33 0x00007fd3a4e50bd0 in ?? () from /usr/lib/libpython3.9.so.1.0
#34 0x00007fd3a4e51af2 in PyRun_AnyFileExFlags () from /usr/lib/libpython3.9.so.1.0
#35 0x00007fd3a4e529da in Py_RunMain () from /usr/lib/libpython3.9.so.1.0
#36 0x00007fd3a4e52bda in Py_BytesMain () from /usr/lib/libpython3.9.so.1.0
#37 0x00007fd3a4a3f310 in __libc_start_call_main () from /usr/lib/libc.so.6
#38 0x00007fd3a4a3f3c1 in __libc_start_main_impl () from /usr/lib/libc.so.6
#39 0x000056450c271055 in _start ()

@fabioz
Copy link
Collaborator

fabioz commented Apr 1, 2022

I wonder if this is happening because of some incompatibility on the Arch linux libc (I'm not really familiar with that Linux flavor).

The files related to the attach are compiled at:

https://github.com/microsoft/debugpy/tree/main/src/debugpy/_vendored/pydevd/pydevd_attach_to_process/linux_and_mac

Maybe it may be worth trying to compile those on your own machine to know if it makes any difference.

You can run https://github.com/microsoft/debugpy/blob/main/src/debugpy/_vendored/pydevd/pydevd_attach_to_process/linux_and_mac/compile_linux.sh

To find where that's in your machine, you can do execute:

import debugpy
print(debugpy.__file__)

and then find those files locally.

Also, does regular debugging work?

@fabioz
Copy link
Collaborator

fabioz commented Apr 1, 2022

You may also want to try disabling the cython extensions in this case (you can do this by setting an environment variable such as: PYDEVD_USE_CYTHON=0).

@fabioz fabioz added bug Something isn't working and removed classify labels Apr 1, 2022
@mosbasik
Copy link
Author

mosbasik commented Apr 1, 2022

Answering from easiest to hardest:

Regular debugging
Yes, that works fine.

Disabling cython extensions
I think you're saying to set PYDEVD_USE_CYTHON=0 when running the test script; I just tried that and I don't see a difference when diffing against my previous faulthandler output and coredump summary. I'll keep the setting during future tests though.

Compiling for my own machine
Ok, I think I found that script on my machine at /home/phenry/.vscode-oss/extensions/ms-python.python-2022.4.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd_attach_to_process/linux_and_mac/compile_linux.sh.

However, it doesn't have a shebang, isn't executable, and the second half is about 32 bit libs which (fuzzy on the details) I think Arch doesn't support. So (from same folder as the script) I manually ran the two lines of the script that seemed relevant:

$ g++ -m64 -shared -o attach_linux_amd64.so -fPIC -nostartfiles attach.cpp
$ mv attach_linux_amd64.so ../attach_linux_amd64.so

And that certainly produced a attach_linux_amd64.so without error and replaced the preexisting file of the same name in the parent folder.

Then I ran "Developer: Reload Window" in the editor, started the script, and tried to attach again. Still got a segfault, but at least the coredump summary seems to confirm that the library I recompiled was involved - here's the diff (original on left; recompiled on right).

image

@fabioz
Copy link
Collaborator

fabioz commented Apr 1, 2022

Can you do import threading at the top of the module you're debugging (this should at least get rid of the warning: The threading module was not imported by user code in the main thread ....).

Another thing to check may be gdb (as the attach to process uses gdb to do the actual attach). Do you think you can try with a newer version of gdb?

-- Installing it and making sure it's in the PATH should do the trick.

@mosbasik
Copy link
Author

mosbasik commented Apr 1, 2022

I did try import threading before opening the issue - you're correct, it does make the warning go away. That fact made me think the warning might well be unrelated to the segfault, which was why I did the digging in my first comment.

Arch does rolling releases; I believe I'm running the newest version of gdb:

$ gdb --version
GNU gdb (GDB) 11.2

I have a mac running OSX 10.14 and a friend running Ubuntu, maybe I'll see what happens if I try to replicate in those environments.

@mosbasik
Copy link
Author

mosbasik commented Apr 1, 2022

Well, I'm able to attach to my test script using the Mac (no segfault, works great!)

I'll ask my friend to try.

@mosbasik
Copy link
Author

mosbasik commented Apr 1, 2022

Interesting! He was able to replicate the segfault 100% on Ubuntu 21.10.

His `faulthandler` output:

Fatal Python error: Segmentation fault

Thread 0x00007f614a7fc640 (most recent call first):
  File "/usr/lib/python3.9/threading.py", line 316 in wait
  File "/usr/lib/python3.9/threading.py", line 574 in wait
  File "/home/friend/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd.py", line 246 in _on_run
  File "/home/friend/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_daemon_thread.py", line 46 in run
  File "/usr/lib/python3.9/threading.py", line 973 in _bootstrap_inner
  File "/usr/lib/python3.9/threading.py", line 930 in _bootstrap

Thread 0x00007f614affd640 (most recent call first):
  File "/usr/lib/python3.9/threading.py", line 316 in wait
  File "/usr/lib/python3.9/threading.py", line 574 in wait
  File "/home/friend/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd.py", line 200 in _on_run
  File "/home/friend/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_daemon_thread.py", line 46 in run
  File "/usr/lib/python3.9/threading.py", line 973 in _bootstrap_inner
  File "/usr/lib/python3.9/threading.py", line 930 in _bootstrap

Thread 0x00007f614b7fe640 (most recent call first):
  File "/usr/lib/python3.9/threading.py", line 312 in wait
  File "/usr/lib/python3.9/threading.py", line 574 in wait
  File "/home/friend/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_timeout.py", line 43 in _on_run
  File "/home/friend/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_daemon_thread.py", line 46 in run
  File "/usr/lib/python3.9/threading.py", line 973 in _bootstrap_inner
  File "/usr/lib/python3.9/threading.py", line 930 in _bootstrap

Thread 0x00007f614bfff640 (most recent call first):
  File "/home/friend/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 219 in _read_line
  File "/home/friend/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 237 in _on_run
  File "/home/friend/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_daemon_thread.py", line 46 in run
  File "/usr/lib/python3.9/threading.py", line 973 in _bootstrap_inner
  File "/usr/lib/python3.9/threading.py", line 930 in _bootstrap

Thread 0x00007f6150b0a640 (most recent call first):
  File "/usr/lib/python3.9/threading.py", line 316 in wait
  File "/usr/lib/python3.9/queue.py", line 180 in get
  File "/home/friend/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 382 in _on_run
  File "/home/friend/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_daemon_thread.py", line 46 in run
  File "/usr/lib/python3.9/threading.py", line 973 in _bootstrap_inner
  File "/usr/lib/python3.9/threading.py", line 930 in _bootstrap

Current thread 0x00007f6152d94c00 (most recent call first):
  File "/home/friend/lumin/lumin-bot/scratch.py", line 2241 in main
  File "/home/friend/lumin/lumin-bot/scratch.py", line 2244 in <module>
Segmentation fault (core dumped)

His coredump summary:

           PID: 63240 (python)
           UID: 1000 (friend)
           GID: 1000 (friend)
        Signal: 11 (SEGV)
     Timestamp: Fri 2022-04-01 12:49:48 PDT (13s ago)
  Command Line: python -u scratch.py
    Executable: /usr/bin/python3.9
 Control Group: /user.slice/user-1000.slice/user@1000.service/app.slice/app-org.gnome.Terminal.slice/vte-spawn-ea087b9a-a780-428d-8d0b-7a3d57dfb6f3.scope
          Unit: user@1000.service
     User Unit: vte-spawn-ea087b9a-a780-428d-8d0b-7a3d57dfb6f3.scope
         Slice: user-1000.slice
     Owner UID: 1000 (friend)
       Boot ID: 0c46b7ff89c444fd913eb14dc15597a0
    Machine ID: c8e8b17a05314272ae55a35e7286b7c6
      Hostname: friend-desktop
       Storage: /var/lib/systemd/coredump/core.python.1000.0c46b7ff89c444fd913eb14dc15597a0.63240.1648842588000000.zst (present)
     Disk Size: 8.6M
       Message: Process 63240 (python) of user 1000 dumped core.
                
                Stack trace of thread 63240:
                #0  0x00007ffe0babaebf n/a (n/a + 0x0)
                #1  0x00000000018210d0 n/a (n/a + 0x0)
                #2  0x000000005261b100 n/a (n/a + 0x0)

@mosbasik
Copy link
Author

mosbasik commented Apr 1, 2022

I noticed that on the Mac, gdb doesn't seem to be in the PATH:

$ which gdb
gdb not found

Is that significant?

@fabioz
Copy link
Collaborator

fabioz commented Apr 1, 2022

Mac has its own fork (lldb).

I'll see if I can reproduce on Ubuntu 21.10.

@mosbasik
Copy link
Author

mosbasik commented Apr 1, 2022

If you think it might be useful and can give me steps for how to switch out which binary is used by debugpy, I can install gdb on the Mac or lldb in my Arch environment and see what happens.

@mosbasik
Copy link
Author

mosbasik commented Apr 2, 2022

Heh. I installed gdb 11.2 on the Mac using homebrew and symlinked /usr/local/bin/lldb to /usr/local/Cellar/gdb/11.2/bin/gdb. Restarted VSCode and tried again.

Different result:

Nothing happens for about 15 seconds and then I get a popup saying "Timed out waiting for debug server to connect." That's exactly what happens in my Arch environment when the kernel is configured to forbid one process from snooping on another.

(Note: when the Mac was using lldb and I tried to attach for the first time, I got a nice popup saying something like "hey type in your password to permit this process to snoop on that one", I typed in my password and it attached.)

I don't think Linux's ptrace_scope approach to relaxing security works on Mac :) So on the off chance that Mac lets the root user snoop on any process, I killed VSCode and started it using sudo code. No luck though; same timeout message.


Then later I tried the reverse: on Arch, symlinked /usr/bin/gdb to /usr/bin/lldb. Same thing: "Timed out waiting for debug server to connect." Since I already had logToFile enabled in my launch.json, I peeked at the log:

As one could probably predict, `lldb` and `gdb` do not support identical arguments and flags :)

 40 I+00000.032: /handling #2 request "attach" from Client[1]/                                                                                                                                                                                 
 39              Session[1] waiting for connection from debug server...                                                                                                                                                                        
 38                                                                                                                                                                                                                                            
 37 I+00000.313: Injector[PID=943122] output:                                                                                                                                                                                                  
 36              b'Running: gdb --nw --nh --nx --pid 943122 --batch --init-eval-command=\'set auto-solib-add off\' --eval-command=\'set scheduler-locking off\' --eval-command=\'set architecture auto\' --eval-command=\'sharedlibrary libdl\'
 35                                                                                                                                                                                                                                            
 34 I+00000.313: Injector[PID=943122] output:                                                                                                                                                                                                  
 33              b'Running gdb in target process.'                                                                                                                                                                                             
 32                                                                                                                                                                                                                                            
 31 I+00000.313: Injector[PID=943122] output:                                                                                                                                                                                                  
 30              b"stdout: b''"                                                                                                                                                                                                                
 29                                                                                                                                                                                                                                            
 28 I+00000.316: Injector[PID=943122] output:                                                                                                                                                                                                  
 27              b'stderr: b\'error: unknown option: --nw\\nerror: unknown option: --nh\\nerror: unknown option: --nx\\nerror: unknown option: --pid\\nerror: unknown option: --init-eval-command=set auto-solib-add off\\nerror: unknown optio
 26                                                                                                                                                                                                                                            
 25 I+00000.358: Injector[PID=943122] exited.

@mosbasik
Copy link
Author

mosbasik commented Apr 4, 2022

Tried for a bit to compile gdb 10.2 on Arch (thinking gdb 11.2 might be too new).

Didn't succeed to my satisfaction; when I call gdb --version on the gdb 10.2 binary I built, I get the warnings Limited Guile support is available and Limited Python support is available from the _gdb module. I'm not confident that issues I encounter while using this binary wouldn't be caused by my building it incorrectly.

Since it was a wild guess to start with, I decided to stop trying this approach for now.

@mosbasik
Copy link
Author

mosbasik commented Apr 6, 2022

Wanted to try running VSCode as root after reading comments at the top of src/debugpy/_vendored/pydevd/pydevd_attach_to_process/add_code_to_python_process.py. Although I was able to do this on Mac a couple of days ago, I wasn't able to do it on Arch; not sure why.

  1. Running sudo code failed silently.
  2. journalctl systemd-coredump showed Process 518273 (electron) of user 0 dumped core; Resource limits disable core dumping for process 518273 (electron)
  3. I couldn't figure out how to change those system resource limits
    • Running # ulimit -c unlimited caused no error and no effect (# ulimit -c still gives 0).
    • Haven't figured if/how I can use /etc/security/limits.conf to do this

@mosbasik
Copy link
Author

mosbasik commented Apr 6, 2022

Maybe the injection logs of debugpy would be useful. These are from a "normal" run: (Arch, no symlinks, no sudo, etc )

Here are all the lines from ~/.vscode-oss/extensions/ms-python.python-2022.4.0/debugpy.adapter-534910.log that are about Injector - they seem most relevant:

Raw logs


I+00001.386: Injector[PID=533198] output:
             b'Running: gdb --nw --nh --nx --pid 533198 --batch --init-eval-command=\'set auto-solib-add off\' --eval-command=\'set scheduler-locking off\' --eval-command=\'set architecture auto\' --eval-command=\'sharedlibrary libdl\' --eval-command=\'call (void*)dlopen("/home/phenry/.vscode-oss/extensions/ms-python.python-2022.4.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd_attach_to_process/attach_linux_amd64.so", 2)\' --eval-command=\'sharedlibrary attach_linux_amd64\' --eval-command=\'call (int)DoAttach(0, "import codecs;import json;import sys;decode = lambda s: codecs.utf_8_decode(bytearray(s))[0] if s is not None else None;script_dir = decode([47, 104, 111, 109, 101, 47, 112, 104, 101, 110, 114, 121, 47, 46, 118, 115, 99, 111, 100, 101, 45, 111, 115, 115, 47, 101, 120, 116, 101, 110, 115, 105, 111, 110, 115, 47, 109, 115, 45, 112, 121, 116, 104, 111, 110, 46, 112, 121, 116, 104, 111, 110, 45, 50, 48, 50, 50, 46, 52, 46, 48, 47, 112, 121, 116, 104, 111, 110, 70, 105, 108, 101, 115, 47, 108, 105, 98, 47, 112, 121, 116, 104, 111, 110, 47, 100, 101, 98, 117, 103, 112, 121, 47, 46, 46, 47, 100, 101, 98, 117, 103, 112, 121, 47, 115, 101, 114, 118, 101, 114]);setup = json.loads(decode([123, 34, 109, 111, 100, 101, 34, 58, 32, 34, 99, 111, 110, 110, 101, 99, 116, 34, 44, 32, 34, 97, 100, 100, 114, 101, 115, 115, 34, 58, 32, 91, 34, 49, 50, 55, 46, 48, 46, 48, 46, 49, 34, 44, 32, 51, 52, 53, 57, 57, 93, 44, 32, 34, 119, 97, 105, 116, 95, 102, 111, 114, 95, 99, 108, 105, 101, 110, 116, 34, 58, 32, 102, 97, 108, 115, 101, 44, 32, 34, 108, 111, 103, 95, 116, 111, 34, 58, 32, 110, 117, 108, 108, 44, 32, 34, 97, 100, 97, 112, 116, 101, 114, 95, 97, 99, 99, 101, 115, 115, 95, 116, 111, 107, 101, 110, 34, 58, 32, 34, 97, 100, 50, 57, 52, 52, 101, 98, 56, 53, 52, 53, 97, 98, 52, 55, 102, 97, 55, 50, 98, 101, 52, 99, 99, 51, 50, 55, 52, 51, 50, 100, 97, 101, 56, 98, 55, 57, 52, 101, 53, 98, 51, 50, 50, 53, 54, 102, 100, 50, 52, 99, 100, 101, 102, 100, 56, 100, 51, 50, 56, 98, 57, 56, 34, 125]));sys.path.insert(0, script_dir);import attach_pid_injected;del sys.path[0];attach_pid_injected.attach(setup);", 0)\''

I+00001.387: Injector[PID=533198] output:
             b'Running gdb in target process.'

I+00001.389: Injector[PID=533198] output:
             b'stdout: b\'[Thread debugging using libthread_db enabled]\\nUsing host libthread_db library "/usr/lib/libthread_db.so.1".\\n0x00007fdfe0b9eab9 in select () from /usr/lib/libc.so.6\\nThe target architecture is set to "auto" (currently "i386:x86-64").\\n$1 = (void *) 0x55b1de851fc0\\n[New Thread 0x7fdfdef20640 (LWP 534933)]\\n[New Thread 0x7fdfde71f640 (LWP 534935)]\\n[New Thread 0x7fdfddf1e640 (LWP 534937)]\\n[New Thread 0x7fdfdd6dd640 (LWP 534938)]\\n[New Thread 0x7fdfdcedc640 (LWP 534939)]\\n[New Thread 0x7fdfc7fff640 (LWP 534940)]\\n[Thread 0x7fdfc7fff640 (LWP 534940) exited]\\n[New Thread 0x7fdfc7fff640 (LWP 534941)]\\n[Thread 0x7fdfc7fff640 (LWP 534941) exited]\\n[New Thread 0x7fdfc7fff640 (LWP 534942)]\\n[Thread 0x7fdfc7fff640 (LWP 534942) exited]\\n[New Thread 0x7fdfc7fff640 (LWP 534943)]\\n[Thread 0x7fdfc7fff640 (LWP 534943) exited]\\n[New Thread 0x7fdfc7fff640 (LWP 534944)]\\n[Thread 0x7fdfc7fff640 (LWP 534944) exited]\\n[New Thread 0x7fdfc7fff640 (LWP 534945)]\\n[Thread 0x7fdfc7fff640 (LWP 534945) exited]\\n[New Thread 0x7fdfc7fff640 (LWP 534946)]\\n[Thread 0x7fdfc7fff640 (LWP 534946) exited]\\n[New Thread 0x7fdfc7fff640 (LWP 534947)]\\n[Thread 0x7fdfc7fff640 (LWP 534947) exited]\\n\\nThread 1 "python" received signal SIGSEGV, Segmentation fault.\\n0x00007ffe7392cd0f in ?? ()\\n[Inferior 1 (process 533198) detached]\\n\''

I+00001.390: Injector[PID=533198] output:
             b'stderr: b\'[Detaching after fork from child process 534932]\\nThe program being debugged was signaled while in a function called from GDB.\\nGDB remains in the frame where the signal was received.\\nTo change this behavior use "set unwindonsignal on".\\nEvaluation of the expression containing the function\\n(DoAttach) will be abandoned.\\nWhen the function is done executing, GDB will silently stop.\\n\''

...

I+00001.459: Injector[PID=533198] exited.

Cleaned logs

Running:
gdb \
  --nw \
  --nh \
  --nx \
  --pid 533198 \
  --batch \
  --init-eval-command=\'set auto-solib-add off\' \
  --eval-command=\'set scheduler-locking off\' \
  --eval-command=\'set architecture auto\' \
  --eval-command=\'sharedlibrary libdl\' \
  --eval-command=\'call (void*)dlopen("/home/phenry/.vscode-oss/extensions/ms-python.python-2022.4.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd_attach_to_process/attach_linux_amd64.so", 2)\' \
  --eval-command=\'sharedlibrary attach_linux_amd64\' \
  --eval-command=\'call (int)DoAttach(0, "import codecs;import json;import sys;decode = lambda s: codecs.utf_8_decode(bytearray(s))[0] if s is not None else None;script_dir = decode([47, 104, 111, 109, 101, 47, 112, 104, 101, 110, 114, 121, 47, 46, 118, 115, 99, 111, 100, 101, 45, 111, 115, 115, 47, 101, 120, 116, 101, 110, 115, 105, 111, 110, 115, 47, 109, 115, 45, 112, 121, 116, 104, 111, 110, 46, 112, 121, 116, 104, 111, 110, 45, 50, 48, 50, 50, 46, 52, 46, 48, 47, 112, 121, 116, 104, 111, 110, 70, 105, 108, 101, 115, 47, 108, 105, 98, 47, 112, 121, 116, 104, 111, 110, 47, 100, 101, 98, 117, 103, 112, 121, 47, 46, 46, 47, 100, 101, 98, 117, 103, 112, 121, 47, 115, 101, 114, 118, 101, 114]);setup = json.loads(decode([123, 34, 109, 111, 100, 101, 34, 58, 32, 34, 99, 111, 110, 110, 101, 99, 116, 34, 44, 32, 34, 97, 100, 100, 114, 101, 115, 115, 34, 58, 32, 91, 34, 49, 50, 55, 46, 48, 46, 48, 46, 49, 34, 44, 32, 51, 52, 53, 57, 57, 93, 44, 32, 34, 119, 97, 105, 116, 95, 102, 111, 114, 95, 99, 108, 105, 101, 110, 116, 34, 58, 32, 102, 97, 108, 115, 101, 44, 32, 34, 108, 111, 103, 95, 116, 111, 34, 58, 32, 110, 117, 108, 108, 44, 32, 34, 97, 100, 97, 112, 116, 101, 114, 95, 97, 99, 99, 101, 115, 115, 95, 116, 111, 107, 101, 110, 34, 58, 32, 34, 97, 100, 50, 57, 52, 52, 101, 98, 56, 53, 52, 53, 97, 98, 52, 55, 102, 97, 55, 50, 98, 101, 52, 99, 99, 51, 50, 55, 52, 51, 50, 100, 97, 101, 56, 98, 55, 57, 52, 101, 53, 98, 51, 50, 50, 53, 54, 102, 100, 50, 52, 99, 100, 101, 102, 100, 56, 100, 51, 50, 56, 98, 57, 56, 34, 125]));sys.path.insert(0, script_dir);import attach_pid_injected;del sys.path[0];attach_pid_injected.attach(setup);", 0)\'
  
  
  
stdout:
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
0x00007fdfe0b9eab9 in select () from /usr/lib/libc.so.6
The target architecture is set to "auto" (currently "i386:x86-64").
$1 = (void *) 0x55b1de851fc0
[New Thread 0x7fdfdef20640 (LWP 534933)]
[New Thread 0x7fdfde71f640 (LWP 534935)]
[New Thread 0x7fdfddf1e640 (LWP 534937)]
[New Thread 0x7fdfdd6dd640 (LWP 534938)]
[New Thread 0x7fdfdcedc640 (LWP 534939)]
[New Thread 0x7fdfc7fff640 (LWP 534940)]
[Thread 0x7fdfc7fff640 (LWP 534940) exited]
[New Thread 0x7fdfc7fff640 (LWP 534941)]
[Thread 0x7fdfc7fff640 (LWP 534941) exited]
[New Thread 0x7fdfc7fff640 (LWP 534942)]
[Thread 0x7fdfc7fff640 (LWP 534942) exited]
[New Thread 0x7fdfc7fff640 (LWP 534943)]
[Thread 0x7fdfc7fff640 (LWP 534943) exited]
[New Thread 0x7fdfc7fff640 (LWP 534944)]
[Thread 0x7fdfc7fff640 (LWP 534944) exited]
[New Thread 0x7fdfc7fff640 (LWP 534945)]
[Thread 0x7fdfc7fff640 (LWP 534945) exited]
[New Thread 0x7fdfc7fff640 (LWP 534946)]
[Thread 0x7fdfc7fff640 (LWP 534946) exited]
[New Thread 0x7fdfc7fff640 (LWP 534947)]
[Thread 0x7fdfc7fff640 (LWP 534947) exited]
Thread 1 "python" received signal SIGSEGV, Segmentation fault.
0x00007ffe7392cd0f in ?? ()
[Inferior 1 (process 533198) detached]



stderr:
[Detaching after fork from child process 534932]
The program being debugged was signaled while in a function called from GDB.
GDB remains in the frame where the signal was received.
To change this behavior use "set unwindonsignal on".
Evaluation of the expression containing the function
(DoAttach) will be abandoned.
When the function is done executing, GDB will silently stop.

@fabioz
Copy link
Collaborator

fabioz commented Apr 7, 2022

As a note, I've been able to reproduce this with Ubuntu 21.10 and I'm investigating.

@fabioz
Copy link
Collaborator

fabioz commented Apr 7, 2022

Ok, I think I found the culprit... try to do the following: find the file:

debugpy/vendored/pydevd/pydevd_attach_to_process/add_code_to_python_process.py

Note: you can do

import debugpy
print(debugpy.__file__)

to know the base location where the debugger resides in your installation (run that in a simple debug session).

And then in that file comment out the lines with the following contents:

cmd.extend(["--init-eval-command='set auto-solib-add off'"])
cmd.extend(["--eval-command='sharedlibrary libdl'"])

and try to do a new attach.

That should make it work again...

Those changes were done some time ago as an optimization to make the attach run faster (because it shouldn't need to load all symbols from all dlls at an attach), but this is already the 2nd time we've had issues due to that change (the first time it was solved by forcing to load libdl to get the open symbol).

I'll investigate a bit more what's the proper solution, but at least for now that can be used as a workaround.

@mosbasik
Copy link
Author

mosbasik commented Apr 7, 2022

Workaround works! Beautiful. I've been looking forward to being able to debug a running process for - probably a year or so since I saw it in the docs. Thanks for looking into this.

@mosbasik mosbasik changed the title Attaching debugger to a running process causes the process to segfault On Linux, attaching debugger to a running process causes the process to segfault Apr 7, 2022
@fabioz
Copy link
Collaborator

fabioz commented Apr 7, 2022

I guess I'll make that the default (as it was previously)... it's better to be slower but working by default than faster and crashing sometimes depending on the linux installation (maybe I'll create an env variable to customize that if it's too slow for someone).

@mosbasik
Copy link
Author

mosbasik commented Apr 8, 2022

Out of curiosity, is it that

  1. the workaround takes longer to attach, but after it's attached it runs at the same speed as the "optimized" verision? or
  2. the workaround runs slower than the "optimized" version as long as it's attached?

@int19h
Copy link
Collaborator

int19h commented Apr 8, 2022

I wonder if this could also be the cause of sporadic failures of attach-to-PID tests in CI?

@fabioz
Copy link
Collaborator

fabioz commented Apr 9, 2022

Out of curiosity, is it that

1. the workaround takes longer to attach, but after it's attached it runs at the same speed as the "optimized" verision? or

2. the workaround runs slower than the "optimized" version as long as it's attached?

It should just take longer to attach and it shouldn't make a difference afterwards.

@fabioz
Copy link
Collaborator

fabioz commented Apr 9, 2022

I wonder if this could also be the cause of sporadic failures of attach-to-PID tests in CI?

I don't think it's likely, I think it's a works/doesn't work difference -- but we'll see... I'll do the change and we'll know for certain ;)

@mosbasik
Copy link
Author

mosbasik commented Jul 5, 2022

I'm a little curious if there's a plan for when the changes mitigating this issue might get merged (don't think 6d88802 is merged yet). I opened the issue against version 2022.2.1924087327, but I'm on 2022.8.1 now and I still need to apply the workaround manually before I start debugging.

@fabioz
Copy link
Collaborator

fabioz commented Jul 6, 2022

@mosbasik it's merged but a debugpy release still wasn't done after that.

@int19h @karthiknadig are there plans to do a release? I guess we have enough things to do a release worthwhile at this point (Python 3.11 support is still not complete, but it should be enough to be usable and definitely better than the last release and there are a number of fixes which should be interesting -- the major change in the repo is that frame eval mode, when available, is the default again).

@int19h
Copy link
Collaborator

int19h commented Jul 6, 2022

Yes, we're planning a release this week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants