Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy setup of inotify_fd in sd-journal may cause clients to leak file descriptors #14695

Closed
msekletar opened this issue Jan 29, 2020 · 0 comments · Fixed by #14769 or #15243
Closed

Lazy setup of inotify_fd in sd-journal may cause clients to leak file descriptors #14695

msekletar opened this issue Jan 29, 2020 · 0 comments · Fixed by #14769 or #15243
Labels
bug 🐛 Programming errors, that need preferential fixing journal

Comments

@msekletar
Copy link
Contributor

msekletar commented Jan 29, 2020

systemd version the issue has been seen with

  • git master

Used distribution

  • Fedora

Expected behaviour you didn't see

  • No deleted files referenced by the sd-journal client after calling sd_journal_process()

Unexpected behaviour you saw

  • Files that were deleted by the journald server are still opened by the sd-journal client after calling sd_journal_process(). This is because files were opened by the client and deleted by the server before we've setup inotify monitor.

Steps to reproduce the problem

#!/usr/bin/python3

from systemd import journal
from os import listdir,readlink
from subprocess import run

def list_fds():
    for fd in listdir("/proc/self/fd"):
        try:
            print(f"    {fd} -> {readlink('/proc/self/fd/' + fd)}")
        except FileNotFoundError as e:
            pass
devnull = open("/dev/null", "w")

print("Rotate journal 3 times")
for i in range(3):
    run(["journalctl", "--rotate"], stdout=devnull, stderr=devnull)

print("Open journal")
r = journal.Reader()

print("List fds")
list_fds()

print("Delete some journal files")
run(["journalctl", "--vacuum-files", "1"], stdout=devnull, stderr=devnull)

print("Process journal events")
r.wait(0)

print("List fds")
list_fds()

Example output of the reproducer script

Rotate journal 3 times
Open journal
List fds
    0 -> /dev/pts/0
    1 -> /dev/pts/0
    2 -> /dev/pts/0
    3 -> /dev/null
    6 -> /var/log/journal/56161deef0f24959be056aa81810ce4f/system@3d354d8051214925b8d43f50e63d7307-000000000011eb29-00059d4928d6c3d6.journal
    7 -> /var/log/journal/56161deef0f24959be056aa81810ce4f/system.journal
    8 -> /var/log/journal/56161deef0f24959be056aa81810ce4f/user-1000@aaabf79080ca4edcafc88708e607b702-000000000011ebb7-00059d497ac0ed84.journal
    9 -> /var/log/journal/56161deef0f24959be056aa81810ce4f/system@3d354d8051214925b8d43f50e63d7307-000000000011eca1-00059d49fd720acb.journal
    10 -> /var/log/journal/56161deef0f24959be056aa81810ce4f/user-1001.journal
    11 -> /var/log/journal/56161deef0f24959be056aa81810ce4f/system@3d354d8051214925b8d43f50e63d7307-000000000011eca2-00059d49fd728a75.journal
    12 -> /var/log/journal/56161deef0f24959be056aa81810ce4f/user-1001@cbd42769b3e547cba69261355d9ba2f8-000000000011ec84-00059d49c900efb9.journal
    13 -> /var/log/journal/56161deef0f24959be056aa81810ce4f/user-1000.journal
Delete some journal files
Process journal events
List fds
    0 -> /dev/pts/0
    1 -> /dev/pts/0
    2 -> /dev/pts/0
    3 -> /dev/null
    6 -> /var/log/journal/56161deef0f24959be056aa81810ce4f/system@3d354d8051214925b8d43f50e63d7307-000000000011eb29-00059d4928d6c3d6.journal (deleted)
    7 -> /var/log/journal/56161deef0f24959be056aa81810ce4f/system.journal
    8 -> /var/log/journal/56161deef0f24959be056aa81810ce4f/user-1000@aaabf79080ca4edcafc88708e607b702-000000000011ebb7-00059d497ac0ed84.journal (deleted)
    9 -> /var/log/journal/56161deef0f24959be056aa81810ce4f/system@3d354d8051214925b8d43f50e63d7307-000000000011eca1-00059d49fd720acb.journal (deleted)
    10 -> /var/log/journal/56161deef0f24959be056aa81810ce4f/user-1001.journal
    11 -> /var/log/journal/56161deef0f24959be056aa81810ce4f/system@3d354d8051214925b8d43f50e63d7307-000000000011eca2-00059d49fd728a75.journal (deleted)
    12 -> /var/log/journal/56161deef0f24959be056aa81810ce4f/user-1001@cbd42769b3e547cba69261355d9ba2f8-000000000011ec84-00059d49c900efb9.journal (deleted)
    13 -> /var/log/journal/56161deef0f24959be056aa81810ce4f/user-1000.journal
@msekletar msekletar added bug 🐛 Programming errors, that need preferential fixing journal labels Jan 29, 2020
msekletar added a commit to msekletar/systemd that referenced this issue Feb 4, 2020
msekletar added a commit to msekletar/systemd that referenced this issue Feb 5, 2020
keszybz pushed a commit that referenced this issue Feb 5, 2020
msekletar added a commit to msekletar/systemd that referenced this issue Mar 27, 2020
journal_file_fstat() returns an error if we call it on already unlinked
journal file and hence we never reach remove_file_real() which is the
entire point.

I must have made some mistake while testing the fix that got me thinking
the issue is gone while opposite was true.

Fixes systemd#14695
keszybz pushed a commit that referenced this issue Mar 28, 2020
journal_file_fstat() returns an error if we call it on already unlinked
journal file and hence we never reach remove_file_real() which is the
entire point.

I must have made some mistake while testing the fix that got me thinking
the issue is gone while opposite was true.

Fixes #14695
DaanDeMeyer pushed a commit to DaanDeMeyer/systemd that referenced this issue Mar 30, 2020
journal_file_fstat() returns an error if we call it on already unlinked
journal file and hence we never reach remove_file_real() which is the
entire point.

I must have made some mistake while testing the fix that got me thinking
the issue is gone while opposite was true.

Fixes systemd#14695
eworm-de pushed a commit to eworm-de/systemd that referenced this issue Jun 23, 2020
journal_file_fstat() returns an error if we call it on already unlinked
journal file and hence we never reach remove_file_real() which is the
entire point.

I must have made some mistake while testing the fix that got me thinking
the issue is gone while opposite was true.

Fixes systemd#14695

(cherry picked from commit 8581b9f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Programming errors, that need preferential fixing journal
1 participant