Skip to content

Commit ff9e320

Browse files
committed
Replace pthread-based logging buffer with signal masking for thread safety
This commit replaces the pthread mutex-based synchronization in the async logging buffer with signal masking, eliminating the pthread dependency while maintaining thread safety. Fixes: #38 Signed-off-by: Jindrich Novy <[email protected]>
1 parent 88628cc commit ff9e320

File tree

11 files changed

+700
-4
lines changed

11 files changed

+700
-4
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LIBEXECDIR ?= ${PREFIX}/libexec
55
PKG_CONFIG ?= pkg-config
66
HEADERS := $(wildcard src/*.h)
77

8-
OBJS := src/conmon.o src/cmsg.o src/ctr_logging.o src/utils.o src/cli.o src/globals.o src/cgroup.o src/conn_sock.o src/oom.o src/ctrl.o src/ctr_stdio.o src/parent_pipe_fd.o src/ctr_exit.o src/runtime_args.o src/close_fds.o src/seccomp_notify.o
8+
OBJS := src/conmon.o src/cmsg.o src/ctr_logging.o src/ctr_logging_buffer.o src/utils.o src/cli.o src/globals.o src/cgroup.o src/conn_sock.o src/oom.o src/ctrl.o src/ctr_stdio.o src/parent_pipe_fd.o src/ctr_exit.o src/runtime_args.o src/close_fds.o src/seccomp_notify.o
99

1010
MAKEFILE_PATH := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
1111

nix/derivation.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ with pkgs; stdenv.mkDerivation rec {
2424
# Static builds will use PKG_CONFIG_PATH approach instead
2525
];
2626
prePatch = ''
27-
export CFLAGS='-static -pthread'
27+
export CFLAGS='-static'
2828
export LDFLAGS='-s -w -static-libgcc -static'
2929
export EXTRA_LDFLAGS='-s -w -linkmode external -extldflags "-static -lm"'
3030
${lib.optionalString (!enableSystemd) "export DISABLE_SYSTEMD=1"}

src/cli.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "cli.h"
22
#include "globals.h"
33
#include "ctr_logging.h"
4+
#include "ctr_logging_buffer.h"
45
#include "config.h"
56
#include "utils.h"
67

@@ -224,6 +225,14 @@ void process_cli()
224225

225226
configure_log_drivers(opt_log_path, opt_log_size_max, opt_log_global_size_max, opt_cid, opt_name, opt_log_tag, opt_log_labels);
226227

228+
/* Initialize async logging for improved log performance */
229+
if (!init_async_logging()) {
230+
nwarn("Failed to initialize async logging, falling back to direct logging");
231+
} else {
232+
/* Register cleanup handler for async logging */
233+
atexit(shutdown_async_logging);
234+
}
235+
227236
/* Warn if --no-container-partial-message is used without journald logging */
228237
if (opt_no_container_partial_message && !logging_is_journald_enabled()) {
229238
nwarnf("--no-container-partial-message has no effect without journald log driver");

src/conmon.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "conn_sock.h"
1515
#include "ctrl.h"
1616
#include "ctr_stdio.h"
17+
#include "ctr_logging_buffer.h"
1718
#include "config.h"
1819
#include "parent_pipe_fd.h"
1920
#include "ctr_exit.h"
@@ -418,6 +419,8 @@ int main(int argc, char *argv[])
418419
if (mainfd_stderr >= 0) {
419420
g_unix_fd_add(mainfd_stderr, G_IO_IN, stdio_cb, GINT_TO_POINTER(STDERR_PIPE));
420421
}
422+
/* Setup log buffer timer for async flushing */
423+
setup_log_timer_in_main_loop();
421424

422425
if (opt_timeout > 0) {
423426
g_timeout_add_seconds(opt_timeout, timeout_cb, NULL);

src/ctr_exit.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "parent_pipe_fd.h"
77
#include "globals.h"
88
#include "ctr_logging.h"
9+
#include "ctr_logging_buffer.h"
910
#include "close_fds.h"
1011
#include "oom.h"
1112

@@ -129,6 +130,8 @@ void runtime_exit_cb(G_GNUC_UNUSED GPid pid, int status, G_GNUC_UNUSED gpointer
129130
{
130131
runtime_status = status;
131132
create_pid = -1;
133+
/* Flush logs before runtime exit to ensure CRI-O sees all logs */
134+
flush_log_buffer();
132135
g_main_loop_quit(main_loop);
133136
}
134137

@@ -137,6 +140,10 @@ void container_exit_cb(G_GNUC_UNUSED GPid pid, int status, G_GNUC_UNUSED gpointe
137140
if (get_exit_status(status) != 0) {
138141
ninfof("container %d exited with status %d", pid, get_exit_status(status));
139142
}
143+
144+
/* Force flush async log buffer before container exit to ensure CRI-O sees all logs */
145+
flush_log_buffer();
146+
140147
container_status = status;
141148
container_pid = -1;
142149
/* In the case of a quickly exiting exec command, the container exit callback
@@ -149,6 +156,8 @@ void container_exit_cb(G_GNUC_UNUSED GPid pid, int status, G_GNUC_UNUSED gpointe
149156
return;
150157
}
151158

159+
/* Final flush before main loop quits */
160+
flush_log_buffer();
152161
g_main_loop_quit(main_loop);
153162
}
154163

src/ctr_logging.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#define _GNU_SOURCE
22
#include "ctr_logging.h"
3+
#include "ctr_logging_buffer.h"
34
#include "cli.h"
45
#include "config.h"
56
#include <ctype.h>
@@ -761,6 +762,9 @@ static void set_k8s_timestamp(char *buf, ssize_t buflen, const char *pipename)
761762
/* Force closing any open FD. */
762763
void close_logging_fds(void)
763764
{
765+
/* Shutdown async logging system */
766+
shutdown_async_logging();
767+
764768
if (k8s_log_fd >= 0)
765769
close(k8s_log_fd);
766770
k8s_log_fd = -1;
@@ -1243,6 +1247,9 @@ static void reopen_k8s_file(void)
12431247

12441248
void sync_logs(void)
12451249
{
1250+
/* Flush any buffered logs before syncing */
1251+
flush_log_buffer();
1252+
12461253
/* Sync the logs to disk */
12471254
if (k8s_log_fd > 0)
12481255
if (fsync(k8s_log_fd) < 0)

0 commit comments

Comments
 (0)