Merge "Shut down the gearman server last in tests"
diff --git a/doc/source/launchers.rst b/doc/source/launchers.rst
index db49933..b92ccdb 100644
--- a/doc/source/launchers.rst
+++ b/doc/source/launchers.rst
@@ -77,17 +77,17 @@
 follows:
 
 **ZUUL_UUID**
-  Zuul provided key to link builds with Gerrit events
+  Zuul provided key to link builds with Gerrit events.
 **ZUUL_REF**
-  Zuul provided ref that includes commit(s) to build
+  Zuul provided ref that includes commit(s) to build.
 **ZUUL_COMMIT**
-  The commit SHA1 at the head of ZUUL_REF
+  The commit SHA1 at the head of ZUUL_REF.
 **ZUUL_PROJECT**
-  The project that triggered this build
+  The project that triggered this build.
 **ZUUL_PIPELINE**
-  The Zuul pipeline that is building this job
+  The Zuul pipeline that is building this job.
 **ZUUL_URL**
-  The url for the zuul server as configured in zuul.conf.
+  The URL for the zuul server as configured in zuul.conf.
   A test runner may use this URL as the basis for fetching
   git commits.
 
@@ -96,28 +96,28 @@
 comment-added events):
 
 **ZUUL_BRANCH**
-  The target branch for the change that triggered this build
+  The target branch for the change that triggered this build.
 **ZUUL_CHANGE**
-  The Gerrit change ID for the change that triggered this build
+  The Gerrit change ID for the change that triggered this build.
 **ZUUL_CHANGE_IDS**
   All of the Gerrit change IDs that are included in this build (useful
-  when the DependentPipelineManager combines changes for testing)
+  when the DependentPipelineManager combines changes for testing).
 **ZUUL_PATCHSET**
-  The Gerrit patchset number for the change that triggered this build
+  The Gerrit patchset number for the change that triggered this build.
 
 The following additional parameters will only be provided for
 post-merge (ref-updated) builds:
 
 **ZUUL_OLDREV**
   The SHA1 of the old revision at this ref (recall the ref name is
-  in ZUUL_REF)
+  in ZUUL_REF).
 **ZUUL_NEWREV**
   The SHA1 of the new revision at this ref (recall the ref name is
-  in ZUUL_REF)
+  in ZUUL_REF).
 **ZUUL_SHORT_OLDREV**
-  The shortened (7 character) SHA1 of the old revision
+  The shortened (7 character) SHA1 of the old revision.
 **ZUUL_SHORT_NEWREV**
-  The shortened (7 character) SHA1 of the new revision
+  The shortened (7 character) SHA1 of the new revision.
 
 In order to test the correct build, configure the Jenkins Git SCM
 plugin as follows::
diff --git a/doc/source/reporters.rst b/doc/source/reporters.rst
index 7c0214d..f94a439 100644
--- a/doc/source/reporters.rst
+++ b/doc/source/reporters.rst
@@ -39,10 +39,10 @@
 SMTP Configuration
 ~~~~~~~~~~~~~~~~~~
 
-zuul.conf contains the SMTP server and default to/from as describe
+zuul.conf contains the SMTP server and default to/from as described
 in :ref:`zuulconf`.
 
-Each pipeline can overwrite the subject or the to or from address by
+Each pipeline can overwrite the ``subject`` or the ``to`` or ``from`` address by
 providing alternatives as arguments to the reporter. For example, ::
 
   pipelines:
diff --git a/doc/source/zuul.rst b/doc/source/zuul.rst
index 1a6a23d..a68a030 100644
--- a/doc/source/zuul.rst
+++ b/doc/source/zuul.rst
@@ -10,11 +10,11 @@
 
 **zuul.conf**
   Connection information for Gerrit and Gearman, locations of the
-  other config files
+  other config files.
 **layout.yaml**
-  Project and pipeline configuration -- what Zuul does
+  Project and pipeline configuration -- what Zuul does.
 **logging.conf**
-    Python logging config
+    Python logging config.
 
 Examples of each of the three files can be found in the etc/ directory
 of the source distribution.
@@ -46,7 +46,7 @@
   ``server=gearman.example.com``
 
 **port**
-  Port on which the Gearman server is listening
+  Port on which the Gearman server is listening.
   ``port=4730``
 
 gearman_server
@@ -365,14 +365,14 @@
       If present, the approval must be older than this amount of time
       to match.  Provide a time interval as a number with a suffix of
       "w" (weeks), "d" (days), "h" (hours), "m" (minutes), "s"
-      (seconds).  Example "48h" or "2d".
+      (seconds).  Example ``48h`` or ``2d``.
 
       *newer-than*
       If present, the approval must be newer than this amount of time
       to match.  Same format as "older-than".
 
       Any other field is interpreted as a review category and value
-      pair.  For example "verified: 1" would require that the approval
+      pair.  For example ``verified: 1`` would require that the approval
       be for a +1 vote in the "Verified" column.
 
   **timer**
@@ -547,7 +547,7 @@
   The ``change-merged`` event does not include the commit sha1 which can be
   hazardous, it would let you report back to Gerrit though.  If you were to
   build a tarball for a specific commit, you should consider instead using
-  the ``ref-updated`` event which does include the commit sha1 (but lack the
+  the ``ref-updated`` event which does include the commit sha1 (but lacks the
   Gerrit change number).
 
 Jobs
diff --git a/tests/test_scheduler.py b/tests/test_scheduler.py
index 0399ca5..5bd8b00 100755
--- a/tests/test_scheduler.py
+++ b/tests/test_scheduler.py
@@ -1112,6 +1112,7 @@
                     self.sched.result_event_queue.empty() and
                     self.fake_gerrit.event_queue.empty() and
                     not self.merge_client.build_sets and
+                    self.haveAllBuildsReported() and
                     self.areAllBuildsWaiting()):
                     self.sched.run_handler_lock.release()
                     self.worker.lock.release()
diff --git a/zuul/launcher/gearman.py b/zuul/launcher/gearman.py
index 0e248af..3638add 100644
--- a/zuul/launcher/gearman.py
+++ b/zuul/launcher/gearman.py
@@ -352,11 +352,11 @@
         self.log.debug("Still unable to find build %s to cancel" % build)
         if build.number:
             self.log.debug("Build %s has just started" % build)
-            self.cancelRunningBuild(build)
-            self.log.debug("Canceled just running build %s" % build)
         else:
-            self.log.error("Build %s has not started but "
-                           "was not found in queue" % build)
+            self.log.error("Build %s has not started but was not"
+                           "found in queue; canceling anyway" % build)
+        self.cancelRunningBuild(build)
+        self.log.debug("Canceled possibly running build %s" % build)
 
     def onBuildCompleted(self, job, result=None):
         if job.unique in self.meta_jobs: