Skip to content

Commit 6ca0150

Browse files
committed
Serialize host syslog mask changes
Extract the syslog lock and whole canonical free-threading test from #9439. Regenerate the test with scripts/update_lib from CPython d220409a13b77de7a6c30abe16ea90db01528edc. Assisted-by: OpenAI Codex: model version unavailable
1 parent 84d7afc commit 6ca0150

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import unittest
2+
import threading
3+
4+
from test.support import import_helper, threading_helper
5+
from test.support.threading_helper import run_concurrently
6+
7+
syslog = import_helper.import_module("syslog")
8+
9+
NTHREADS = 32
10+
11+
# Similar to Lib/test/test_syslog.py, this test's purpose is to verify that
12+
# the code neither crashes nor leaks.
13+
14+
15+
@threading_helper.requires_working_threading()
16+
class TestSyslog(unittest.TestCase):
17+
def test_racing_syslog(self):
18+
def worker():
19+
"""
20+
The syslog module provides the following functions:
21+
openlog(), syslog(), closelog(), and setlogmask().
22+
"""
23+
thread_id = threading.get_ident()
24+
syslog.openlog(f"thread-id: {thread_id}")
25+
try:
26+
for _ in range(5):
27+
syslog.syslog("logline")
28+
syslog.setlogmask(syslog.LOG_MASK(syslog.LOG_INFO))
29+
syslog.syslog(syslog.LOG_INFO, "logline LOG_INFO")
30+
syslog.setlogmask(syslog.LOG_MASK(syslog.LOG_ERR))
31+
syslog.syslog(syslog.LOG_ERR, "logline LOG_ERR")
32+
syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG))
33+
finally:
34+
syslog.closelog()
35+
36+
# Run the worker concurrently to exercise all these syslog functions
37+
run_concurrently(
38+
worker_func=worker,
39+
nthreads=NTHREADS,
40+
)
41+
42+
43+
if __name__ == "__main__":
44+
unittest.main()

‎crates/host_env/src/syslog.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ pub fn closelog() {
6767

6868
#[must_use]
6969
pub fn setlogmask(maskpri: i32) -> i32 {
70+
// libc setlogmask() is not thread-safe.
71+
static SETLOGMASK: parking_lot::Mutex<()> = parking_lot::Mutex::new(());
72+
let _guard = SETLOGMASK.lock();
7073
unsafe { libc::setlogmask(maskpri) }
7174
}
7275

0 commit comments

Comments
 (0)