From: Zooko O'Whielacronx Date: Tue, 22 May 2007 21:06:00 +0000 (-0700) Subject: don't check for existence of portnum file and then try to open it -- instead try... X-Git-Tag: allmydata-tahoe-0.3.0~69 X-Git-Url: https://git.rkrishnan.org/pf/content/en/footer/(%5B%5E?a=commitdiff_plain;h=921e643b93c88c2a0b560bbc21989918cde47c60;p=tahoe-lafs%2Ftahoe-lafs.git don't check for existence of portnum file and then try to open it -- instead try to open it and catch exception This avoids a race condition, also known as time-of-check-to-time-of-use. --- diff --git a/src/allmydata/node.py b/src/allmydata/node.py index 2611fddf..79dc3186 100644 --- a/src/allmydata/node.py +++ b/src/allmydata/node.py @@ -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