◐ Shell
reader mode source ↗
Skip to content

Fixes SIGSEGV and improve thread safety of journal.Reader class#144

Open
sebres wants to merge 4 commits into
systemd:mainfrom
sebres:gh-143-sf-fix
Open

Fixes SIGSEGV and improve thread safety of journal.Reader class#144
sebres wants to merge 4 commits into
systemd:mainfrom
sebres:gh-143-sf-fix

Conversation

@sebres

@sebres sebres commented Apr 1, 2025

Copy link
Copy Markdown

This PR fixes threaded issues of journal.Reader class and improves its thread-safety, using simple protection with ref-counter;
It would avoid segfault by closing journal across threads.

The algorithm is simple, here short explanation illustrating the approach on an example of #143:

TH Action ref_count j_handle
... Reader created and has initial ref-count 1 1 valid
A Enter Reader_wait(), increment ref-count, release the lock (GIL), enter sd_journal_wait() 2 valid
A Leave sd_journal_wait(), acquiring the lock (GIL), decrement ref-count, not calling sd_journal_close() because of ref_count == 1, leave Reader_wait() 1 valid
- ... now thread A repeats the wait() however thread B invokes close() in-between ... - -
A Enter Reader_wait(), increment ref-count, release the lock (GIL), enter sd_journal_wait() 2 valid
B Enter Reader_close(), set closed flag, decrement ref-count, but don't close it because of ref_count > 0 1 valid
A Leave sd_journal_wait(), acquiring the lock (GIL), decrement ref-count, calling sd_journal_close() because of ref_count == 0, leave Reader_wait() 0 NULL

If thread B manages to close the handle before A enters wait mode, it is also safe, because then sd_journal_wait() would generate EINVARG and Reader.wait() raises an error ([Errno 22] Invalid argument).

Anyway, no segfault happens anymore and it is more or less thread-safe.

closes gh-143

sebres added 2 commits April 1, 2025 16:33
…th ref-counter; avoid segfault by closing journal across threads (closes systemdgh-143)
…ids unexpected states on repeated Reader.close)
@sebres sebres force-pushed the gh-143-sf-fix branch 2 times, most recently from 5a7719b to 03eec9d Compare April 2, 2025 16:31
@sebres

sebres commented Apr 2, 2025

Copy link
Copy Markdown
Author

7c99a39 seems to fix the GHA flows now (completely different issue), so can be cherry-picked to main branch as it is (or I could make a separate PR if necessary).

@sebres

sebres commented Jun 4, 2025

Copy link
Copy Markdown
Author

Ping.
Is this repository alive?

@hongquan

Copy link
Copy Markdown
Contributor

The PR #147 will help fix the GH Action headache for this PR.

@sebres

sebres commented Jun 30, 2025

Copy link
Copy Markdown
Author

The PR #147 will help fix the GH Action headache for this PR.

Hmm... There is basically no problem to fix "for this PR" - as you can see all GHA checks are passed.
Nothing about #147 (maybe it solves more, no time to review), just... actually everything work here in this PR as expected.

@keszybz keszybz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hide comment

I think the thread-safety stuff is confused. The last commit seems useful, it might be worth submitting it as a separate PR.

Note we also have #148 which tries to update the build system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

SIGSEGV using journal.Reader, thread safety issue?

4 participants