Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IO mutex and threads #5141

Closed
vicuna opened this issue Sep 2, 2010 · 4 comments
Closed

IO mutex and threads #5141

vicuna opened this issue Sep 2, 2010 · 4 comments

Comments

@vicuna
Copy link

vicuna commented Sep 2, 2010

Original bug ID: 5141
Reporter: @ygrek
Status: resolved (set by @xavierleroy on 2012-07-04T18:00:23Z)
Resolution: suspended
Priority: normal
Severity: major
Version: 3.11.2
Category: threads
Related to: #7503

Bug description

Consider :

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
#3 0x000000000041a1ab in caml_io_mutex_lock ()
#4 0x00000000004266fb in caml_ml_output ()
#5 0x000000000042e65c in caml_c_call ()
#6 0x0000000000000001 in ?? ()
#7 0x0000000000410e9a in camlPervasives__print_endline_298 ()
#8 0x0000000000000000 in ?? ()

Running ./io ok will behave as expected (pause 1 sec then continue).

Additional information

Reproduces :
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

@vicuna
Copy link
Author

vicuna commented Jul 4, 2012

Comment author: @xavierleroy

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.

@vicuna vicuna closed this as completed Jul 4, 2012
@vicuna
Copy link
Author

vicuna commented Jul 8, 2012

Comment author: @ygrek

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?

@vicuna
Copy link
Author

vicuna commented Jan 12, 2018

Comment author: @ygrek

#7503 yay

@stedolan
Copy link
Contributor

Fixed in #9722.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants