Skip to content

Commit

Permalink
Fix checkpoints getting stuck
Browse files Browse the repository at this point in the history
  • Loading branch information
egbertbouman authored and ichorid committed Feb 28, 2020
1 parent c00cfac commit 7008530
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import logging
import sys
import time
from asyncio import CancelledError, Future, iscoroutine, sleep
from asyncio import CancelledError, Future, TimeoutError, iscoroutine, sleep, wait_for
from collections import defaultdict

from ipv8.taskmanager import TaskManager, task
Expand Down Expand Up @@ -662,7 +662,7 @@ def get_state(self):
return DownloadState(self, self.lt_status, self.error, vod)

@task
async def save_resume_data(self):
async def save_resume_data(self, timeout=10):
"""
Save the resume data of a download. This method returns when the resume data is available.
Note that this method only calls save_resume_data once on subsequent calls.
Expand All @@ -672,9 +672,9 @@ async def save_resume_data(self):
handle.save_resume_data()

try:
await self.wait_for_alert('save_resume_data_alert', None,
'save_resume_data_failed_alert',
lambda a: SaveResumeDataError(a.error.message()))
await wait_for(self.wait_for_alert('save_resume_data_alert', None,
'save_resume_data_failed_alert',
lambda a: SaveResumeDataError(a.error.message())), timeout=timeout)
except (CancelledError, SaveResumeDataError, TimeoutError) as e:
self._logger.error("Resume data failed to save: %s", e)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,20 +597,13 @@ def test_on_state_changed(self):
self.libtorrent_download_impl.on_state_changed_alert(type('state_changed_alert', (object,), dict(state=5)))
self.libtorrent_download_impl.apply_ip_filter.assert_called_with(False)

# TODO: re-enable this test for asyncio
#self.libtorrent_download_impl.deferreds_resume.append(Deferred().addErrback(
# self.libtorrent_download_impl._on_resume_err).addCallback(on_error))
#self.libtorrent_download_impl.on_save_resume_data_failed_alert(mock_alert)

@skipIf(True, reason="Needs to be converted to asyncio")
async def test_checkpoint_timeout(self):
"""
Testing whether making a checkpoint times out when we receive no alert from libtorrent
"""
self.libtorrent_download_impl._on_resume_err = Mock()
self.libtorrent_download_impl.deferreds_resume = [Deferred()]
self.libtorrent_download_impl.save_resume_data(timeout=.01)
self.libtorrent_download_impl.deferreds_resume.pop(0)
self.libtorrent_download_impl.futures['save_resume_data'] = [Future()]
task = self.libtorrent_download_impl.save_resume_data(timeout=.01)
self.libtorrent_download_impl.futures['save_resume_data'].pop(0)
await sleep(0.2)
args, _ = self.libtorrent_download_impl._on_resume_err.call_args
self.assertEqual(args[0].type, CancelledError)
self.assertTrue(task.done())

0 comments on commit 7008530

Please sign in to comment.