~ubuntuone-control-tower/software-center/trunk

« back to all changes in this revision

Viewing changes to softwarecenter/utils.py

* lp:~mvo/software-center/downloader-fix-race839462-again:
  - fix a race condition in the SimpleFileDownloader when the file
    is downloaded but the signal did not get delivered yet by the
    gtk event loop (LP: #1055441)

Show diffs side-by-side

added added

removed removed

Lines of Context:
921
921
        f.query_info_async(Gio.FILE_ATTRIBUTE_STANDARD_SIZE, 0, 0,
922
922
                           self._cancellable,
923
923
                           self._check_url_reachable_and_then_download_cb,
924
 
                           None)
925
 
 
926
 
    def _check_url_reachable_and_then_download_cb(self, f, result,
927
 
                                                  user_data=None):
 
924
                           url)
 
925
 
 
926
    def _ensure_correct_url(self, want_url):
 
927
        """This function will ensure that the url we requested to download
 
928
           earlier matches that is now downloaded.
 
929
        """
 
930
        # this function is needed as there is a rance condition when the
 
931
        # operation is finished but the signal is not delivered yet (its
 
932
        # still in the gtk event loop). in this case there is nothing to
 
933
        # self._cancel but self.url/self.dest_file_path will still point to
 
934
        # the wrong file
 
935
        if self.url != want_url:
 
936
            self.LOG.warn("url changed from '%s' to '%s'" % (
 
937
                    want_url, self.url))
 
938
            return False
 
939
        return True
 
940
 
 
941
    def _check_url_reachable_and_then_download_cb(self, f, result, want_url):
928
942
        self.LOG.debug("_check_url_reachable_and_then_download_cb: %s" % f)
 
943
        if not self._ensure_correct_url(want_url):
 
944
            return
 
945
        # normal operation
929
946
        try:
930
947
            info = f.query_info_finish(result)
931
948
            etag = info.get_etag()
935
952
                                                        etag))
936
953
            # url is reachable, now download the file
937
954
            f.load_contents_async(
938
 
                self._cancellable, self._file_download_complete_cb, None)
 
955
                self._cancellable, self._file_download_complete_cb, want_url)
939
956
        except GObject.GError as e:
940
957
            self.LOG.debug("file *not* reachable %s" % self.url)
941
958
            self.emit('file-url-reachable', False)
942
959
            self.emit('error', GObject.GError, e)
943
960
        del f
944
961
 
945
 
    def _file_download_complete_cb(self, f, result, path=None):
 
962
    def _file_download_complete_cb(self, f, result, want_url):
946
963
        self.LOG.debug("file download completed %s" % self.dest_file_path)
 
964
        if not self._ensure_correct_url(want_url):
 
965
            return
947
966
        # The result from the download is actually a tuple with three
948
967
        # elements (content, size, etag?)
949
968
        # The first element is the actual content so let's grab that