From 1cabae05839a7e74b6ee6c713ca15093c5ced9c3 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Thu, 2 Jun 2016 21:23:11 +0200 Subject: [PATCH] mhtml: don't crash when user cancels a download Fixes 1535 The browser crashed because both callbacks were called (finished and error), trying to remove the item twice from the list of downloads. --- qutebrowser/browser/mhtml.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/qutebrowser/browser/mhtml.py b/qutebrowser/browser/mhtml.py index 24cc828f3..71e1f6122 100644 --- a/qutebrowser/browser/mhtml.py +++ b/qutebrowser/browser/mhtml.py @@ -345,7 +345,7 @@ class _Downloader: self.pending_downloads.add((url, item)) item.finished.connect(functools.partial(self._finished, url, item)) item.error.connect(functools.partial(self._error, url, item)) - item.cancelled.connect(functools.partial(self._error, url, item)) + item.cancelled.connect(functools.partial(self._cancelled, url, item)) def _finished(self, url, item): """Callback when a single asset is downloaded. @@ -418,6 +418,20 @@ class _Downloader: return self._finish_file() + def _cancelled(self, url, item): + """Callback when a download is cancelled by the user. + + Args: + url: The original url of the asset as QUrl. + item: The DownloadItem given by the DownloadManager. + """ + # This callback is called before _finished, so there's no need to + # remove the item or close the fileobject. + log.downloads.debug("MHTML download cancelled by used: {}".format(url)) + # Write an empty file instead + item.fileobj.seek(0) + item.fileobj.truncate() + def _finish_file(self): """Save the file to the filename given in __init__.""" if self._finished_file: