 
   (ycmd--start-keepalive-timer))
 
-(defun ycmd-close (&optional time-out-secs)
+(defun ycmd-close ()
   "Shutdown any running ycmd server.
 
-Wait TIME-OUT-SECS seconds after `interrupt-process' call for the
-ycmd server to end before killing the process with
-`delete-process'.
-
 This does nothing if no server is running."
   (interactive)
-
-  (unwind-protect
-      (when (ycmd-running?)
-        (condition-case nil
-            (progn
-              (interrupt-process ycmd--server-process)
-              (when time-out-secs
-                (sit-for time-out-secs)
-                (delete-process ycmd--server-process)))
-          (error nil)))
-    (ycmd--global-teardown))
-
+  (when (ycmd-running?)
+    (ycmd--stop-server))
+  (ycmd--global-teardown)
   (ycmd--kill-timer ycmd--keepalive-timer))
+
+(defun ycmd--stop-server ()
+  "Stop the ycmd server process.
+
+Call `interrupt-process' for the ycmd server and wait for the
+ycmd server to stop.  If the ycmd server is still running after a
+timeout specified by `ycmd-delete-process-delay', then kill the
+process with `delete-process'."
+  (condition-case nil
+      (let ((start-time (float-time)))
+        (interrupt-process ycmd--server-process)
+        (while (and (ycmd-running?)
+                    (> ycmd-delete-process-delay
+                       (- (float-time) start-time)))
+          (sit-for 0.05))
+        (delete-process ycmd--server-process))
+    (error nil)))
 
 (defun ycmd-running? ()
   "Return t if a ycmd server is already running."
-  (and (get-process ycmd--server-process) t))
+  (process-live-p (get-process ycmd--server-process)))
 
 (defun ycmd--keepalive ()
   "Sends an unspecified message to the server.

  :group 'ycmd
  :type '(number))

(defcustom ycmd-startup-timeout 3000
  "Number of milliseconds to wait for the server to start."
  :group 'ycmd
  :type '(number))


  "Start a new server using HMAC-SECRET as its hmac secret."
  (let ((proc-buff (get-buffer-create ycmd--server-buffer-name)))
    (with-current-buffer proc-buff
      (buffer-disable-undo proc-buff)
      (erase-buffer)

      (let* ((options-file (ycmd--create-options-file hmac-secret))
