Ruby 2.6.5 and PostgreSQL pg-gem segmentation fault
Asked Answered
T

2

19

From the console I cannot do any operation that touches the database. I get a Segmentation fault.

.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/pg-1.1.4/lib/pg.rb:56: [BUG] Segmentation fault at 0x0000000000000110 ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin18]

It is literally any operation that might need the database, including MyModel.new.

-- Control frame information ----------------------------------------------- c:0071 p:---- s:0406 e:000405 CFUNC :initialize c:0070 p:---- s:0403 e:000402 CFUNC :new c:0069 p:0016 s:0398 e:000397 METHOD /Users/xxx/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/pg-1.1.4/lib/pg.rb:56 c:0068 p:0107 s:0393 e:000392 METHOD /Users/xxx/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activerecord-6.0.1/lib/active_record/connection_adapters/postgres

I have uninstalled and reinstalled the pg gem. And rebuilt the database. And restarted PostgreSQL.

I have seen other people reporting the problem when running under Puma, but my configuration works under Puma, fails under console!

Edit for clarity:

Yes, using bundler.

Starting the rails console either with rails c or bundle exec rails c has the same effect (segfault) with same stack trace.

Gemfile.lock has pg (1.1.4)

I re-bundled, specifying a bundle path. The stack trace now has that bundle path, so I guess by default bundler was using the rbenv path.

Toni answered 28/11, 2019 at 13:28 Comment(6)
Are you using Bundler in your project?Enos
My thinking is since it works with Puma, perhaps Ruby is loading the incorrect pg binary extension when you run under the console. If you're using Bundler then you should not see gem errors from the rbenv gem repository. Try running the console with bundle exec in that case.Enos
How are you starting your console? Does your Gemfile.lock show the same version of pg as shown here?Eczema
Had same issue; FWIW I was able to fix it by downgrading to 1.1.3. Seems like a pg gem bug that was closed for lack of more info: github.com/ged/ruby-pg/issues/291. You may want to provide them a stack trace?Boltzmann
This is an incompatibility with the GSS libraries that ship with macOS. This issue is open and linked to other relevant trackers: github.com/ged/ruby-pg/issues/311Greaten
I am getting the same issue on Amazon Linux AMI, doesn't seem to be strictly macOS related.Redcoat
M
50

As per https://github.com/ged/ruby-pg/issues/291, which is linked in the comments section of the question, adding gssencmode: disable to database.yml stops segmentation faults. I've added it under development and test environment options only, as I don't use OS X in other environments:

default: &default
  adapter: postgresql

development:
  <<: *default
  gssencmode: disable
test:
  <<: *default
  gssencmode: disable

gssencmode was added in Postgresql 12:

gssencmode

This option determines whether or with what priority a secure GSS TCP/IP connection will be negotiated with the server.

The underlying bug is tracked here https://www.postgresql.org/message-id/93f7379b-2e2f-db0c-980e-07ebd5de92ff%40crunchydata.com

Miamiami answered 4/12, 2019 at 10:42 Comment(9)
I can't thank you enough for sharing!! The details provided for this particular crash were very unhelpful for the unenlightened.Goral
You saved my day. Thanks a lot!Longsighted
Did not work for Ubuntu 20.04, ruby 2.3.1p112Zales
@Zales Did you manage to fix this issue?Lully
@FutoRicky, I can't remember what exactly I did to solve it but you can have a look at this issueZales
This worked for me too. My symptoms and env were a bit different. rails console worked fine but I got a segmentation fault when the server handled any web request. Happened for me with ruby (3.2.2) and the pg gem (1.5.3) on my M1 macbook pro. I also connect to PostgreSQL running in Docker via a TCP port. I believe if you connect over a unix socket the issue is not present because gssencmode does not apply. Anyway, if helpful my failure was here: ~/.rvm/gems/ruby-3.2.2/gems/pg-1.5.3/lib/pg/connection.rb:690: [BUG] Segmentation fault at 0x0000000104278ada CFUNC :connect_pollIntonation
Did not work for me, I'm on pg 11 and Amazon Linux AMI, but seeing the same issue.Redcoat
Such a simple fix for a hair-pulling issue. Thanks a lot!Bed
what would be the option for Sequel?Remittance
M
0

For me helped to

  • downgrade postgres downto 12
  • install openssl 1.1 locally
  • remove all gemsets, rubies and even rvm and reinstall with openssl 1.1 (I used rbenv, but not 100% sure that it's necessary)
Marvelofperu answered 18/9, 2023 at 21:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.