Title: SIGSEGV using `journal.Reader`, thread safety issue? · Issue #143 · systemd/python-systemd · GitHub
Open Graph Title: SIGSEGV using `journal.Reader`, thread safety issue? · Issue #143 · systemd/python-systemd
X Title: SIGSEGV using `journal.Reader`, thread safety issue? · Issue #143 · systemd/python-systemd
Description: Env: $ python3 --version Python 3.11.2 $ systemctl --version systemd 252 (252.36-1~deb12u1) +PAM +AUDIT +SELINUX +APPARMOR +IMA +SMACK +SECCOMP +GCRYPT -GNUTLS +OPENSSL +ACL +BLKID +CURL +ELFUTILS +FIDO2 +IDN2 -IDN +IPTC +KMOD +LIBCRYPTS...
Open Graph Description: Env: $ python3 --version Python 3.11.2 $ systemctl --version systemd 252 (252.36-1~deb12u1) +PAM +AUDIT +SELINUX +APPARMOR +IMA +SMACK +SECCOMP +GCRYPT -GNUTLS +OPENSSL +ACL +BLKID +CURL +ELFUTILS ...
X Description: Env: $ python3 --version Python 3.11.2 $ systemctl --version systemd 252 (252.36-1~deb12u1) +PAM +AUDIT +SELINUX +APPARMOR +IMA +SMACK +SECCOMP +GCRYPT -GNUTLS +OPENSSL +ACL +BLKID +CURL +ELFUTILS ...
Opengraph URL: https://github.com/systemd/python-systemd/issues/143
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"SIGSEGV using `journal.Reader`, thread safety issue?","articleBody":"### Env:\n```\n$ python3 --version\nPython 3.11.2\n$ systemctl --version\nsystemd 252 (252.36-1~deb12u1)\n+PAM +AUDIT +SELINUX +APPARMOR +IMA +SMACK +SECCOMP +GCRYPT -GNUTLS +OPENSSL +ACL +BLKID +CURL +ELFUTILS +FIDO2 +IDN2 -IDN +IPTC +KMOD +LIBCRYPTSETUP +LIBFDISK +PCRE2 -PWQUALITY +P11KIT +QRENCODE +TPM2 +BZIP2 +LZ4 +XZ +ZLIB +ZSTD -BPF_FRAMEWORK -XKBCOMMON +UTMP +SYSVINIT default-hierarchy=unified\n$ python3 -c \"from systemd import journal; print(journal.__version__)\"\n235\n```\n### Issue:\nI got sporadically a segfault using journal.Reader from python-systemd module.\nThereby it seemed to happen by stop of thread, monitoring journal with Reader.\n\nI could reproduce it also under dgb, here is the callstack:\n\u003cdetails\u003e\n\n```\n[New Thread 0x7ffff5f876c0 (LWP 89339)]\n\nThread 2 \"f2b/f.DummyJail\" received signal SIGSEGV, Segmentation fault.\n[Switching to Thread 0x7ffff5f876c0 (LWP 89339)]\n0x00007ffff6f86d63 in ?? () from /lib/x86_64-linux-gnu/libsystemd.so.0\n(gdb) where\n#0 0x00007ffff6f86d63 in ?? () from /lib/x86_64-linux-gnu/libsystemd.so.0\n#1 0x00007ffff6f877f5 in ?? () from /lib/x86_64-linux-gnu/libsystemd.so.0\n#2 0x00007ffff6f6a848 in ?? () from /lib/x86_64-linux-gnu/libsystemd.so.0\n#3 0x00007ffff6f99502 in ?? () from /lib/x86_64-linux-gnu/libsystemd.so.0\n#4 0x00007ffff6f69403 in ?? () from /lib/x86_64-linux-gnu/libsystemd.so.0\n#5 0x00007ffff6f6961e in ?? () from /lib/x86_64-linux-gnu/libsystemd.so.0\n#6 0x00007ffff6f6d11a in sd_journal_get_fd () from /lib/x86_64-linux-gnu/libsystemd.so.0\n#7 0x00007ffff6f6f3c9 in sd_journal_wait () from /lib/x86_64-linux-gnu/libsystemd.so.0\n#8 0x00007ffff6cadf2c in ?? () from /usr/lib/python3/dist-packages/systemd/_reader.cpython-311-x86_64-linux-gnu.so\n#9 0x0000000000548ac8 in ?? ()\n#10 0x00000000005188b3 in _PyObject_MakeTpCall ()\n#11 0x000000000052c2f0 in _PyEval_EvalFrameDefault ()\n#12 0x0000000000585e54 in ?? ()\n#13 0x0000000000584e98 in ?? ()\n#14 0x000000000053034e in _PyEval_EvalFrameDefault ()\n#15 0x0000000000585e54 in ?? ()\n#16 0x0000000000584e98 in ?? ()\n#17 0x000000000067b32c in ?? ()\n#18 0x0000000000656384 in ?? ()\n#19 0x00007ffff7d381f5 in start_thread (arg=\u003coptimized out\u003e) at ./nptl/pthread_create.c:442\n#20 0x00007ffff7db889c in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:81\n```\n\u003c/details\u003e\n\nIt can be either some missing lock or atomic handling somewhere in journal.Reader, because if I rewrote the code to avoid too earlier clean-up, closing the journal with `Reader.close()` before join of thread (now it ensures that the thread working with Reader is really stopped, before clean-up happens), it doesn't happen anymore.\nSo my assumption is if TH2 is somewhere within `reader.wait()` and TH1 would invoke `reader.close()`, it could segfault.\nSure, it can be avoided on the user side, just SF is SF and shall not happen normally.\n\nNaive attempts to reproduce it with simple wait/close in different threads did not work, but I could find a bit complex scenario (with a busy-wait \u0026 lock using to synchronize the timing a bit), where it is pretty reproducible, almost every time not later than on 10 iteration:\n```python\nimport time\nfrom systemd import journal\nfrom threading import Thread, Lock\nl = Lock(); # just used for \"better\" timing here to get SF\n\ndef test():\n stat = {};\n jr = journal.Reader()\n def mon():\n print('start')\n try:\n jr.seek_head()\n print('work')\n stat['started'] = 1\n l.acquire(); l.release()\n # *-* [A], thread is here as another thread is on *-* [B]\n while (j := jr):\n if j.wait(3) in (journal.NOP, journal.APPEND):\n j.get_next()\n except Exception as e:\n print('ERR: %s' % e)\n if jr: jr.close()\n th = Thread(target=mon); \n th.start()\n # just \"sync\" wait and close calls:\n while not stat: time.sleep(0.00001);\n l.acquire(); l.release()\n # *-* [B], thread is here as another thread is on *-* [A]\n # do evil close:\n jr.close(); jr = None\n th.join()\n print('OK')\n\nfor i in range(100): # just 100 attempts, because of sporadic matter\n test()\n```","author":{"url":"https://github.com/sebres","@type":"Person","name":"sebres"},"datePublished":"2025-03-30T03:29:49.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/143/python-systemd/issues/143"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:66540eac-1857-03f0-5fba-750b746a48e4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A8BA:2FE0E1:F4E635:15467E6:6A4DF0E1 |
| html-safe-nonce | 0b5cb2af829922619f13a4d91b711ff38fd39c97cac303d5e9f84db9db61fd6f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBOEJBOjJGRTBFMTpGNEU2MzU6MTU0NjdFNjo2QTRERjBFMSIsInZpc2l0b3JfaWQiOiIyMjU4MDM3NTYzOTc2NDQ2MTc4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 8470453f6943c9fd0f01ddab9403efe0951c5e4ec3034b8d3510eea39317518d |
| hovercard-subject-tag | issue:2958637592 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/systemd/python-systemd/143/issue_layout |
| twitter:image | https://opengraph.githubassets.com/10c48acce0cd8cafc8dccb7a5f549b618bb5ebde840b692a7a045b8eb02d334d/systemd/python-systemd/issues/143 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/10c48acce0cd8cafc8dccb7a5f549b618bb5ebde840b692a7a045b8eb02d334d/systemd/python-systemd/issues/143 |
| og:image:alt | Env: $ python3 --version Python 3.11.2 $ systemctl --version systemd 252 (252.36-1~deb12u1) +PAM +AUDIT +SELINUX +APPARMOR +IMA +SMACK +SECCOMP +GCRYPT -GNUTLS +OPENSSL +ACL +BLKID +CURL +ELFUTILS ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sebres |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5818716c93c6a2925b815402541a32814e43a7b1261c322b0c2df75224289566 |
| turbo-cache-control | no-preview |
| go-import | github.com/systemd/python-systemd git https://github.com/systemd/python-systemd.git |
| octolytics-dimension-user_id | 1918868 |
| octolytics-dimension-user_login | systemd |
| octolytics-dimension-repository_id | 38590109 |
| octolytics-dimension-repository_nwo | systemd/python-systemd |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 38590109 |
| octolytics-dimension-repository_network_root_nwo | systemd/python-systemd |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f4bb89367ca678f057d79b1abc45d6675b1bd5b2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width