Skip to content

Commit 5414add

Browse files
committed
Make log mutex immune to raising logging functions (dbuenzli#57).
1 parent 7f8614f commit 5414add

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

B0.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ let test_multi =
8080
let test_threaded =
8181
test ~/"test/test_threaded.ml" ~requires:[logs_fmt; logs_threaded; threads]
8282

83+
let test_mutext_safe =
84+
test ~/"test/test_mutex_safe.ml" ~requires:[logs_fmt; logs_threaded; threads]
85+
8386
let test_lwt =
8487
let requires = [b0_std; logs_fmt; logs_lwt; fmt; fmt_tty; lwt; lwt_unix] in
8588
test ~/"test/test_lwt.ml" ~requires

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2+
3+
* Make log mutex immune to raising logging functions.
4+
Thanks to Nathan Taylor for the report and the repro (#57).
5+
16
v0.9.0 2025-07-08 Zagreb
27
------------------------
38

src/logs.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,11 @@ let report src level ~over k msgf =
188188
let mutex = Atomic.get reporter_mutex' in
189189
let over () = over (); mutex.unlock () in
190190
mutex.lock ();
191-
(Atomic.get reporter').report src level ~over k msgf
191+
try (Atomic.get reporter').report src level ~over k msgf with
192+
| exn ->
193+
let bt = Printexc.get_raw_backtrace () in
194+
over ();
195+
Printexc.raise_with_backtrace exn bt
192196

193197
let pp_brackets pp_v ppf v =
194198
Format.pp_print_char ppf '['; pp_v ppf v; Format.pp_print_char ppf ']'

test/test_mutex_safe.ml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
(*---------------------------------------------------------------------------
2+
Copyright (c) 2025 The logs programmers. All rights reserved.
3+
SPDX-License-Identifier: ISC
4+
---------------------------------------------------------------------------*)
5+
6+
(* See https://github.com/dbuenzli/logs/issues/57 *)
7+
8+
let src = Logs.Src.create "repro case"
9+
module Log = (val Logs.src_log src)
10+
11+
let setup_logs () =
12+
Logs.set_reporter (Logs_fmt.reporter ());
13+
Logs.set_level ~all:true (Some Logs.Debug);
14+
Logs_threaded.enable ();
15+
let m = Mutex.create () in
16+
let lock () = Mutex.lock m and unlock () = Mutex.unlock m in
17+
Logs.set_reporter_mutex ~lock ~unlock
18+
19+
let main () =
20+
setup_logs ();
21+
(try Logs.app (fun _m -> failwith "uh oh...") with Failure _ -> ());
22+
Logs.app (fun m -> m "It works!");
23+
0
24+
25+
let () = if !Sys.interactive then () else exit (main ())

0 commit comments

Comments
 (0)