Hi, everyone,
I ran into this bug today and after an hour of searching the Internet, I came up with nothing.
It seems I was able to fix the issue with a minor change in Storage.py.
Basically, instead of holding a dictionary of open file handles, the Storage class would keep only the file mode, and open the file with it when necessary. I know this "solution" probably suffers from performance issues, but I think it's better to work slow rather than not work at all (for me, at least). Anyway, the patch is attached to this message. It should apply cleanly to the latest CVS as of the time of writing.

Cheers,
Anton
Index: BitTorrent/Storage.py
===================================================================
--- BitTorrent/Storage.py       (revision 203)
+++ BitTorrent/Storage.py       (working copy)
@@ -30,15 +30,16 @@
             if exists(file):
                 l = getsize(file)
                 if l != length:
-                    self.handles[file] = open(file, 'rb+')
+                    self.handles[file] = 'rb+'
                     self.whandles[file] = 1
                     if l > length:
-                        self.handles[file].truncate(length)
+                        fh = open(file, self.handles[file])
+                        fh.truncate(length).close()
                 else:
-                    self.handles[file] = open(file, 'rb')
+                    self.handles[file] = 'rb'
                 self.tops[file] = l
             else:
-                self.handles[file] = open(file, 'wb+')
+                self.handles[file] = 'wb+'
                 self.whandles[file] = 1
 
     def was_preallocated(self, pos, length):
@@ -50,10 +51,10 @@
     def set_readonly(self):
         # may raise IOError or OSError
         for file in self.whandles.keys():
-            old = self.handles[file]
+            old = open(file, self.handles[file])
             old.flush()
             old.close()
-            self.handles[file] = open(file, 'rb')
+            self.handles[file] = 'rb'
 
     def get_total_length(self):
         return self.total_length
@@ -71,9 +72,10 @@
     def read(self, pos, amount):
         r = []
         for file, pos, end in self._intervals(pos, amount):
-            h = self.handles[file]
+            h = open(file, self.handles[file])
             h.seek(pos)
             r.append(h.read(end - pos))
+            h.close()
         return ''.join(r)
 
     def write(self, pos, s):
@@ -81,17 +83,16 @@
         total = 0
         for file, begin, end in self._intervals(pos, len(s)):
             if not self.whandles.has_key(file):
-                self.handles[file].close()
-                self.handles[file] = open(file, 'rb+')
+                self.handles[file] = 'rb+'
                 self.whandles[file] = 1
-            h = self.handles[file]
+            h = open(file, self.handles[file])
             h.seek(begin)
             h.write(s[total: total + end - begin])
             total += end - begin
+            h.close()
 
     def close(self):
-        for h in self.handles.values():
-            h.close()
+        pass
 
 def lrange(a, b, c):
     r = []

Reply via email to