6

I am getting a GHCI segfault when trying to create a database connection in GHCI with postgresql-simple.

I am using GHC 8.0.1, OSX 10.11.16. I do not have a system GHC, only the one installed by stack.

If I do the following in my project, it segfaults:

$ stack ghci
Loaded GHCi configuration from /private/var/folders/wb/vvtxjd7n2qz02f571yjyr9j40000gs/T/ghci62972/ghci-script
gchi> :set -XOverloadedStrings
gchi> import Database.PostgreSQL.Simple
gchi> let connstr = "host=localhost port=5432 user=myusername password=mypass dbname=local"
gchi> conn <- connectPostgreSQL connstr
zsh: segmentation fault  stack ghci

I thought it might be worth looking at that file it mentions, so here are the contents:

> cat /private/var/folders/wb/vvtxjd7n2qz02f571yjyr9j40000gs/T/ghci62972/ghci-script
:load "/Users/erewok/projects/haskell/simpleservantblog/app/Main.hs" "Api" "Api.Post" "Api.User" "Config" "Html.Home" "Models.Author" "Models.Post"
:module + Api Api.Post Api.User Config Html.Home Models.Author Models.Post

It's a list of all the modules loaded from the project (which compiles and runs fine).


Here's the complete output of everything from starting ghci up to the seg fault:

