mirror of
https://github.com/python/cpython
synced 2026-11-06 09:02:40 +03:00
syslog_get_argv() took sys.argv[0] with PyList_GetItem() and increfed it
only afterwards, so on the free-threaded build a concurrent replacement of
sys.argv[0] could free it in between. Use PyList_GetItemRef() instead.
(cherry picked from commit e5e0748abd)
Co-authored-by: Junhyung Lee <70549809+espressolee@users.noreply.github.com>
Co-authored-by: Lazizbek Ergashev <20501725+lazerg@users.noreply.github.com>
67 lines
2.1 KiB
Python
67 lines
2.1 KiB
Python
import sys
|
|
import unittest
|
|
import threading
|
|
|
|
from test import support
|
|
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,
|
|
)
|
|
|
|
def test_racing_openlog_argv(self):
|
|
# gh-159864: openlog() without an ident reads sys.argv[0] while
|
|
# another thread replaces it.
|
|
argv = ["prog"]
|
|
|
|
def opener():
|
|
for _ in range(1000):
|
|
syslog.openlog()
|
|
|
|
def writer():
|
|
for i in range(1000):
|
|
argv[0] = f"prog-{i}"
|
|
|
|
with support.swap_attr(sys, "argv", argv):
|
|
try:
|
|
run_concurrently(
|
|
worker_func=[opener] * (NTHREADS - 1) + [writer],
|
|
)
|
|
finally:
|
|
syslog.closelog()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|