sdjournal: SeekTail should be followed by Previous, not Next #345
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
sd_journal_next is supposed to return 0 (i.e. EOF) if called at the end of the journal, which would make this issue immediately apparent. However, there is an open bug that causes it to advance to the wrong message instead (not the last one in the journal) after a call to sd_journal_seek_tail, which causes code which follows the documented use here to produce non-obviously incorrect results (see systemd/systemd#9934).
Instead, sd_journal_previous will correctly seek to the last journal entry, with the unavoidable race condition that it may not actually be the last entry if the journal is written to between calls to sd_journal_seek_tail and sd_journal_previous.
This can be verified by comparing the output of
journalctl -o json | tail -1 | jq -r .MESSAGE_ID
with that of the following simple Go programand noting that the message IDs are equal (again, with an unavoidable race condition if a message is written in between).