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

rdbg --util=gen-sockpath generate socket names that are too long #982

Closed
louim opened this issue May 19, 2023 · 5 comments
Closed

rdbg --util=gen-sockpath generate socket names that are too long #982

louim opened this issue May 19, 2023 · 5 comments

Comments

@louim
Copy link

louim commented May 19, 2023

Your environment

  • ruby -v: ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [arm64-darwin22]
  • rdbg -v: rdbg 1.8.0 {:mode=>:start, :no_color=>nil}

Describe the bug
rdbg --util=gen-sockpath generate a socket name that is too long. Running that command on my machine does:

bundle exec rdbg --util=gen-sockpath
/var/folders/fd/3pvxbtzs2gz38bxd96pf7frw0000gq/T/ruby-debug-sock-503/ruby-debug-louis-michel.couture-1833

As I understand, this is called when running RUBY_DEBUG_OPEN=true rails s to establish a socket path for where the remote debugger will connect. This is the output from starting the rails server, which crashes immediately because the socket path is too long.

DEBUGGER: Debugger can attach via UNIX domain socket (/var/folders/fd/3pvxbtzs2gz38bxd96pf7frw0000gq/T/ruby-debug-sock-503/ruby-debug-louis-michel.couture-2775)
#<Thread:0x0000000119a54f50@DEBUGGER__::Server::reader /Users/louis-michel.couture/.asdf/installs/ruby/3.1.3/lib/ruby/gems/3.1.0/gems/debug-1.8.0/lib/debug/server.rb:44 run> terminated with exception (report_on_exception is true):
/Users/louis-michel.couture/.asdf/installs/ruby/3.1.3/lib/ruby/3.1.0/socket.rb:1116:in `unix': too long unix socket path (105 bytes given but 104 bytes max) (ArgumentError)
	from /Users/louis-michel.couture/.asdf/installs/ruby/3.1.3/lib/ruby/3.1.0/socket.rb:1116:in `unix_server_socket'
	from /Users/louis-michel.couture/.asdf/installs/ruby/3.1.3/lib/ruby/3.1.0/socket.rb:1164:in `unix_server_loop'
	from /Users/louis-michel.couture/.asdf/installs/ruby/3.1.3/lib/ruby/gems/3.1.0/gems/debug-1.8.0/lib/debug/server.rb:500:in `accept'
	from /Users/louis-michel.couture/.asdf/installs/ruby/3.1.3/lib/ruby/gems/3.1.0/gems/debug-1.8.0/lib/debug/server.rb:49:in `block in activate'

To Reproduce
bundle exec rdbg --util=gen-sockpath. I haven't dug in to find why there is so much randomness added to the socket path, I'm not sure if this is always like that or related to some configuration on my machine. I also happen to have a long login name, pushing the already long socket name over the limit.

Expected behavior
Socket name generated is under 104 bytes, working for all lengths of user name.

Additional context
I'm aware that I can set the socket path manually to get a shorter one, eg RUBY_DEBUG_OPEN=true RUBY_DEBUG_SOCK_PATH=tmp/rdbg.sock rails s. I would prefer if it was working out of the box without the need of extra config, because then I have to also setup the matching path in VSCode. Currently VSCode doesn't seem to accept a relative path.

@ko1
Copy link
Collaborator

ko1 commented Sep 25, 2023

Now we use the following logic to get tmpdir.

  def self.unix_domain_socket_tmpdir
    require 'tmpdir'

    if tmpdir = Dir.tmpdir
      path = File.join(tmpdir, "ruby-debug-sock-#{Process.uid}")

      unless File.exist?(path)
        d = Dir.mktmpdir
        File.rename(d, path)
      end

      check_dir_authority(path)
    end
  end

and Dir.tmpdir can return /var/folders/fd/3pvxbtzs2gz38bxd96pf7frw0000gq/T/ (50 chars).

File name is specified with:

  def self.create_unix_domain_socket_name_prefix(base_dir = unix_domain_socket_dir)
    user = ENV['USER'] || 'UnknownUser'
    File.join(base_dir, "ruby-debug-#{user}")
  end

and long user name will generates long name.

Do you have an idea to short them?

@louim
Copy link
Author

louim commented Sep 25, 2023

We could probably shorten ruby-debug-sock- to a meaningful acronym, like rb-dbg-. And then get rid or the reference to ruby-debug- in the file name altogether. While not a bulletproof solution, it would leave more chars for the username portion of the logic.

@matt17r
Copy link

matt17r commented Nov 1, 2023

I just came across this too. My username is 23 characters long but I just did a test and macOS seems to allow usernames up to 80† characters long. I assume other *nix OSes are similar?

Perhaps including the full username in socket path is not advisable?

Do you have an idea to short them?

If it's desirable to keep username, can we just keep the first 20 characters?

   def self.create_unix_domain_socket_name_prefix(base_dir = unix_domain_socket_dir)
     user = ENV['USER'] || 'UnknownUser'
-     File.join(base_dir, "ruby-debug-#{user}")
+     File.join(base_dir, "ruby-debug-#{user.slice[0-19]}")
   end

Edit:
Misunderstood how .slice! worked on Strings. Changed to .slice

†I tried to create a user with a 100 letter name but it only let me enter the first 80. Verified with dscacheutil -q user:

name: abcdefghi1abcdefghi2abcdefghi3abcdefghi4abcdefghi5abcdefghi6abcdefghi7abcdefghi8
password: ********
uid: 504
gid: 20
dir: /Users/abcdefghi1abcdefghi2abcdefghi3abcdefghi4abcdefghi5abcdefghi6abcdefghi7abcdefghi8
shell: /bin/zsh
gecos: Length Test

@ko1
Copy link
Collaborator

ko1 commented Dec 5, 2023

Ok, now I changed the UNIX domain socket file name to use UID instead of username

[master]$ exe/rdbg --util=gen-sockpath
/run/user/1000/rdbg-3150

and I hope it will solve the problem.

@ko1 ko1 closed this as completed Dec 5, 2023
@ko1
Copy link
Collaborator

ko1 commented Dec 5, 2023

without XDG_RUNTIME_DIR:

$ exe/rdbg --util=gen-sockpath
/tmp/rdbg-1000/rdbg-3247

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants