diff --git a/anki/sync.py b/anki/sync.py index 4d4dffd37..a8b7af7dd 100644 --- a/anki/sync.py +++ b/anki/sync.py @@ -48,6 +48,7 @@ SYNC_HOST = "anki.ichi2.net"; SYNC_PORT = 80 #SYNC_HOST = "localhost"; SYNC_PORT = 8001 KEYS = ("models", "facts", "cards", "media") +SOCKET_TIMEOUT=30 ########################################################################## # Monkey-patch httplib to incrementally send instead of chewing up large @@ -974,6 +975,7 @@ and cards.id in %s""" % ids2str([c[0] for c in cards]))) size = tmp.tell() tmp.seek(0) # open http connection + socket.setdefaulttimeout(SOCKET_TIMEOUT) runHook("fullSyncStarted", size) headers = { 'Content-type': 'multipart/form-data; boundary=%s' % @@ -991,11 +993,13 @@ and cards.id in %s""" % ids2str([c[0] for c in cards]))) os.close(fd) os.unlink(name) finally: + socket.setdefaulttimeout(None) runHook("fullSyncFinished") def fullSyncFromServer(self, fields, path): try: runHook("fullSyncStarted", 0) + socket.setdefaulttimeout(SOCKET_TIMEOUT) fields = urllib.urlencode(fields) src = urllib.urlopen(SYNC_URL + "fulldown", fields) (fd, tmpname) = tempfile.mkstemp(dir=os.path.dirname(path), @@ -1018,6 +1022,7 @@ and cards.id in %s""" % ids2str([c[0] for c in cards]))) os.unlink(path) os.rename(tmpname, path) finally: + socket.setdefaulttimeout(None) runHook("fullSyncFinished") # Local syncing @@ -1050,14 +1055,11 @@ class HttpSyncServerProxy(SyncServer): def connect(self, clientVersion=""): "Check auth, protocol & grab deck list." if not self.decks: - import socket - socket.setdefaulttimeout(30) d = self.runCmd("getDecks", libanki=anki.version, client=clientVersion, sources=simplejson.dumps(self.sourcesToCheck), pversion=self.protocolVersion) - socket.setdefaulttimeout(None) if d['status'] != "OK": raise SyncError(type="authFailed", status=d['status']) self.decks = d['decks'] @@ -1106,11 +1108,15 @@ class HttpSyncServerProxy(SyncServer): data['d'] = None data.update(args) data = urllib.urlencode(data) + socket.setdefaulttimeout(SOCKET_TIMEOUT) try: - f = urllib2.urlopen(SYNC_URL + action, data) - except (urllib2.URLError, socket.error, socket.timeout, - httplib.BadStatusLine): - raise SyncError(type="noResponse") + try: + f = urllib2.urlopen(SYNC_URL + action, data) + except (urllib2.URLError, socket.error, socket.timeout, + httplib.BadStatusLine): + raise SyncError(type="noResponse") + finally: + socket.setdefaulttimeout(None) ret = f.read() if not ret: raise SyncError(type="noResponse")