Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Lib/test/test_free_threading/test_syslog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import unittest
import threading

from test.support import import_helper, threading_helper
from test.support.threading_helper import run_concurrently

syslog = import_helper.import_module("syslog")

NTHREADS = 32

# Similar to Lib/test/test_syslog.py, this test's purpose is to verify that
# the code neither crashes nor leaks.


@threading_helper.requires_working_threading()
class TestSyslog(unittest.TestCase):
def test_racing_syslog(self):
def worker():
"""
The syslog module provides the following functions:
openlog(), syslog(), closelog(), and setlogmask().
"""
thread_id = threading.get_ident()
syslog.openlog(f"thread-id: {thread_id}")
try:
for _ in range(5):
syslog.syslog("logline")
syslog.setlogmask(syslog.LOG_MASK(syslog.LOG_INFO))
syslog.syslog(syslog.LOG_INFO, "logline LOG_INFO")
syslog.setlogmask(syslog.LOG_MASK(syslog.LOG_ERR))
syslog.syslog(syslog.LOG_ERR, "logline LOG_ERR")
syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG))
finally:
syslog.closelog()

# Run the worker concurrently to exercise all these syslog functions
run_concurrently(
worker_func=worker,
nthreads=NTHREADS,
)


if __name__ == "__main__":
unittest.main()
3 changes: 3 additions & 0 deletions crates/host_env/src/syslog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ pub fn closelog() {

#[must_use]
pub fn setlogmask(maskpri: i32) -> i32 {
// libc setlogmask() is not thread-safe.
static SETLOGMASK: parking_lot::Mutex<()> = parking_lot::Mutex::new(());
let _guard = SETLOGMASK.lock();
unsafe { libc::setlogmask(maskpri) }
}

Expand Down
Loading