TLDR Dev · ARTICLES & TUTORIALS
WHEN YOUR HASH BECOMES A STRING: HUNTING RUBY'S MILLION-TO-ONE MEMORY BUG
Summary from the TLDR Dev newsletter
This article goes over an investigation of an extremely rare Ruby bug where an internal FFI (Foreign Function Interface) Hash was mysteriously replaced by a String object at runtime, causing 2,700 identical "undefined method 'default' for String" errors. The root cause was discovered to be missing write barriers in FFI versions before 1.17.0, which prevented Ruby's garbage collector from knowing about object references in C code, allowing it to incorrectly free the Hash and replace it with a String at the same memory address.