Ruby pg gem segfault

When running Rails tests (esp. in parallel mode for some reason), once in a while I get a segfault. This is coming from some interaction of pg gem code with krb5 (Kerberos protocol? I’m not familiar.)

The beginning of the error looks like this:

/Users/max/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/pg-1.5.3/lib/pg/connection.rb:690: [BUG] Segmentation fault at 0x00000001007c4abe
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin22]

There are 2 issues in pg’s repo about this:

  • An older issue, where a workaround is provided.
  • A newer issue that has more discussion, and links to the older one.

There are a few ways to work around this, but they all (except the last one) come down to disabling GSSENCMODE in postgres.

  1. Set ENV variable PGGSSENCMODE=disable (presumably when launching Postgres?)
  2. Set gssencmode=disable in the connection string. In Rails, this means you just add gssencmode: disable to the database.yml file, under each of your environments.
  3. Install postgres without gssencmode (doesn’t have --with-gssapi enabled). Homebrew postgres has --with-gssapi, asdf postgres doesn’t. The command otool -L can help check. E.g. otool -L ~/.asdf/installs/postgres/13.9/bin/postgres.
  4. Initiate the database connection by preloading the app before forking so the macOS system calls are invoked in the parent process. In Puma, the preload_app! config option does this.

August 8, 2023