Fixing Rails 5.2 Bus Error on ARMv7/Raspberry Pi

Rails 5.2 apps crash with a bus error on ARMv7 CPUs like the Raspberry Pi. Here's a quick fix.

The Problem

Bringing up a small Rails 5.2 app on a Raspberry Pi 3B threw a really alarming bus error:

A “bus error” is a hardware indication that a program tried to access memory that the hardware can’t address. It might be an alignment error (CPUs often have strict requirements about how data or instructions are aligned on even or odd word boundaries – don’t worry if that doesn’t mean anything to you). It’s not a permissions problem – it’s not caused by a program trying to access memory it’s not allowed to.

It’s a very low level fault which shouldn’t happen and may indicate a hardware issue or a compiler or language bug.

Debugging

I tried swapping the microSD card into another Raspberry Pi and got the same problem. I tried doing a clean reinstall on a new microSD card and also saw the same problem. So it wasn’t likely to be a hardware issue.

The problem seems to be a bad interaction between the bootsnap gem and the Ruby virtual machine. Rails 5.2 started using bootsnap in order to reduce startup time – and it works well, bootsnap is awesome – startup time may be improved by 50%. On ARMv7 CPUs there’s a bad interaction between bootsnap’s caching and the Ruby VM. This happens with multiple versions of Ruby – at the least, Ruby 2.4.1 through Ruby 2.5.1. It may be fixed in Ruby 2.6, or at least its frequency reduced.

Workaround

Phil Ferne suggests an easy workaround: edit Rails’ config/boot.rb  file and comment out

This will disable bootsnap. If you deploy your Rails app to multiple platforms and want the benefits of bootsnap on CPUs other than ARMv7, you might try something like this in your config/boot.rb : 

Hopefully Ruby 2.6 or a release soon after it will permanently fix this problem.