Skip to content

logger: Set the queue and the log file up before the writer thread starts - #6

Open
jasonsopko wants to merge 1 commit into
CONVOYMining:masterfrom
jasonsopko:convoy-logger-wait-init
Open

jasonsopko wants to merge 1 commit into
CONVOYMining:masterfrom
jasonsopko:convoy-logger-wait-init

Conversation

@jasonsopko

@jasonsopko jasonsopko commented Sep 4, 2026 •

Copy link
Copy Markdown

What

datum_logger_init does the logger's setup itself, the queue allocation and opening the log file, before it starts the writer thread. The thread keeps only its loop.

Why

It started the thread and returned at once. Anything logged in the next millisecond, which includes datum_protocol_init and datum_api_init, went to the console if console logging was on and nowhere if it was off. With log_to_console: false and a log file, the DATUM pool host is blank. NON-POOLED MINING! warning never reached the file.

The first version of this PR waited for the thread instead. Doing the setup in init removes the window rather than waiting it out, and it makes a setup failure an init failure: it now returns -1 like the other init functions and main exits on it, where the thread's panic_from_thread() would only have spun.

How I tested

Same config both ways, file on, console off, a gateway pointed at an unreachable node, five seconds. On master the file starts several lines in and the startup key and warning lines are missing; with this change the file starts with them. An unwritable log path now logs two FATAL lines and exits. Built with -Wall -Werror, --test passes.

Risk and rollback

None to the running loop; the log handle moved from a thread local to file scope, which the rotate path already treated as shared. Revert the commit.

@luke-jr luke-jr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just move the top of datum_logger_thread down before the thread gets started?

@jasonsopko
jasonsopko force-pushed the convoy-logger-wait-init branch from 3b8ad35 to 18c2a13 Compare September 6, 2026 21:50
@jasonsopko

Copy link
Copy Markdown
Author

That's simpler, and it removes the window instead of waiting it out. The queue and log file setup now runs in init before pthread_create and the thread keeps only its loop. One thing that followed: a failure in that setup used to call panic_from_thread(), which from the main thread would only spin, so init returns -1 like the other init functions and main exits on it. Checked against an unreachable node: the file now starts with the first startup lines, which are missing on master.

@jasonsopko jasonsopko changed the title logger: Wait for the writer thread before returning from init logger: Set the queue and the log file up before the writer thread starts Sep 6, 2026
Comment thread src/datum_gateway.c Outdated
datum_logger_init();
if (datum_logger_init()) {
DLOG_FATAL("Error initializing the logger!");
usleep(100000);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a need to sleep here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the logger isn't up on that path, so the message prints directly. Removed.

Comment thread src/datum_logger.c Outdated
const char *level_text[] = { " ALL", "DEBUG", " INFO", " WARN", "ERROR", "FATAL" };

volatile bool datum_logger_initialized = false;
static FILE *log_handle = NULL;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about passing it as the thread's ptr instead?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, init passes the handle as the thread's argument and the static is gone. Init also checks pthread_create now, since it sets the ready flag itself.

…arts

datum_logger_init() returned as soon as the writer thread was created,
while the thread was still allocating its queue and opening the log
file. Anything logged in that window went to the console or nowhere,
so the first lines of a run were missing from the file.

Do that setup in init itself, before pthread_create, and leave the
thread with only its loop; init passes it the open log handle as its
argument. A failure to allocate or to open the file now returns -1 like
the other init functions, and main exits on it, instead of
panic_from_thread(), which from the main thread would only spin. Init
now marks the logger ready only once the thread has started, so a failed
pthread_create still logs to the console instead of queueing for a
thread that does not exist. A run against an unreachable node now has
its first startup lines in the file; on master the file starts several
lines in.
@jasonsopko
jasonsopko force-pushed the convoy-logger-wait-init branch from 18c2a13 to 97c7eef Compare September 26, 2026 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants