Ruby 2 segfault case


Recently, I've built a new desktop PC, and decided to make my workstation for development in it. As I use Linux for work and given that I've been using Ubuntu for some time now, the free OS had to get installed, along with all my workstation data. So I did all that, everyone knows how to install an OS and software in it, including RVM, Ruby, MySQL, etc, all the stack used. However I had a problem that ate my brain shortly after.

The problem

Soon after everything was set up, I started noticing nasty problem with Ruby. Randomly occurring everywhere, going away shortly after, running a command with one tool and fail, running the same command with another tool and working, and so on. Quite frustrating. The issue was mostly a nasty segmentation fault, or segfault as it's typically called. A segfault basically is a very drastic error from a program reading memory and not finding what it was expecting. This then triggers a trap in the OS and dumps a really long list and stuff from the kernel to help debug, but it doesn't always help, a bit tricky to get this output to a file. At the bottom it would say "You may have found a bug in the interpeter" and suggests a few actions. The same software on another system didn't have this problem, so it was weird.

Some of the cases collected that threw this fault:

  • Using bundler either for updating, installing or executing binaries
  • Asset compilation during dev sessions
  • Packaging gems
  • Running webrick
  • Using network related features like net/http
All this cases, at some point threw a segfault. Launching webrick was painful even.

Bug hunting

Spent countless hours trying to fix this. At first, I thought it was related to the desktop networking, since it looked like anything using networking would likely fail. On Ubuntu 17 systemd was built in, and noticed that DNS resolution was now something systemd was doing. I was also having problems with internet connectivity, and set systemd to not do any DNS resolves and fallback to what previous Ubuntu version had. That fixed my internet, but the segfault was still there.

Reinstalled RVM countless times, with OpenSSL, without it, with the deprecated rvm pkg, etc. Reinstalled gems, Rubies, etc. No change. Switched from system RVM to user RVM, cleaned up lots of times and nothing.

Countless searches over possible Ruby issues, gems I was using, people get this error but in different contexts. And lucky for them, most could resolve them. And I'm sitting here, running the same command over and over again like if it was something temperamental, thinking that insisting will make it work.

Right on your face

So one day, already acustomed to live with the error along, decided to update to Ubuntu 18, maybe something in OS was crashing on me. After the update, still the same. Next up was a complete reinstall of the OS just to make sure EVERYTHING is clean. However, something I now do frequantly is review the packages I have installed, check the installation and cleanup old stuff. After an Ubuntu upgrade you will surely have to do some cleanup, like re-enabling APT custom sources, removing unused packages, etc. And what I found? A Ruby interpreter installed from APT along with Rubygems. 

So, apparently, RVM or some other gems and stuff from it, mistakenly called or used binaries from the package rather than the isolated installation on RVM environment. Possible a leak of some sort. I obviously removed everything Ruby related from APT packages, reinstalled RVM once again and all its stuff. Doing that finally seemed to fix this problem.

Closing note

If you experience a similar problem, be sure to strip your OS from any overlapping software you migth have lurking around. Ubuntu is known for pre-installing some stuff, so it may come with unwanted software if you're going to use something like RVM. I haven't tried rbenv though, I heard it has a few things done better than RVM, but I'm too comfortable in RVM now to switch, and this aside, I have never have bigger issues with it, either in servers or in my dev machines.

Sorry to disapoint you, if you expected some code or commands. It's a fairly simple thing to deal with, if you use any Linux, you can handle it, I believe in you, you can do it, nothing obscure was used anyway, just OS package manager to remove some unwanted packages.

Minor update

Having resolved this, I have to say that with RVM is still possible to get this problem. I wouldn't recommend to share gems or other libs between different rubies and gemsets. So be aware about how the RVM is linking everything up.