Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
fix(log): make logging configuration atomic
Martin Vogelc51482ae4540
diff --git a/src/foundation/log.c b/src/foundation/log.cindex 4d4477928..1fd7b351b 100644--- a/src/foundation/log.c+++ b/src/foundation/log.c@@ -6,15 +6,27 @@#include <ctype.h>#include <inttypes.h>#include <stdarg.h>+#include <stdatomic.h>#include <stdint.h>#include <stdio.h>#include <stdlib.h>#include <string.h>-static CBMLogLevel g_log_level = CBM_LOG_INFO;-static CBMLogFormat g_log_format = CBM_LOG_FORMAT_TEXT;-static cbm_log_sink_fn g_log_sink = NULL;-static CBMLogSinkMode g_log_sink_mode = CBM_LOG_SINK_REPLACE;+/* These four are written by whatever thread configures logging and read by+ * every thread that logs — daemon connection workers, pipeline workers, the+ * watcher. They were plain globals, which is a data race on the SINK in the+ * strict sense that matters: emit_line reads the pointer and CALLS it, so a+ * torn or stale read is a jump through a partially-written pointer, not just+ * a stale value. Found by running the daemon_runtime suite under TSan (that+ * suite had been excluded from the TSan set, which is why it went unseen).+ *+ * Relaxed ordering is the right level: each is an independent scalar with no+ * happens-before relationship to publish alongside it, and the log path must+ * stay cheap enough that nobody is tempted to route around it. */+static _Atomic CBMLogLevel g_log_level = CBM_LOG_INFO;+static _Atomic CBMLogFormat g_log_format = CBM_LOG_FORMAT_TEXT;+static _Atomic cbm_log_sink_fn g_log_sink = NULL;+static _Atomic CBMLogSinkMode g_log_sink_mode = CBM_LOG_SINK_REPLACE;/* CBM_LOG_LEVEL support — distilled from #414 (closes #413, thanks @santanusinha). */void cbm_log_init_from_env(void) {@@ -76,24 +88,26 @@ void cbm_log_set_sink(cbm_log_sink_fn fn) {}void cbm_log_set_sink_ex(cbm_log_sink_fn fn, CBMLogSinkMode mode) {- g_log_sink = fn;- g_log_sink_mode = mode;+ /* Mode first: a reader that observes the new sink then reads the mode can+ * never see the mode belonging to the PREVIOUS sink. */+ atomic_store_explicit(&g_log_sink_mode, mode, memory_order_relaxed);+ atomic_store_explicit(&g_log_sink, fn, memory_order_relaxed);}void cbm_log_set_level(CBMLogLevel level) {- g_log_level = level;+ atomic_store_explicit(&g_log_level, level, memory_order_relaxed);}CBMLogLevel cbm_log_get_level(void) {- return g_log_level;+ return atomic_load_explicit(&g_log_level, memory_order_relaxed);}void cbm_log_set_format(CBMLogFormat format) {- g_log_format = format;+ atomic_store_explicit(&g_log_format, format, memory_order_relaxed);}CBMLogFormat cbm_log_get_format(void) {- return g_log_format;+ return atomic_load_explicit(&g_log_format, memory_order_relaxed);}static const char *level_str(CBMLogLevel level) {@@ -196,9 +210,13 @@ static void finish_line(char *buf, size_t bufsz, size_t pos) {}static void emit_line(const char *line) {- if (g_log_sink) {- g_log_sink(line);- if (g_log_sink_mode == CBM_LOG_SINK_REPLACE) {+ /* Load ONCE: re-reading the global between the test and the call would+ * let a concurrent cbm_log_set_sink turn a checked pointer into a NULL+ * call. */+ cbm_log_sink_fn sink = atomic_load_explicit(&g_log_sink, memory_order_relaxed);+ if (sink) {+ sink(line);+ if (atomic_load_explicit(&g_log_sink_mode, memory_order_relaxed) == CBM_LOG_SINK_REPLACE) {return;}}@@ -206,7 +224,7 @@ static void emit_line(const char *line) {}void cbm_log(CBMLogLevel level, const char *msg, ...) {- if (level < g_log_level) {+ if (level < atomic_load_explicit(&g_log_level, memory_order_relaxed)) {return;}@@ -215,7 +233,7 @@ void cbm_log(CBMLogLevel level, const char *msg, ...) {va_list args;va_start(args, msg);- if (g_log_format == CBM_LOG_FORMAT_JSON) {+ if (atomic_load_explicit(&g_log_format, memory_order_relaxed) == CBM_LOG_FORMAT_JSON) {append_raw(line_buf, sizeof(line_buf), &pos, "{\"level\":");append_json_string(line_buf, sizeof(line_buf), &pos, level_str(level));append_raw(line_buf, sizeof(line_buf), &pos, ",\"event\":");