Skip to content

Segfault while connecting from multiple processes with Postgres 12 #311

Closed

Description

@jessebs

I'm running Postgres 12.1, Ruby 2.6.5 and pg 1.1.4 on MacOS Catalina. This also happens on Ruby 2.5.1 and 2.5.7 as well as MacOS Mojave. It does not happen with Postgres 10 or 11.

Running the following small script will cause a segfault.

#!/usr/bin/env ruby
require 'pg'

PG.connect(:host => 'localhost', :user => 'username', :dbname => 'test_db')

Process.fork do
  PG.connect(:host => 'localhost', :user => 'username', :dbname => 'test_db')
end

See the attached segfault.txt and crash_report.txt

Activity

  1. konung commented on Dec 3, 2019

    @konung

    Confirming the same issue. Ran into it while testing on my MacOS an app, after upgrading to Postgres 12 from Postgres 11 ( via brew) . Works for a Single connection, but crashes for multiple, i.e. crashes a Rails app, when trying to load models.

    gem 'pg' => PG::VERSION = "1.1.4"
    rails 6.0.1
    ruby 2.6.5 (also tried 2.5.3 with the same result)
    psql (PostgreSQL) 12.1
    MacOS Catalina
    

    The only thing that changed - upgraded Postrgres to 12.1 from 11

  2. cbandy commented on Dec 3, 2019

    @cbandy
    Contributor

    @jessebs I expect gssencmode: "disable" will avoid the crash. Can you please verify?

  3. jessebs commented on Dec 3, 2019

    @jessebs
    Author

    Yes, that did avoid the issue.

  4. cbandy commented on Dec 3, 2019

    @cbandy
    Contributor
  5. trevorcreech commented on Dec 5, 2019

    @trevorcreech

    It looks like in Rails 5, this can be fixed by adding gssencmode: disable as a connection parameter in database.yml

    Unfortunately Rails 4 doesn't pass gssencmode along to pg (because VALID_CONN_PARAMS is hardcoded until Rails 5). We were able to fix this by setting a PGGSSENCMODE="disable" environment variable.

  6. miguelpeniche commented on Dec 6, 2019

    @miguelpeniche

    Thanks a lot @trevorcreech, ENV variable PGGSSENCMODE="disable" worked for me.

  7. jessebs commented on Dec 6, 2019

    @jessebs
    Author

    I'm a bit confused - based on my understanding of the problem, I would expect that closing the connection before the fork would resolve it but it does not.

    Any understanding of why that's the case?

  8. cbandy commented on Dec 6, 2019

    @cbandy
    Contributor

    It's not about shared handles or anything. It seems that macOS libraries are generally not safe to use after fork() (due to libdispatch.) It's a known issue over at Homebrew.

  9. alienxp03 commented on Dec 11, 2019

    @alienxp03

    Another workaround that I found is by not setting any value to the host, or just omit the host params.

    # 1
    Process.fork do
      PG.connect(:host => '', :user => 'username', :dbname => 'test_db')
    end
    
    # 2
    Process.fork do
      PG.connect(:user => 'username', :dbname => 'test_db')
    end

    If you're in Rails, you can just do the same thing with the host params.

    # 1: This will crash
    development:
      adapter: postgresql
      database: my_database
      host: localhost
    
    # 2: This won't crash and will still connect to localhost just fine
    development:
      adapter: postgresql
      database: my_database
      host:

    So far I noticed that I only need this workaround if the host is localhost or 127.0.0.1.

  10. cbandy commented on Dec 11, 2019

    @cbandy
    Contributor

    @alienxp03 Yes, this also skips GSS encryption by connecting over Unix socket (which is only available when client and server are on the same machine.) Depending on how the server is configured, Unix socket may involve different authentication than TCP socket.

  11. TiuTalk commented on Dec 11, 2019

    @TiuTalk

    Just letting you guys know that this also happens on Ruby 2.3.1, using pg 0.19.0, with postgresql stable 12.1 from Homebrew.

    Adding gssencmode: disable to the database.yaml fixed the segfaults, thanks @trevorcreech

  12. ankane commented on Jan 21, 2020

    @ankane

    If you installed PostgreSQL through Homebrew, reinstalling should fix it, thanks to this PR: Homebrew/homebrew-core#47494

    brew uninstall postgresql
    # then kill all postgresql processes or restart machine
    brew install postgresql
  13. 22 remaining items

  14. VickTor61 commented on Oct 18, 2025

    @VickTor61

    I have been experiencing this issue for little over a week now and no solution so far has worked for me.

    Ruby: ruby 3.1.4p223 (2023-03-30 revision 957bb7cb81) [arm64-darwin25]
    Pg: 1.4.5
    Rails: 6.1.7.2
    Postgresql: psql (PostgreSQL) 16.10 (Homebrew)
    Machine:Mbp m4 pro 2024 running macos Tahoe (latest version)

    I have tried adding some suggestions above to my database.yml file yet i still get the segmentation fault error.

      gssencmode: "disable"
      host:
    

    Any and every help is highly appreciated as this has been a major blocker for me. For context below is a snapshot of the error stack trace

    error.txt

  15. VickTor61 commented on Oct 18, 2025

    @VickTor61

    I have been experiencing this issue for little over a week now and no solution so far has worked for me.

    Ruby: ruby 3.1.4p223 (2023-03-30 revision 957bb7cb81) [arm64-darwin25] Pg: 1.4.5 Rails: 6.1.7.2 Postgresql: psql (PostgreSQL) 16.10 (Homebrew) Machine:Mbp m4 pro 2024 running macos Tahoe (latest version)

    I have tried adding some suggestions above to my database.yml file yet i still get the segmentation fault error.

      gssencmode: "disable"
      host:
    

    Any and every help is highly appreciated as this has been a major blocker for me. For context below is a snapshot of the error stack trace

    error.txt

    @Meekohi @docwhat @stevenharman @konung @trevorcreech

  16. harsh183 commented on Jan 30, 2026

    @harsh183

    I got the same issue on Macbook pro latest Tahoe (26.2) and setting gssencmode: disable to false fixed it. Before this mac update it used to come up occasionally but now it's became all the time. It was also strange because it would happen when starting it from rails 8.1's new bin/ci command, but running rails test in my normal shell worked fine.

    https://notes.max.engineer/ruby-pg-gem-segfault - reading this was also a help. We should reopen this issue

  17. Tectract commented on Jan 31, 2026

    @Tectract

    I'm still version-locked on Rails 5 and PostgresQL 13 from old debian repo backports because of this issue. I tried all the suggestions above, setting gssencode: disable didn't fix the solution on my servers.

    Rails just segfaults on any modern installation of PG. It appears Rails and PostgresQL have parted ways, as of like years ago. Surely I can't be the only one experiencing this.

  18. rromanchuk commented on Sep 6, 2026

    @rromanchuk

    Here from the future, @harsh183 did you ever figure out why this seems like it regressed recently? Was it specific to 26.2 or did my own behavior change and now just only noticing it. I'm also aggressively using bin/ci now, or i guess, a lot more forking workers in general. When i first saw this ignored it because of the year, but this is forsure the issue. I'm also able to "fix" with PARALLEL_WORKERS=1 which is another dead give away.

  19. harsh183 commented on Sep 6, 2026

    @harsh183

    https://notes.max.engineer/ruby-pg-gem-segfault

    best one I've found but it stayed broken across several mac os versions. My projects have quite a few tests and my MacBook has 12 cores so I want to use all of them to crunch so I just disable it.

    At least I've only configured this hack to be local dev and ci. Ownership of fixing this seems so diffused, I'm not sure if someone will handle it.

    I've only seen it on minitest, and I've also run into a lot because of all the bin/ci I use recently

  20. rromanchuk commented on Sep 7, 2026

    @rromanchuk

    same, all of the the above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions