Skip to content

Commit fe74581

Browse files
committed
feat: preserve backtrace_enabled between domains
We've been seeing opaque errors where, at Domain.join time, an exception is raised because that exception bubbled up to the top of the domain's stack. Unfortunately, the backtrace that is printed is the parent's backtrace, not the child's (that is, it starts at Domain.join and bubbles down to the parent's entrypoint). What we want to see is what happens inside the child, but it's lost by this point in time. Thinking that this was owing to the implemention of Domain.join, which catches and rethrows the exception but without capturing the raw backtrace, I believed the correct fix to be in the OCaml runtime, and put out ocaml#14358 . KC, however, pointed out that this isn't the right fix because the child's stack is fundamentally not propagated between domains. Mea culpa. semgrep/semgrep-proprietary#5022 fixed this at the level of `Concurrent.map`, but the ergonomics of that patch weren't great because we redundantly re-enable backtraces for each iteration of the map. I later modified the OCaml runtime itself to do this, which I merged upstream at ocaml#14416 , after filing ocaml#14411 . This patch is just a backport of that PR (which was already reviewed by compiler folks.)
1 parent 7497113 commit fe74581

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

runtime/domain.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -742,9 +742,10 @@ static void domain_create(uintnat initial_minor_heap_wsize,
742742

743743
domain_state->parser_trace = 0;
744744

745-
if (caml_params->backtrace_enabled) {
746-
caml_record_backtraces(1);
747-
}
745+
bool bt_enabled = parent
746+
? parent->backtrace_active
747+
: caml_params->backtrace_enabled;
748+
caml_record_backtraces(bt_enabled);
748749

749750
#ifndef NATIVE_CODE
750751
domain_state->external_raise = NULL;

stdlib/printexc.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ val record_backtrace: bool -> unit
7474
on (if [b = true]) or off (if [b = false]). Initially, backtraces
7575
are not recorded, unless the [b] flag is given to the program
7676
through the [OCAMLRUNPARAM] variable.
77+
@before 5.3-semgrep Spawned domains' backtraces would not be recorded,
78+
even if the parent had called [record_backtrace true].
7779
@since 3.11
7880
*)
7981

0 commit comments

Comments
 (0)