使用 postgresql-simple 创建数据库连接时出现 GHCI 段错误

GHCI Segfault When Creating a Database Connection with postgresql-simple

我在尝试使用 postgresql-simple 在 GHCI 中创建数据库连接时遇到 GHCI 段错误。

我正在使用 GHC 8.0.1,OSX 10.11.16。我没有系统 GHC,只有通过堆栈安装的 GHC。

如果我在我的项目中执行以下操作,它会出现段错误:

$ 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

我认为它提到的那个文件可能值得一看,所以内容如下:

> 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

它是从项目中加载的所有模块的列表(可以正常编译和运行)。


这是从开始 ghci 到段错误的所有内容的完整输出:

$ 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

更多信息

Postgresql 版本:9.5,使用EnterpriseDB installer

安装

对 post 我的 stack.yaml 的请求(在下面)让我想起了我在 trying to install postgresql-libpq 时遇到的一个问题。这可能是相关的。

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]

在带有解析器 lts-6.17

的 GHC 7.10.3 上也出现段错误
gchi> conn <- connectPostgreSQL connstr
zsh: segmentation fault  stack ghci --resolver lts-6.17

我的一个想法

我认为我正在使用的 postgresql-libpq 版本是用 libiconv.2.dylib 构建的,为 Postgresql 9.4 编译,但我正在尝试连接的数据库实际上是Postgresql 9.5。这可能是我的问题的原因。

我受那个想法启发而做的一件事

以下是我尝试进一步调试所采取的一些步骤 and/or 修复此问题。我首先将 Postgresql 9.5 *dylib 文件链接到我的 /usr/local/lib 目录中,以替换与 Postgresql 9.4 版本链接的旧文件。

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

之后,我试图用这种方式强制stack重建postgresql-libpqpostgresql-simple

  $ 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

此时,它向我保证它将构建和配置已删除的包。但是,当我 运行 以上内容时仍然出现段错误。

在尝试为我的问题生成堆栈跟踪时,我发现我可以通过以下方式启动 ghci:

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

然后,出于某种原因,段错误没有出现,一切都按预期进行。我不知道其他人是否会觉得这有用。

令我感到奇怪的是,我在编译代码时没有遇到任何问题 运行。我只在 GHCI 中遇到问题。

无论如何,在我找到更多信息或有人提出更好的建议之前,我会继续这样做,这样我就可以继续在 REPL 中测试我想要的内容。