lpsmith / postgresql-simple

Mid-level client library for accessing PostgreSQL from Haskell
Other
206 stars 71 forks source link

Access violation when converting query result in Windows 64-bit #244

Closed sonowz closed 6 years ago

sonowz commented 6 years ago

Hello, I have a runtime error when running this code. (brought from first tutorial code in here for demo purpose.)

{-# LANGUAGE OverloadedStrings #-}

import Control.Monad
import Database.PostgreSQL.Simple

_init :: IO Connection
_init = do
  conn <- connectDB
  putStrLn "connected"
  four <- hello conn
  putStrLn $ show four
  putStrLn "wrote 4"
  -- ...

hello :: Connection -> IO Int
hello conn = do
  [Only i] <- query_ conn "select 2 + 2"
  return i

-- ...

where running this yields

$ stack exec my-project-exe
connected
Access violation in generated code when executing data at 000000008000fb50

notice that "wrote 4" didn't print, so the issue would lie in query_. (I set flush with newline.)

I suggest this issue arises from converting SQL query result into Haskell value, regardless of types. (I tried with Int, String, and Text type.) Also, when I try query like SELECT * FROM empty_table, the program runs without error.

The cause of the issue might be 32bit/64bit, since the address 000000008000fb50 is not accessible in 32bit space. (It is kernel space!) I don't know how Haskell runs in low-level, so I'm posting here for help.

Seems like this guy had same issue with me.

Environment:

All programs are installed as 64-bit, at least I tried so.

lpsmith commented 6 years ago

Well, postgresql-simple itself doesn't use any unsafe memory operations, so (outside some kind of weird race condition, if you are using postgresql-simple concurrently) I don't see how this can be postgresql-simple's problem. I suppose there could be a problem in postgresql-libpq, but it could also pretty easily be in libpq, GHC, or some integration problem thereof.

I don't use Windows much, so I'm not going to be of much help here. Good luck!

sonowz commented 6 years ago

Oh, I thought there was C binding code in this repo. I tried changing GHC version to 7 (which means downgrading cabal too), but had no luck. libpq.dll works fine when I use it through psql, so I'm now suspecting either postgresql-libpq or GHC. I can't still figure out what's wrong, but it seems obvious this bug has nothing to do with postgresql-simple, so I'm closing it. Thanks for commenting!