Skip to content

Commit

Permalink
fritzcollectd: Recover from XMLSyntaxError in read callback
Browse files Browse the repository at this point in the history
In rare cases reading from the FRITZ!Box can trigger XMLSyntaxError
exceptions (for example: 'Opening and ending tag mismatch: HR line 1
and BODY, line 1, column 180 (line 1)'. If this happens the connection
seems to be unusable and the error would happen also on subsequent
calls. Reinitialize the connection if this happens.

This has been reported for a FRITZ!Box 6490 (6.52) when the device is
remotely reset by Unitymedia.

See #12.
  • Loading branch information
fetzerch committed Mar 25, 2018
1 parent 130f7d5 commit f81a489
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
9 changes: 7 additions & 2 deletions fritzcollectd.py
Expand Up @@ -29,7 +29,7 @@

import collectd # pylint: disable=import-error

__version__ = '0.5.0'
__version__ = '0.5.1'


CONFIGS = []
Expand Down Expand Up @@ -261,7 +261,12 @@ def callback_init():
def callback_read():
""" Read callback """
for config in CONFIGS:
config.read()
try:
config.read()
except XMLSyntaxError:
collectd.warning('fritzcollectd: Invalid data received, '
'attempting to reconnect')
config.init()


def callback_shutdown():
Expand Down
10 changes: 10 additions & 0 deletions test_fritzcollectd.py
Expand Up @@ -281,6 +281,16 @@ def test_incorrect_password(fc_class_mock):
MOCK.process(CollectdConfig({'Password': 'incorrect'}))


@mock.patch('fritzconnection.FritzConnection', autospec=True)
@with_setup(teardown=MOCK.reset_mock)
def test_xmlsyntaxerror_in_read(fc_class_mock):
""" Simulate an XMLSyntaxError exception when reading data. """
fc_mock = FritzConnectionMock()
fc_class_mock.return_value = fc_mock
fc_mock.call_action.side_effect = [{0}, XMLSyntaxError(0, 0, 0, 0), {0}]
MOCK.process()


# System tests that try to interact with a real hardware device.

@nottest
Expand Down

0 comments on commit f81a489

Please sign in to comment.