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

Adjust logging lock handling for Python 3.13 #15

Merged
merged 3 commits into from
Jan 22, 2025
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
27 changes: 10 additions & 17 deletions paste/script/util/logging_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,13 @@ def fileConfig(fname, defaults=None):
formatters = _create_formatters(cp)

# critical section
logging._acquireLock()
try:
with logging._lock:
logging._handlers.clear()
if hasattr(logging, '_handlerList'):
del logging._handlerList[:]
# Handlers add themselves to logging._handlers
handlers = _install_handlers(cp, formatters)
_install_loggers(cp, handlers)
finally:
logging._releaseLock()


def _resolve(name):
Expand Down Expand Up @@ -327,9 +324,8 @@ class ConfigSocketReceiver(ThreadingTCPServer):
def __init__(self, host='localhost', port=DEFAULT_LOGGING_CONFIG_PORT,
handler=None):
ThreadingTCPServer.__init__(self, (host, port), handler)
logging._acquireLock()
self.abort = 0
logging._releaseLock()
with logging._lock:
self.abort = 0
self.timeout = 1

def serve_until_stopped(self):
Expand All @@ -341,16 +337,14 @@ def serve_until_stopped(self):
self.timeout)
if rd:
self.handle_request()
logging._acquireLock()
abort = self.abort
logging._releaseLock()
with logging._lock:
abort = self.abort

def serve(rcvr, hdlr, port):
server = rcvr(port=port, handler=hdlr)
global _listener
logging._acquireLock()
_listener = server
logging._releaseLock()
with logging._lock:
_listener = server
server.serve_until_stopped()

return threading.Thread(target=serve,
Expand All @@ -363,7 +357,6 @@ def stopListening():
"""
global _listener
if _listener:
logging._acquireLock()
_listener.abort = 1
_listener = None
logging._releaseLock()
with logging._lock:
_listener.abort = 1
_listener = None
20 changes: 4 additions & 16 deletions tests/test_logging_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,11 @@ def test4():
conf = globals()['config%d' % i]
sys.stdout.write('config%d: ' % i)
loggerDict = logging.getLogger().manager.loggerDict
logging._acquireLock()
try:
with logging._lock:
saved_handlers = logging._handlers.copy()
if hasattr(logging, '_handlerList'):
saved_handler_list = logging._handlerList[:]
saved_loggers = loggerDict.copy()
finally:
logging._releaseLock()
try:
fn = tempfile.mktemp(".ini")
f = open(fn, "w")
Expand All @@ -146,17 +143,14 @@ def test4():
message('ok.')
os.remove(fn)
finally:
logging._acquireLock()
try:
with logging._lock:
logging._handlers.clear()
logging._handlers.update(saved_handlers)
if hasattr(logging, '_handlerList'):
logging._handlerList[:] = saved_handler_list
loggerDict = logging.getLogger().manager.loggerDict
loggerDict.clear()
loggerDict.update(saved_loggers)
finally:
logging._releaseLock()

#----------------------------------------------------------------------------
# Test 5
Expand Down Expand Up @@ -196,14 +190,11 @@ def formatException(self, ei):

def test5():
loggerDict = logging.getLogger().manager.loggerDict
logging._acquireLock()
try:
with logging._lock:
saved_handlers = logging._handlers.copy()
if hasattr(logging, '_handlerList'):
saved_handler_list = logging._handlerList[:]
saved_loggers = loggerDict.copy()
finally:
logging._releaseLock()
try:
fn = tempfile.mktemp(".ini")
f = open(fn, "w")
Expand All @@ -218,14 +209,11 @@ def test5():
hdlr = logging.getLogger().handlers[0]
logging.getLogger().handlers.remove(hdlr)
finally:
logging._acquireLock()
try:
with logging._lock:
logging._handlers.clear()
logging._handlers.update(saved_handlers)
if hasattr(logging, '_handlerList'):
logging._handlerList[:] = saved_handler_list
loggerDict = logging.getLogger().manager.loggerDict
loggerDict.clear()
loggerDict.update(saved_loggers)
finally:
logging._releaseLock()
Loading