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.
This commit is contained in:
Simon Ruderich 2024-05-09 20:30:36 +02:00
parent 657ac4faa3
commit 5fb5a2908d
1 changed files with 9 additions and 0 deletions

View File

@ -1,3 +1,6 @@
// For F_SETPIPE_SZ
#define _GNU_SOURCE
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
@ -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);