@cryptotaxi247 / netdata-1 / commits / 8fd4f3e0a

ensure atomicity when logging pending message (#20185)

Costa Tsaousis committed Apr 26, 2025 at 10:18 UTC 8fd4f3e0a2734ab5a9bd72424e6bcc64b34d6b52
1 file changed +25 -22
src/libnetdata/log/nd_log.c
+25 -22
@@ -343,48 +343,51 @@ static void nd_logger(const char *file, const char *function, const unsigned lon
343 nd_logger_log_fields(spinlock, fp, limit, priority, output, &nd_log.sources[source],
344 thread_log_fields, THREAD_FIELDS_MAX);
345
346 - if(nd_log.sources[source].pending_msg) {
347 - // log a pending message
346 + if(nd_log.sources[source].pending_msg && spinlock_trylock(&nd_log.sources[source].limits.spinlock)) {
347 + // we have to check again if the pending message is still there
348
349 - nd_logger_unset_all_thread_fields();
349 + if(nd_log.sources[source].pending_msg) {
350 + nd_logger_unset_all_thread_fields();
351
351 - thread_log_fields[NDF_TIMESTAMP_REALTIME_USEC].entry = (struct log_stack_entry){
352 + thread_log_fields[NDF_TIMESTAMP_REALTIME_USEC].entry = (struct log_stack_entry){
353 .set = true,
354 .type = NDFT_U64,
355 .u64 = now_realtime_usec(),
355 - };
356 + };
357
357 - thread_log_fields[NDF_LOG_SOURCE].entry = (struct log_stack_entry){
358 + thread_log_fields[NDF_LOG_SOURCE].entry = (struct log_stack_entry){
359 .set = true,
360 .type = NDFT_TXT,
361 .txt = nd_log_id2source(source),
361 - };
362 + };
363
363 - thread_log_fields[NDF_SYSLOG_IDENTIFIER].entry = (struct log_stack_entry){
364 + thread_log_fields[NDF_SYSLOG_IDENTIFIER].entry = (struct log_stack_entry){
365 .set = true,
366 .type = NDFT_TXT,
367 .txt = program_name,
367 - };
368 + };
369
369 - thread_log_fields[NDF_MESSAGE].entry = (struct log_stack_entry){
370 + thread_log_fields[NDF_MESSAGE].entry = (struct log_stack_entry){
371 .set = true,
372 .type = NDFT_TXT,
373 .txt = nd_log.sources[source].pending_msg,
373 - };
374 + };
375
375 - thread_log_fields[NDF_MESSAGE_ID].entry = (struct log_stack_entry){
376 - .set = nd_log.sources[source].pending_msgid != NULL,
377 - .type = NDFT_UUID,
378 - .uuid = nd_log.sources[source].pending_msgid,
379 - };
376 + thread_log_fields[NDF_MESSAGE_ID].entry = (struct log_stack_entry){
377 + .set = nd_log.sources[source].pending_msgid != NULL,
378 + .type = NDFT_UUID,
379 + .uuid = nd_log.sources[source].pending_msgid,
380 + };
381 +
382 + nd_logger_log_fields(
383 + spinlock, fp, false, priority, output, &nd_log.sources[source], thread_log_fields, THREAD_FIELDS_MAX);
384
381 - nd_logger_log_fields(spinlock, fp, false, priority, output,
382 - &nd_log.sources[source],
383 - thread_log_fields, THREAD_FIELDS_MAX);
385 + freez((void *)nd_log.sources[source].pending_msg);
386 + nd_log.sources[source].pending_msg = NULL;
387 + nd_log.sources[source].pending_msgid = NULL;
388 + }
389
385 - freez((void *)nd_log.sources[source].pending_msg);
386 - nd_log.sources[source].pending_msg = NULL;
387 - nd_log.sources[source].pending_msgid = NULL;
390 + spinlock_unlock(&nd_log.sources[source].limits.spinlock);
391 }
392
393 errno_clear();