Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0abf61d

Browse files
committed
logging: Added locking in flush() and close() handler methods. Thanks to Fayaz Yusuf Khan for the suggestion.
1 parent ba528f5 commit 0abf61d

2 files changed

Lines changed: 35 additions & 29 deletions

File tree

Lib/logging/__init__.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2001-2010 by Vinay Sajip. All Rights Reserved.
1+
# Copyright 2001-2012 by Vinay Sajip. All Rights Reserved.
22
#
33
# Permission to use, copy, modify, and distribute this software and its
44
# documentation for any purpose and without fee is hereby granted,
@@ -16,9 +16,9 @@
1616

1717
"""
1818
Logging package for Python. Based on PEP 282 and comments thereto in
19-
comp.lang.python, and influenced by Apache's log4j system.
19+
comp.lang.python.
2020
21-
Copyright (C) 2001-2011 Vinay Sajip. All Rights Reserved.
21+
Copyright (C) 2001-2012 Vinay Sajip. All Rights Reserved.
2222
2323
To use, simply 'import logging' and log away!
2424
"""
@@ -917,8 +917,9 @@ def flush(self):
917917
"""
918918
Flushes the stream.
919919
"""
920-
if self.stream and hasattr(self.stream, "flush"):
921-
self.stream.flush()
920+
with self.lock:
921+
if self.stream and hasattr(self.stream, "flush"):
922+
self.stream.flush()
922923

923924
def emit(self, record):
924925
"""
@@ -969,12 +970,13 @@ def close(self):
969970
"""
970971
Closes the stream.
971972
"""
972-
if self.stream:
973-
self.flush()
974-
if hasattr(self.stream, "close"):
975-
self.stream.close()
976-
StreamHandler.close(self)
977-
self.stream = None
973+
with self.lock:
974+
if self.stream:
975+
self.flush()
976+
if hasattr(self.stream, "close"):
977+
self.stream.close()
978+
StreamHandler.close(self)
979+
self.stream = None
978980

979981
def _open(self):
980982
"""

Lib/logging/handlers.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2001-2010 by Vinay Sajip. All Rights Reserved.
1+
# Copyright 2001-2012 by Vinay Sajip. All Rights Reserved.
22
#
33
# Permission to use, copy, modify, and distribute this software and its
44
# documentation for any purpose and without fee is hereby granted,
@@ -16,10 +16,9 @@
1616

1717
"""
1818
Additional handlers for the logging package for Python. The core package is
19-
based on PEP 282 and comments thereto in comp.lang.python, and influenced by
20-
Apache's log4j system.
19+
based on PEP 282 and comments thereto in comp.lang.python.
2120
22-
Copyright (C) 2001-2010 Vinay Sajip. All Rights Reserved.
21+
Copyright (C) 2001-2012 Vinay Sajip. All Rights Reserved.
2322
2423
To use, simply 'import logging.handlers' and log away!
2524
"""
@@ -554,10 +553,11 @@ def close(self):
554553
"""
555554
Closes the socket.
556555
"""
557-
if self.sock:
558-
self.sock.close()
559-
self.sock = None
560-
logging.Handler.close(self)
556+
with self.lock:
557+
if self.sock:
558+
self.sock.close()
559+
self.sock = None
560+
logging.Handler.close(self)
561561

562562
class DatagramHandler(SocketHandler):
563563
"""
@@ -752,9 +752,10 @@ def close (self):
752752
"""
753753
Closes the socket.
754754
"""
755-
if self.unixsocket:
756-
self.socket.close()
757-
logging.Handler.close(self)
755+
with self.lock:
756+
if self.unixsocket:
757+
self.socket.close()
758+
logging.Handler.close(self)
758759

759760
def mapPriority(self, levelName):
760761
"""
@@ -1095,7 +1096,8 @@ def flush(self):
10951096
10961097
This version just zaps the buffer to empty.
10971098
"""
1098-
self.buffer = []
1099+
with self.lock:
1100+
self.buffer = []
10991101

11001102
def close(self):
11011103
"""
@@ -1145,18 +1147,20 @@ def flush(self):
11451147
11461148
The record buffer is also cleared by this operation.
11471149
"""
1148-
if self.target:
1149-
for record in self.buffer:
1150-
self.target.handle(record)
1151-
self.buffer = []
1150+
with self.lock:
1151+
if self.target:
1152+
for record in self.buffer:
1153+
self.target.handle(record)
1154+
self.buffer = []
11521155

11531156
def close(self):
11541157
"""
11551158
Flush, set the target to None and lose the buffer.
11561159
"""
11571160
self.flush()
1158-
self.target = None
1159-
BufferingHandler.close(self)
1161+
with self.lock:
1162+
self.target = None
1163+
BufferingHandler.close(self)
11601164

11611165

11621166
class QueueHandler(logging.Handler):

0 commit comments

Comments
 (0)