From 77f55f368a63615175ad8e7630505a991ebcba57 Mon Sep 17 00:00:00 2001
From: Zooko O'Whielacronx <zooko@zooko.com>
Date: Wed, 23 May 2007 15:08:03 -0700
Subject: [PATCH] make stopService() defer until startService() completes
 (fixes a problem with the new not-yet-committed unit test)

---
 src/allmydata/node.py | 29 +++++++++++++----------------
 1 file changed, 13 insertions(+), 16 deletions(-)

diff --git a/src/allmydata/node.py b/src/allmydata/node.py
index 44b9d15f..750e61fb 100644
--- a/src/allmydata/node.py
+++ b/src/allmydata/node.py
@@ -1,11 +1,11 @@
-
 import os.path, re
 
 from twisted.python import log
 from twisted.application import service
-from twisted.internet import defer
-from foolscap import Tub
+from twisted.internet import defer, reactor
+from foolscap import Tub, eventual
 from allmydata.util import idlib, iputil, observer
+from allmydata.util.assertutil import _assert, precondition
 
 
 # Just to get their versions:
@@ -62,18 +62,8 @@ class Node(service.MultiService):
 
         self.log("Node constructed.  tahoe version: %s, foolscap version: %s, zfec version: %s" % (allmydata.__version__, foolscap.__version__, zfec.__version__,))
 
-    def startService(self):
-        """Start the node. Returns a Deferred that fires (with self) when it
-        is ready to go.
-
-        Many callers don't pay attention to the return value from
-        startService, since they aren't going to do anything special when it
-        finishes. If they are (for example unit tests which need to wait for
-        the node to fully start up before it gets shut down), they can wait
-        for the Deferred I return to fire. In particular, you should wait for
-        my startService() Deferred to fire before you call my stopService()
-        method.
-        """
+    def _startService(self):
+        precondition(reactor.running)
 
         # note: this class can only be started and stopped once.
         service.MultiService.startService(self)
@@ -86,8 +76,15 @@ class Node(service.MultiService):
             self._tub_ready_observerlist.fire(self)
             return self
         d.addCallback(_ready)
-        return d
 
+    def startService(self):
+        foolscap.eventual.eventually(self._startService)
+
+    def stopService(self):
+        d = self._tub_ready_observerlist.when_fired()
+        d.addCallback(lambda ignored_result: service.MultiService.stopService(self))
+        return d
+       
     def shutdown(self):
         """Shut down the node. Returns a Deferred that fires (with None) when
         it finally stops kicking."""
-- 
2.45.2