Skip to content

Commit 994ed07

Browse files
author
Sahid Orentino Ferdjaoui
committed
common/amqp: fix close() when kombu not initialized
During clean when error happen it may happen that we call close() where kombu has not yet been initialized from method 'establish()'. To avoid multiple checks in base-code, the method close() is now doing no-op is establish has no been initialized. Closes-Jira-Bug: CEM-19030 Signed-off-by: Sahid Orentino Ferdjaoui <sferdjaoui@juniper.net> Change-Id: Ic259e47ab93c8c5837d6ebd5f7089d0bde3f5da2
1 parent e78b916 commit 994ed07

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
3+
#
4+
# Copyright (c) 2020 Juniper Networks, Inc. All rights reserved.
5+
#
6+
7+
import unittest
8+
import mock
9+
10+
from cfgm_common.vnc_amqp import VncAmqpHandle
11+
12+
13+
class TestVncAmqp(unittest.TestCase):
14+
15+
def test_close(self):
16+
vnc = VncAmqpHandle(
17+
*(7 * [mock.MagicMock()]))
18+
19+
# Should not raise anything
20+
vnc.close()
21+
22+
# Pretends call of establish()
23+
vnc._vnc_kombu = mock.MagicMock()
24+
vnc.close()
25+
vnc._vnc_kombu.shutdown.assert_called_once_with()

src/config/common/cfgm_common/vnc_amqp.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def __init__(self, sandesh, logger, db_cls, reaction_map, q_name_prefix,
3232
self.timer = timer_obj
3333
self.host_ip = host_ip
3434
self.register_handler = register_handler
35+
self._vnc_kombu = None
3536

3637
def establish(self):
3738
q_name = '.'.join([self.q_name_prefix, socket.getfqdn(self.host_ip)])
@@ -262,4 +263,9 @@ def evaluate_dependency(self):
262263
self.timer.timed_yield()
263264

264265
def close(self):
265-
self._vnc_kombu.shutdown()
266+
if self._vnc_kombu is not None:
267+
# VncKombuClient is instancied when calling 'establish()',
268+
# if for some reasons (mostly related to cleanup after
269+
# exception handling) we call `close()` we should consider
270+
# a no-op.
271+
self._vnc_kombu.shutdown()

0 commit comments

Comments
 (0)