diff --git a/src/tribler/core/components/libtorrent/download_manager/download.py b/src/tribler/core/components/libtorrent/download_manager/download.py index 90d376aba52..e325d1fea38 100644 --- a/src/tribler/core/components/libtorrent/download_manager/download.py +++ b/src/tribler/core/components/libtorrent/download_manager/download.py @@ -312,12 +312,12 @@ def on_tracker_reply_alert(self, alert: lt.tracker_reply_alert): self.tracker_status[alert.url] = [alert.num_peers, 'Working'] def on_tracker_error_alert(self, alert: lt.tracker_error_alert): - self._logger.error(f'On tracker error alert: {alert}') - - # try-except block here is a workaround and has been added to solve - # the issue: "UnicodeDecodeError: invalid continuation byte" + # The try-except block is added as a workaround to suppress UnicodeDecodeError in `repr(alert)`, + # `alert.url` and `alert.msg`. See https://github.com/arvidn/libtorrent/issues/143 try: + self._logger.error(f'On tracker error alert: {alert}') url = alert.url + peers = self.tracker_status[url][0] if url in self.tracker_status else 0 if alert.msg: status = 'Error: ' + alert.msg @@ -330,8 +330,7 @@ def on_tracker_error_alert(self, alert: lt.tracker_error_alert): self.tracker_status[url] = [peers, status] except UnicodeDecodeError as e: - self._logger.exception(e) - return + self._logger.warning(f'UnicodeDecodeError in on_tracker_error_alert: {e}') def on_tracker_warning_alert(self, alert: lt.tracker_warning_alert): self._logger.warning(f'On tracker warning alert: {alert}') diff --git a/src/tribler/core/components/libtorrent/tests/test_download.py b/src/tribler/core/components/libtorrent/tests/test_download.py index 5e48413dd6d..5f782b5039c 100644 --- a/src/tribler/core/components/libtorrent/tests/test_download.py +++ b/src/tribler/core/components/libtorrent/tests/test_download.py @@ -4,6 +4,7 @@ import libtorrent as lt import pytest +from _pytest.logging import LogCaptureFixture from ipv8.util import succeed from libtorrent import bencode @@ -238,6 +239,14 @@ def test_process_error_alert(test_download): assert test_download.tracker_status[url][1] == 'Timeout' +def test_tracker_error_alert_unicode_decode_error(test_download: Download, caplog: LogCaptureFixture): + exception = UnicodeDecodeError('utf-8', b'\xc3\x28', 0, 1, 'invalid continuation byte') + mock_alert = MagicMock(__repr__=Mock(side_effect=exception)) + test_download.process_alert(mock_alert, 'tracker_error_alert') + assert caplog.messages == ["UnicodeDecodeError in on_tracker_error_alert: " + "'utf-8' codec can't decode byte 0xc3 in position 0: invalid continuation byte"] + + def test_tracker_warning_alert(test_download): """ Test whether a tracking warning alert is processed correctly