Skip to content

Intermittent double free / segmentation fault when using GHC API with concurrent primitives

Summary

I was playing with using GHC API for parsing files in parallel and started getting intermittent segmentation faults / double free errors.

Steps to reproduce

  1. Fetch https://github.com/arybczak/ghc-tags/tree/ghc-bug and build the executable with cabal build (it's a small program that launches 4 workers that use GHC API to parse Haskell source files in parallel).
  2. Run the executable on a somewhat large Haskell project (Cabal works) in gdb, i.e.
gdb ./ghc-tags
run <cabal_repo_path>/cabal/Cabal +RTS -N4

repeatedly until you get an error.

Errors and backtraces differ, I think the most telling is the following:

free(): double free detected in tcache 2

Thread 1 "ghc-tags" received signal SIGABRT, Aborted.
0x00007ffff7b74ce5 in raise () from /usr/lib/libc.so.6
(gdb) bt
#0  0x00007ffff7b74ce5 in raise () from /usr/lib/libc.so.6
#1  0x00007ffff7b5e857 in abort () from /usr/lib/libc.so.6
#2  0x00007ffff7bb82b0 in __libc_message () from /usr/lib/libc.so.6
#3  0x00007ffff7bbf74a in malloc_printerr () from /usr/lib/libc.so.6
#4  0x00007ffff7bc158d in _int_free () from /usr/lib/libc.so.6
#5  0x00007ffff7e1882a in del_curterm_sp () from /usr/lib/libncursesw.so.6
#6  0x00000000035922e7 in runCFinalizers (list=<optimized out>) at rts/Weak.c:37
#7  0x0000000003592552 in runSomeFinalizers (all=all@entry=true) at rts/Weak.c:239
#8  0x0000000003599999 in doIdleGCWork (cap=cap@entry=0x3cb5850, all=all@entry=true)
    at rts/sm/GC.c:1901
#9  0x00000000035880ed in scheduleDoGC (pcap=pcap@entry=0x7fffffffde58, 
    task=task@entry=0x3cd7a90, force_major=force_major@entry=true) at rts/Schedule.c:1811
#10 0x00000000004052c3 in exitScheduler (wait_foreign=wait_foreign@entry=false)
    at rts/Schedule.c:2667
#11 0x0000000003585317 in hs_exit_ (wait_foreign=false) at rts/RtsStartup.c:392
#12 0x00000000035858c5 in shutdownHaskellAndExit (n=0, fastExit=fastExit@entry=0)
    at rts/RtsStartup.c:553
#13 0x0000000003594871 in hs_main (argc=<optimized out>, argv=<optimized out>, 
    main_closure=<optimized out>, rts_config=...) at rts/RtsMain.c:99
#14 0x000000000040eedd in main ()

Backtraces are always related to ncurses, but I have no idea where does it come from.

Expected behavior

No intermittent segfaults / double free errors.

Environment

  • GHC version used: 8.8.3

Optional:

  • Operating System: Arch Linux
  • System Architecture: x86_64
Edited by Andrzej Rybczak
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information