mongodb-haskell/mongodb

SIGSEGV is occured

satosystems opened this issue · 2 comments

SIGSEGV is occurred when to call connect on other thread

Please check the following:

module Main where

import           Control.Concurrent             ( threadDelay )
import           Control.Concurrent.Async.Lifted
                                                ( async )
import qualified Database.MongoDB              as DB

con :: IO ()
con = do
  putStrLn "101"
  pipe <- DB.connect . DB.host $ "localhost"
  putStrLn "102"
  DB.close pipe
  putStrLn "103"

main :: IO ()
main = do
  putStrLn "001"
  pipe <- DB.connect . DB.host $ "localhost"
  putStrLn "002"
  DB.close pipe
  putStrLn "003"
  async con
  putStrLn "004"
  threadDelay (3 * 1000 * 1000)
  putStrLn "005"
  pipe <- DB.connect . DB.host $ "localhost"
  putStrLn "006"
  DB.close pipe
  putStrLn "007"

I have a SIGSEGV when run this code.

$ gdb .stack-work/dist/x86_64-linux/Cabal-2.4.0.1/build/api-server/api-server
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from .stack-work/dist/x86_64-linux/Cabal-2.4.0.1/build/api-server/api-server...done.
(gdb) r
Starting program: /root/api-server/.stack-work/dist/x86_64-linux/Cabal-2.4.0.1/build/api-server/api-server
[New LWP 2092]
[New LWP 2093]
[New LWP 2094]
[New LWP 2095]
[New LWP 2096]
[New LWP 2097]
[New LWP 2099]
[New LWP 2098]
[New LWP 2100]
[New LWP 2101]
001
002
003
004
[New LWP 2103]
101

Thread 9 "api-serve:w" received signal SIGSEGV, Segmentation fault.
[Switching to LWP 2098]
0x00007ffff45df7a0 in internal_getent (stream=stream@entry=0x7ffff40ec490, result=result@entry=0x9364e0 <resbuf.11135>, buffer=buffer@entry=0x98d600 "# Internet (IP) protocols\n",
    buflen=buflen@entry=1024, errnop=errnop@entry=0x7ffff4ff46a0) at nss_files/files-XXX.c:216
216	nss_files/files-XXX.c: No such file or directory.
(gdb) bt
#0  0x00007ffff45df7a0 in internal_getent (stream=stream@entry=0x7ffff40ec490, result=result@entry=0x9364e0 <resbuf.11135>, buffer=buffer@entry=0x98d600 "# Internet (IP) protocols\n",
    buflen=buflen@entry=1024, errnop=errnop@entry=0x7ffff4ff46a0) at nss_files/files-XXX.c:216
#1  0x00007ffff45dfa32 in _nss_files_getprotobyname_r (name=0x42002050a0 "tcp", result=0x9364e0 <resbuf.11135>, buffer=0x98d600 "# Internet (IP) protocols\n", buflen=1024, errnop=0x7ffff4ff46a0)
    at nss_files/files-proto.c:38
#2  0x0000000000817a87 in getprotobyname_r ()
#3  0x0000000000817858 in getprotobyname ()
#4  0x0000000000597dae in ?? ()
#5  0x0000000000000000 in ?? ()
(gdb) q
A debugging session is active.

	Inferior 1 [process 2088] will be killed.

Quit anyway? (y or n) y
$

As a result of the verification, the following is known:

  • SIGSEGV is occurred when call connect on main thread and another thread
    • There is no problem if only one thread
  • Ubuntu 18.04 has this problem
  • macOS Mojave has no problem
  • Using resolver is lts-14.4
    • mongoDB version is 2.5.0.0

Is there a solution or workaround for this issue?

  • Ubuntu 18.04 has this problem
  • macOS Mojave has no problem

This is misunderstand for me. Maybe this problem reason is static linkage.
My package.yaml of linux environment has the following options.

ghc-options
- -static
cc-options:
- -static
ld-options:
- -static

I found the following build log when linking executable.

/root/.stack/snapshots/x86_64-linux/27af6d278fe41290e0147e7d2907035de13658642a3d43c441ff93a7d3f2f512/8.6.5/lib/x86_64-linux-ghc-8.6.5/network-2.8.0.1-Hmt657UE3v349uYmvUXEvW/libHSnetwork-2.8.0.1-Hmt657UE3v349uYmvUXEvW.a(BSD.o)(.text+0xc902): warning: Using 'getprotobyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking

I don't know exactly why, but I think it's not a problem with this package, but a general problem with network related libraries including getprotobyname.