From 5fb5a2908d4d0d2f82bbc52bf92cc60dfbfe2a8c Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Thu, 9 May 2024 20:30:36 +0200 Subject: [PATCH] jsonlogger: increase pipe buffer to reduce likelihood of corrupted messages Our JSON messages can get rather large which can cause a buffer overrun when the reading program is not scheduled at the same time. When the kernel returns EAGAIN we abort writing the current JSON message. However, the unfinished (and thus invalid) JSON message is still in the pipe and will be processed by the reading program. Increase the pipe buffer from the default 65536 to reduce the likelihood of this happening. It's difficult to completely prevent the issue as we don't want to slow down the main program due to slow logging. --- impl/src/jsonlogger.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/impl/src/jsonlogger.c b/impl/src/jsonlogger.c index 3b63439..fb67122 100644 --- a/impl/src/jsonlogger.c +++ b/impl/src/jsonlogger.c @@ -1,3 +1,6 @@ +// For F_SETPIPE_SZ +#define _GNU_SOURCE + #include #include #include @@ -29,6 +32,12 @@ static bool start_message(void) } return false; } + // Increase pipe buffer to prevent/reduce EAGAIN during a JSON + // message to prevent corrupted JSON messages. 1048576 is the + // current maximum as permitted by the Linux kernel. + if (fcntl(m_fifo_fd, F_SETPIPE_SZ, 1048576) < 0) { + perror("fcntl"); + } } ssize_t ret = write(m_fifo_fd, "{", 1);