Skip to content

Commit

Permalink
Fixes #7406 UnicodeDecodeError in on_tracker_error_alert
Browse files Browse the repository at this point in the history
  • Loading branch information
kozlovsky committed Jun 9, 2023
1 parent 187a565 commit 7c03858
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}')
Expand Down
9 changes: 9 additions & 0 deletions src/tribler/core/components/libtorrent/tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import libtorrent as lt
import pytest
from _pytest.logging import LogCaptureFixture
from ipv8.util import succeed
from libtorrent import bencode

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7c03858

Please sign in to comment.