yesodweb / yesodweb/persistent

Segmentation fault: 11 on macOS

Open
#830 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Haskell
Stars
486
Forks
306
PR merge metrics
No merged PRs in 30d

Description

We are using Nix and Haskell in production with Persistent and MySQL
We get a Segmentation fault: 11 on macOS, but not on Linux (works on Ubuntu and NixOS).

You may find our setup on gist.github

In the script below, the functions base, simple and persistent use increasingly higher-level mysql packages. base and simple cause no problem, but persistent does.
For example, in ghci, persistent 1 100 may perform the first 20 insertions successfully, only to segfault on the 21th. Other times, it will segfault on the first insertion. It does seem to be "random".
In addition, printMigration and runMigration reliably cause a segfault.

The local test database and the foo table have been created manually from mysql command line. The same problem occurs when connecting to our remote database.

import qualified Database.MySQL.Base as MySQL.Base
import qualified Control.Monad.Logger as Logger
import qualified Database.Persist.MySQL as Persist.MySQL 
import qualified Database.MySQL.Simple as MySQL.Simple
import qualified Database.Persist.TH as TH 

import Data.Monoid ((<>))
import Data.String (fromString)
import Control.Monad (forM_, void)
import Control.Monad.IO.Class (liftIO)
import Control.Exception.Safe (tryAny)

TH.share [TH.mkPersist TH.sqlSettings, TH.mkMigrate "migrateTables"] [TH.persistLowerCase|
Foo
   i Int
   deriving Show
|]

persist :: Int -> Int -> IO () 
persist from n = do 
    let info = Persist.MySQL.defaultConnectInfo
            { Persist.MySQL.connectHost     = "localhost"
            , Persist.MySQL.connectDatabase = "test"
            , Persist.MySQL.connectUser     = "root"
            , Persist.MySQL.connectPassword = "root" 
            }
    Logger.runNoLoggingT $ Persist.MySQL.withMySQLConn info $ \(be :: Persist.MySQL.SqlBackend) -> do
        forM_ [from..from + n] $ \i -> do 
            r <- tryAny $ flip Persist.MySQL.runSqlConn be $ do
                void $ Persist.MySQL.insert $ Foo i
            liftIO $ print (i, r)


simple :: Int -> Int -> IO () 
simple from n = do 
    conn <- MySQL.Simple.connect MySQL.Simple.defaultConnectInfo
        { MySQL.Simple.connectHost     = "localhost"
        , MySQL.Simple.connectDatabase = "test"
        , MySQL.Simple.connectUser     = "root"
        , MySQL.Simple.connectPassword = "root"
        }
    forM_ [from..from + n] $ \i -> do 
        r <- tryAny (MySQL.Simple.execute conn "INSERT INTO `foo`(`i`) VALUES(?)" $ MySQL.Simple.Only i)
        print (i, r)
    MySQL.Simple.close conn

base :: Int -> Int -> IO () 
base from n = do 
    conn <- MySQL.Base.connect MySQL.Base.defaultConnectInfo
        { MySQL.Base.connectHost     = "localhost"
        , MySQL.Base.connectDatabase = "test"
        , MySQL.Base.connectUser     = "root"
        , MySQL.Base.connectPassword = "root"
        }
    forM_ [from..from + n] $ \i -> do 
        r <- tryAny (MySQL.Base.query conn $ "INSERT INTO `foo`(`i`) VALUES(" <> fromString (show i) <> ");") 
        print (i, r)
    MySQL.Base.close conn

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied reproducer and linked gist, comparing the base, simple, and persistent functions on macOS. Focus first on the persistent path and the calls to printMigration and runMigration; done means the reproducer no longer segfaults on macOS and the affected insert and migration operations complete reliably.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell, macos, mysql
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.