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.
This commit is contained in:
Zooko O'Whielacronx 2007-05-22 14:06:00 -07:00
parent 98066ccaee
commit 921e643b93

View File

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