ruby-lsp icon indicating copy to clipboard operation
ruby-lsp copied to clipboard

Debugger cannot attach to running process

Open gabrielso opened this issue 1 year ago • 9 comments

Operating System

Windows 11 - WSL 2 (Ubuntu) / Dev Containers

Ruby version

3.3.0

Project has a bundle

  • [X] Has bundle

Ruby version manager being used

no manager

Description

Running VSCode on Windows / WSL 2 with Dev Containers, the extension is unable to find the socket created by the rdbg process.

My Dockerfile has the following CMD directive:

CMD [ "bundle", "exec", "rdbg", "-O", "-n", "-c", "--", "bin/rails", "s", "--early-hints", "-b", "0.0.0.0" ]

It starts the server correctly and logs the socket creation (see first line of Container log): container_server_start

But when I start the debugger I see the message: no_debuggee

The debugger is working in other contexts like for instance debugging a test case: attached_test_debug

From the error message, it seems like the attachDebuggee function is not finding the sock (I checked the file location in the container as well as permissions).

gabrielso avatar Feb 27 '24 17:02 gabrielso

This is weird, if run bundle binstub debug and replace the Docker CMD from bundle exec rdbg ... to bin/rdbg ... the debugger can attach to the process, but this is not a consistent behavior. Once in a while the debugger stops finding the socket.

gabrielso avatar Feb 29 '24 20:02 gabrielso

Thank you for the bug report! @st0012 do you have any ideas on what could be happening here? I remember you mentioning some stuff about bin/rdbg.

vinistock avatar Mar 01 '24 16:03 vinistock

I'm seeing the same issue without using a container, on MacOS.

Starting rails with bundle exec rdbg -O -n -c -- bin/rails server -p 3000: image

launch.json:

    {
      "type": "ruby_lsp",
      "request": "attach",
      "name": "Attach to existing dashboard-server",
    },

Result of running attach to existing dashboard-server: image

snickell avatar Mar 02 '24 16:03 snickell

@snickell does it work with the debug gem binstub?

gabrielso avatar Mar 05 '24 08:03 gabrielso

@gabrielso I don't know a lot about the debug gem, could you suggest how I might test that?

snickell avatar Mar 19 '24 10:03 snickell

You certainly have the debug gem installed (as is a dependency of a Rails project), which means you can invoke it on the terminal bundle exec rdbg, but if you genera a binstub (bundle binstub debug will generate the bin/rdbg wrapper) you can start your application with bin/rdbg -O -n -c -- bin/rails server -p 3000.

My assumption is that the wrapper was creating a favorable condition (maybe related to file/socket ownership since the Docker runs as root). That worked but started failing again after a couple of days.

gabrielso avatar Mar 22 '24 16:03 gabrielso

By the way, I copied the Dev Container setup from the Rails main branch and it is working so far (although VS Code still hangs from time to time when I detach the debugger), that one uses the "official" Dev Container image for Ruby provided by Microsoft whereas my previous setup was relying on an image I built "from scratch".

There are probably some fixes on MS's image to make it work more reliably since they're specifically made to be Dev Containers.

I can share the setup if you're interested in testing.

gabrielso avatar Mar 22 '24 16:03 gabrielso

I can reproduce the issue as well, with a different system

Operating System: Ubuntu 22.04

Ruby version 3.2.4

Project has a bundle: Yes

Ruby version manager being used: rbenv

I did a clean install of the shopify ruby extension pack

LouisKottmann avatar May 16 '24 08:05 LouisKottmann

Actually I found a way to make it work, riiight before I was about to give up:

in launch.json specify the port of your rdbg like this:

    {
      "type": "ruby_lsp",
      "name": "Attach debugger",
      "request": "attach",
      "debugServer": 4711
    }

and specify the same port when running rails server in the console:

bundle exec rdbg -O -n --port 4711 -c -- bin/rails server --early-hints

LouisKottmann avatar May 16 '24 10:05 LouisKottmann

This issue is being marked as stale because there was no activity in the last 2 months

github-actions[bot] avatar Jul 15 '24 12:07 github-actions[bot]

@LouisKottmann really thanks for helping , this is make me can not debug from docker to vscode for long time!!!

for any one want to debug rails from vscode inside docker
ruby version 3.3.3 change your compose file like this : ports: - 3000:3000 - 1234:1234 command: ['bundle', 'exec', 'rdbg', '--open', '--nonstop', '--port=1234', '--command', '--', 'rails', 's', '-p', '3000', '-b', '0.0.0.0']

launch.json

{ "version": "0.2.0", "configurations": [ { "name": "Remote Debug - Rails", "type": "rdbg", "request": "attach", "debugPort": "localhost:1234", "localfsMap": "/app:${workspaceFolder}", "rdbgPath": "/usr/local/bundle/bin/rdbg", "showDebuggerOutput": true } ] }

add to gem file
gem 'debug', '~> 1.8'

hienleke avatar Feb 19 '25 18:02 hienleke