Skip to content

Commit

Permalink
Fix: 'key not found' exception in bgp4.py (sonic-net#192)
Browse files Browse the repository at this point in the history
**- What I did**
A 'key not found' exception will be raised in bgp4.py if the state for a given neighbor is not found in STATE_DB. 
```
ERR snmp#snmp-subagent [ax_interface] ERROR: MIBUpdater.start() caught an unexpected exception during update_data()
#012Traceback (most recent call last):
#12  File "/usr/local/lib/python3.7/dist-packages/ax_interface/mib.py", line 43, in start
#12    self.update_data()#12  File "/usr/local/lib/python3.7/dist-packages/sonic_ax_impl/mibs/vendor/cisco/bgp4.py", line 42, in update_data
#12    state = neigh_info['state']
#12  File "/usr/lib/python3/dist-packages/swsscommon/swsscommon.py", line 345, in __getitem__
#12    return _swsscommon.FieldValueMap___getitem__(self, key)
#012IndexError: key not found
```
It is becaues an empty ```dict``` is returned by ```get_all``` when nothing is found for the given key. So check for ```None``` can't detect the error.

**- How I did it**
This commit addressed the issue by checking the key ```state```.

**- How to verify it**
Verified on A7260. No exception is observed after the update.

**- Description for the changelog**
This PR fix exception caused by non existing key.
  • Loading branch information
bingwang-ms authored Jan 28, 2021
1 parent 59e2a1c commit ad302d4
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/sonic_ax_impl/mibs/vendor/cisco/bgp4.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import socket
from bisect import bisect_right
from sonic_ax_impl import mibs
from ax_interface import MIBMeta, ValueType, MIBUpdater, SubtreeMIBEntry
from ax_interface.mib import MIBEntry
from sonic_ax_impl.mibs import Namespace
import ipaddress

Expand All @@ -22,7 +20,7 @@ def __init__(self):
super().__init__()
self.db_conn = Namespace.init_namespace_dbs()

self.neigh_state_map = {}
self.neigh_state_map = {}
self.session_status_map = {}
self.session_status_list = []

Expand All @@ -38,7 +36,7 @@ def update_data(self):
neigh_str = neigh_key
neigh_str = neigh_str.split('|')[1]
neigh_info = self.db_conn[db_index].get_all(mibs.STATE_DB, neigh_key, blocking=False)
if neigh_info is not None:
if neigh_info:
state = neigh_info['state']
ip = ipaddress.ip_address(neigh_str)
if type(ip) is ipaddress.IPv4Address:
Expand Down

0 comments on commit ad302d4

Please sign in to comment.