$ stack ghci
The following GHC options are incompatible with GHCi and have not been passed to it: -threaded
Using main module: 1. Package `simpleservantblog' component exe:simpleservantblog-exe with main-is file: /Users/erewok/projects/haskell/simpleservantblog/app/Main.hs
Configuring GHCi with the following packages: simpleservantblog
GHCi, version 8.0.1: http://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /Users/erewok/.ghci
[1 of 8] Compiling Models.Author    ( /Users/erewok/projects/haskell/simpleservantblog/src/Models/Author.hs, interpreted )
[2 of 8] Compiling Models.Post      ( /Users/erewok/projects/haskell/simpleservantblog/src/Models/Post.hs, interpreted )
[3 of 8] Compiling Html.Home        ( /Users/erewok/projects/haskell/simpleservantblog/src/Html/Home.hs, interpreted )
[4 of 8] Compiling Api.User         ( /Users/erewok/projects/haskell/simpleservantblog/src/Api/User.hs, interpreted )
[5 of 8] Compiling Api.Post         ( /Users/erewok/projects/haskell/simpleservantblog/src/Api/Post.hs, interpreted )
[6 of 8] Compiling Config           ( /Users/erewok/projects/haskell/simpleservantblog/src/Config.hs, interpreted )
[7 of 8] Compiling Api              ( /Users/erewok/projects/haskell/simpleservantblog/src/Api.hs, interpreted )
[8 of 8] Compiling Main             ( /Users/erewok/projects/haskell/simpleservantblog/app/Main.hs, interpreted )
Ok, modules loaded: Api, Config, Api.Post, Api.User, Html.Home, Models.Post, Models.Author, Main.
Loaded GHCi configuration from /private/var/folders/wb/vvtxjd7n2qz02f571yjyr9j40000gs/T/ghci64266/ghci-script
gchi> :set -XOverloadedStrings
gchi> import Database.PostgreSQL.Simple
gchi> let connstr = "host=localhost port=5432 user=myusername password=mypass dbname=local"
gchi> conn <- connectPostgreSQL connstr
zsh: segmentation fault  stack ghci

More Information

Postgresql Version: 9.5, installed using the EnterpriseDB installer

The request to post my stack.yaml (which is below) reminded me of an issue I had when trying to install postgresql-libpq. This is probably related.

Stack.yaml

# This file was automatically generated by stack init
# For more information, see: http://docs.haskellstack.org/en/stable/yaml_configuration/

# Specifies the GHC version and set of packages available (e.g., lts-3.5, nightly-2015-09-21, ghc-7.10.2)
resolver: nightly-2016-08-25

# Local packages, usually specified by relative directory name
packages:
- '.'
- location:
    git: https://www.github.com/mattjbray/elm-export
    commit: 8868c1f09597f44c2e18e014cd9fbcf8320c3fea
  extra-dep: True
- location:
    git: https://www.github.com/mattjbray/servant-elm
    commit: e13c8def8127ea339e9801d804638854947193e8
  extra-dep: True
# Packages to be pulled from upstream that are not in the resolver (e.g., acme-missiles-0.3)
extra-deps:
  - wai-make-assets-0.1.1

# Override default flag values for local packages and extra-deps
flags: {}

# Extra package databases containing global packages
extra-package-dbs: []

# Control whether we use the GHC we find on the path
# system-ghc: true


# Extra directories used by stack for building
# extra-include-dirs: [/path/to/dir]
extra-lib-dirs: [/usr/local/opt/libiconv/lib, /usr/local/lib, /usr/lib]

Also segfaults on GHC 7.10.3 with resolver lts-6.17

gchi> conn <- connectPostgreSQL connstr
zsh: segmentation fault  stack ghci --resolver lts-6.17

An Idea I had

I think the version of postgresql-libpq I am using was built with libiconv.2.dylib compiled for Postgresql 9.4, but the database I am trying to connect to is actually Postgresql 9.5. This may be the reason for my issue.

A Thing I Did Inspired By that Idea

Here are some steps I took to try to debug further and/or fix this. I first linked the Postgresql 9.5 *dylib files into my /usr/local/lib directory in order to replace the old ones linked against the Postgresql 9.4 version.

$ cd /usr/local/lib
$ sudo ln -s -f /Library/PostgreSQL/9.5/lib/*dylib .

After that, I tried to force stack to rebuild postgresql-libpq and postgresql-simple in this way:

  $ stack exec -- ghc-pkg unregister --force postgresql-simple
  $ stack exec -- ghc-pkg unregister --force postgresql-libpq
  $ rm -rf ~/.stack/precompiled/x86_64-osx/ghc-8.0.1/1.24.0.0/postgresql-libpq-0.9.1.1/
  $  rm -rf ~/.stack/precompiled/x86_64-osx/ghc-8.0.1/1.24.0.0/postgresql-simple-0.5.2.*
  $ stack build

At which point, it promised me it would build and configure the removed packages. Still segfaulted when I ran the above, however.

erewok
  • 7,555
  • 3
  • 33
  • 45
  • What is the exact nightly snapshot you're using (in fact would you be able to post a minimal `stack.yaml` where this still occurs?). Does this also occur on the current LTS snapshot for you? Which postgres version do you have installed? Did you compile postgres yourself or install it some other way? – user2847643 Sep 12 '16 at 14:29
  • Thanks for the comment. I remember based on your questions, that I did have problems installed `postgresql-libpq`, which is likely related to the issue I am having. Still, it seemed weird that my project would compile and run fine, but that I would get segfaults in GHCI. I'll try to switch resolvers and see what happens. I am using `DuplicateRecordFields`, but I can probably omit that and give it a shot. – erewok Sep 12 '16 at 15:21
  • @user2847643 I also tried it with `lts-6.17` and it segfaulted there as well. – erewok Sep 12 '16 at 15:38
  • Hmm. This seems hard to reproduce. I can't see anything obviously wrong with your config but I have zero OSX experience nor do I have a Mac handy to run tests. Since you say it segfaults even on `lts-6.17` which is odd it's a good bet that there is something wrong with your postgres install. If I were you I would probably be wiping everything clean right about now. Including all of `~/.stack` and postgres. Also I would stay on LTS for now just to be safe. Otherwise you can hope for some hints from `gdb` maybe but that's a kind of a long shot. – user2847643 Sep 12 '16 at 21:21
  • Well, I appreciate the feedback and that you were reading. I'll give your suggestions a shot. – erewok Sep 12 '16 at 21:57

1 Answers1

1

While attempting to generate a stack trace for my issue, I discovered that I can start ghci in the following way:

$ stack ghci --ghci-options -fexternal-interpreter

And then, for whatever, reason, the segfault does not appear and everything works as expected. I have no idea if anyone else will find this useful.

The thing that seemed weird to me was that I didn't have an issue when the code was compiled and run. I only had an issue in GHCI.

Anyway, until I find out more or someone has a better suggestion, I'll go with this so I can continue testing what I want in the REPL.

erewok
  • 7,555
  • 3
  • 33
  • 45
  • You might want to file a bug report, and send a message to haskell-cafe. Those should get a few more eyes on your issue. – dfeuer Sep 14 '16 at 00:07