Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0005141OCamlOCaml generalpublic2010-09-02 14:432012-07-08 10:59
Reporterygrek 
Assigned To 
PrioritynormalSeveritymajorReproducibilityalways
StatusresolvedResolutionsuspended 
PlatformOSOS Version
Product Version3.11.2 
Target VersionFixed in Version 
Summary0005141: IO mutex and threads
DescriptionConsider :

let () =
  let ok = match Sys.argv with [|_;"ok"|] -> true | _ -> false in
  Sys.set_signal Sys.sigint (Sys.Signal_handle (fun _ ->
    (if ok then prerr_endline else print_endline) "signal received, sleep 1 second";
    Thread.delay 1.));
  while true do print_endline "ha ha ha ha ha ha ha" done

Compile with : ocamlopt -thread unix.cmxa threads.cmxa io.ml -o io

Running `./io hang` and pressing Ctrl-C will deadlock :
(gdb) bt
#0 __lll_lock_wait ()
    at ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:136
#1 0x00007f0fda04e0e9 in _L_lock_953 () from /lib/libpthread.so.0
#2 0x00007f0fda04df0b in __pthread_mutex_lock (mutex=0x1f30360)
    at pthread_mutex_lock.c:61
0000003 0x000000000041a1ab in caml_io_mutex_lock ()
0000004 0x00000000004266fb in caml_ml_output ()
0000005 0x000000000042e65c in caml_c_call ()
0000006 0x0000000000000001 in ?? ()
0000007 0x0000000000410e9a in camlPervasives__print_endline_298 ()
0000008 0x0000000000000000 in ?? ()

Running `./io ok` will behave as expected (pause 1 sec then continue).
Additional InformationReproduces :
  debian x86_64 with ocaml 3.11.2 and 3.12.0
  rhel4 with ocaml 3.11.2
  opensolaris x86_64 with ocaml 3.10.2

Reproduces but prints "signal received" before hanging :
  openbsd x86_64 with ocaml 3.11.1
  FreeBSD 6.3-STABLE i386 with ocaml 3.11.2
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
(0007636)
xleroy (administrator)
2012-07-04 20:00
edited on: 2012-07-04 20:05

Well, taking a lock from a signal handler can cause deadlock, indeed. I don't think there is anything we can do about it, except advise you not to use locks (directly or indirectly through standard I/O) in signal handlers. Even better: never use signals in a multithreaded program, because threads and signals just don't mix, in OCaml as in C and in any other language running under Unix.

I "suspend" this PR in the faint hope that someone will come up with a really clever solution, but I'm not holding my breath.

(0007656)
ygrek (reporter)
2012-07-08 10:59
edited on: 2012-07-08 11:00

I agree. In my (multithreaded) programs I now follow the rule to either use ExtUnix.signalfd for non-trivial signal handling or only set boolean flag in signal handler when using Unix module.

Maybe this problem should be noted in documentation?


- Issue History
Date Modified Username Field Change
2010-09-02 14:43 ygrek New Issue
2011-05-20 17:54 doligez Status new => acknowledged
2012-07-04 17:15 doligez Target Version => 4.00.0+dev
2012-07-04 20:00 xleroy Note Added: 0007636
2012-07-04 20:00 xleroy Status acknowledged => resolved
2012-07-04 20:00 xleroy Resolution open => suspended
2012-07-04 20:00 xleroy Target Version 4.00.0+dev =>
2012-07-04 20:05 xleroy Note Edited: 0007636 View Revisions
2012-07-08 10:59 ygrek Note Added: 0007656
2012-07-08 11:00 ygrek Note Edited: 0007656 View Revisions


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker