From 57b9648d30f9159c0ec64b2b023fd285ca9e9fbb Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Thu, 21 Nov 2024 14:46:52 +0100 Subject: [PATCH] common: Resume the startup trace after exiting `io_loop` This was a bit harder to identify: during an `io_loop` run we suspend the current span before handing over to `io_loop`, and later when a callback is called we resume the span again. Depending on how we return from the `io_loop` instance that is used to drive the startup, we either have resumed the last span, or we don't. Since we start a span before `io_loop` and want it to be emitted afterwards, we need to take care of the case where we returned from a callback that did not resume, and therefore the current context is empty. Making `trace_span_resume` idempotent means we can just resume it manually. Ideally we'd push the suspend / resume logic down into `io_loop` itself, and then we'd have just one place. Maybe suspend and resume callbacks that can be configured in `io_loop`? --- lightningd/lightningd.c | 8 ++++++++ lightningd/test/run-find_my_abspath.c | 3 +++ 2 files changed, 11 insertions(+) diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index 4ec244964..c838a1b6b 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -1412,6 +1412,14 @@ int main(int argc, char *argv[]) /*~ Now handle sigchld, so we can clean up appropriately. */ sigchld_conn = notleak(io_new_conn(ld, sigchld_rfd, sigchld_rfd_in, ld)); + /* This span was started before handing control to `io_loop` + * which suspends active spans in-between processing + * events. Depending on how the `io_loop` was interrupted, the + * current context span may have been suspended. We need to + * manually resume it for this case. Notice that resuming is + * idempotent, and doing so repeatedly is safe. + */ + trace_span_resume(argv); trace_span_end(argv); /*~ Mark ourselves live. diff --git a/lightningd/test/run-find_my_abspath.c b/lightningd/test/run-find_my_abspath.c index 0c2039df7..53b22f4e5 100644 --- a/lightningd/test/run-find_my_abspath.c +++ b/lightningd/test/run-find_my_abspath.c @@ -263,6 +263,9 @@ void towire_node_id(u8 **pptr UNNEEDED, const struct node_id *id UNNEEDED) /* Generated stub for trace_span_end */ void trace_span_end(const void *key UNNEEDED) { fprintf(stderr, "trace_span_end called!\n"); abort(); } +/* Generated stub for trace_span_resume_ */ +void trace_span_resume_(const void *key UNNEEDED, const char *lbl UNNEEDED) +{ fprintf(stderr, "trace_span_resume_ called!\n"); abort(); } /* Generated stub for trace_span_start */ void trace_span_start(const char *name UNNEEDED, const void *key UNNEEDED) { fprintf(stderr, "trace_span_start called!\n"); abort(); }