]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
don't check for existence of portnum file and then try to open it -- instead try...
authorZooko O'Whielacronx <zooko@zooko.com>
Tue, 22 May 2007 21:06:00 +0000 (14:06 -0700)
committerZooko O'Whielacronx <zooko@zooko.com>
Tue, 22 May 2007 21:06:00 +0000 (14:06 -0700)
This avoids a race condition, also known as time-of-check-to-time-of-use.

src/allmydata/node.py

index 2611fddf8a887f9648b3974a7df486d0a97b778d..79dc3186bf3927c486738869d0e258c3af58c60e 100644 (file)
@@ -47,11 +47,12 @@ class Node(service.MultiService):
         f.write(idlib.b2a(self.nodeid) + "\n")
         f.close()
         self.short_nodeid = self.tub.tubID[:4] # ready for printing
-        portnum = 0
         assert self.PORTNUMFILE, "Your node.Node subclass must provide PORTNUMFILE"
         self._portnumfile = os.path.join(self.basedir, self.PORTNUMFILE)
-        if os.path.exists(self._portnumfile):
-            portnum = int(open(self._portnumfile, "r").read())
+        try:
+            portnum = int(open(self._portnumfile, "rU").read())
+        except EnvironmentError, ValueError:
+            portnum = 0
         self.tub.listenOn("tcp:%d" % portnum)
         # we must wait until our service has started before we can find out
         # our IP address and thus do tub.setLocation, and we can't register