Skip to content

Commit 7d9c685

Browse files
authored
gh-120868: Fix breaking change in logging.config when using QueueHandler (GH-120872)
1 parent 4be1f37 commit 7d9c685

File tree

3 files changed

+81
-17
lines changed

3 files changed

+81
-17
lines changed

Lib/logging/config.py

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -780,25 +780,44 @@ def configure_handler(self, config):
780780
# if 'handlers' not in config:
781781
# raise ValueError('No handlers specified for a QueueHandler')
782782
if 'queue' in config:
783-
from multiprocessing.queues import Queue as MPQueue
784-
from multiprocessing import Manager as MM
785-
proxy_queue = MM().Queue()
786-
proxy_joinable_queue = MM().JoinableQueue()
787783
qspec = config['queue']
788-
if not isinstance(qspec, (queue.Queue, MPQueue,
789-
type(proxy_queue), type(proxy_joinable_queue))):
790-
if isinstance(qspec, str):
791-
q = self.resolve(qspec)
792-
if not callable(q):
793-
raise TypeError('Invalid queue specifier %r' % qspec)
794-
q = q()
795-
elif isinstance(qspec, dict):
796-
if '()' not in qspec:
797-
raise TypeError('Invalid queue specifier %r' % qspec)
798-
q = self.configure_custom(dict(qspec))
799-
else:
784+
785+
if isinstance(qspec, str):
786+
q = self.resolve(qspec)
787+
if not callable(q):
800788
raise TypeError('Invalid queue specifier %r' % qspec)
801-
config['queue'] = q
789+
config['queue'] = q()
790+
elif isinstance(qspec, dict):
791+
if '()' not in qspec:
792+
raise TypeError('Invalid queue specifier %r' % qspec)
793+
config['queue'] = self.configure_custom(dict(qspec))
794+
else:
795+
from multiprocessing.queues import Queue as MPQueue
796+
797+
if not isinstance(qspec, (queue.Queue, MPQueue)):
798+
# Safely check if 'qspec' is an instance of Manager.Queue
799+
# / Manager.JoinableQueue
800+
801+
from multiprocessing import Manager as MM
802+
from multiprocessing.managers import BaseProxy
803+
804+
# if it's not an instance of BaseProxy, it also can't be
805+
# an instance of Manager.Queue / Manager.JoinableQueue
806+
if isinstance(qspec, BaseProxy):
807+
# Sometimes manager or queue creation might fail
808+
# (e.g. see issue gh-120868). In that case, any
809+
# exception during the creation of these queues will
810+
# propagate up to the caller and be wrapped in a
811+
# `ValueError`, whose cause will indicate the details of
812+
# the failure.
813+
mm = MM()
814+
proxy_queue = mm.Queue()
815+
proxy_joinable_queue = mm.JoinableQueue()
816+
if not isinstance(qspec, (type(proxy_queue), type(proxy_joinable_queue))):
817+
raise TypeError('Invalid queue specifier %r' % qspec)
818+
else:
819+
raise TypeError('Invalid queue specifier %r' % qspec)
820+
802821
if 'listener' in config:
803822
lspec = config['listener']
804823
if isinstance(lspec, type):

Lib/test/test_logging.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3928,6 +3928,50 @@ def test_config_queue_handler(self):
39283928
msg = str(ctx.exception)
39293929
self.assertEqual(msg, "Unable to configure handler 'ah'")
39303930

3931+
@threading_helper.requires_working_threading()
3932+
@support.requires_subprocess()
3933+
@patch("multiprocessing.Manager")
3934+
def test_config_queue_handler_does_not_create_multiprocessing_manager(self, manager):
3935+
# gh-120868
3936+
3937+
from multiprocessing import Queue as MQ
3938+
3939+
q1 = {"()": "queue.Queue", "maxsize": -1}
3940+
q2 = MQ()
3941+
q3 = queue.Queue()
3942+
3943+
for qspec in (q1, q2, q3):
3944+
self.apply_config(
3945+
{
3946+
"version": 1,
3947+
"handlers": {
3948+
"queue_listener": {
3949+
"class": "logging.handlers.QueueHandler",
3950+
"queue": qspec,
3951+
},
3952+
},
3953+
}
3954+
)
3955+
manager.assert_not_called()
3956+
3957+
@patch("multiprocessing.Manager")
3958+
def test_config_queue_handler_invalid_config_does_not_create_multiprocessing_manager(self, manager):
3959+
# gh-120868
3960+
3961+
with self.assertRaises(ValueError):
3962+
self.apply_config(
3963+
{
3964+
"version": 1,
3965+
"handlers": {
3966+
"queue_listener": {
3967+
"class": "logging.handlers.QueueHandler",
3968+
"queue": object(),
3969+
},
3970+
},
3971+
}
3972+
)
3973+
manager.assert_not_called()
3974+
39313975
@support.requires_subprocess()
39323976
def test_multiprocessing_queues(self):
39333977
# See gh-119819

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,7 @@ Hrvoje Nikšić
13181318
Gregory Nofi
13191319
Jesse Noller
13201320
Bill Noon
1321+
Janek Nouvertné
13211322
Stefan Norberg
13221323
Tim Northover
13231324
Joe Norton

0 commit comments

Comments
 (0)