diff --git a/mercurial/commands.py b/mercurial/commands.py
index 58e7ee23ddbd2e90ce4a256a2f2d182c03d0b73e_bWVyY3VyaWFsL2NvbW1hbmRzLnB5..b7b8a15381614a36e857ef95461ef30db936c5e3_bWVyY3VyaWFsL2NvbW1hbmRzLnB5 100644
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -6348,6 +6348,7 @@
             )
         s = wireprotoserver.sshserver(ui, repo)
         s.serve_forever()
+        sys.exit(0)
 
     service = server.createservice(ui, repo, opts)
     return server.runservice(opts, initfn=service.init, runfn=service.run)
diff --git a/mercurial/debugcommands.py b/mercurial/debugcommands.py
index 58e7ee23ddbd2e90ce4a256a2f2d182c03d0b73e_bWVyY3VyaWFsL2RlYnVnY29tbWFuZHMucHk=..b7b8a15381614a36e857ef95461ef30db936c5e3_bWVyY3VyaWFsL2RlYnVnY29tbWFuZHMucHk= 100644
--- a/mercurial/debugcommands.py
+++ b/mercurial/debugcommands.py
@@ -3376,6 +3376,7 @@
 
     s = wireprotoserver.sshserver(ui, repo, logfh=logfh)
     s.serve_forever()
+    sys.exit(0)
 
 
 @command(b'debugsetparents', [], _(b'REV1 [REV2]'))
diff --git a/mercurial/hgweb/__init__.py b/mercurial/hgweb/__init__.py
index 58e7ee23ddbd2e90ce4a256a2f2d182c03d0b73e_bWVyY3VyaWFsL2hnd2ViL19faW5pdF9fLnB5..b7b8a15381614a36e857ef95461ef30db936c5e3_bWVyY3VyaWFsL2hnd2ViL19faW5pdF9fLnB5 100644
--- a/mercurial/hgweb/__init__.py
+++ b/mercurial/hgweb/__init__.py
@@ -9,6 +9,7 @@
 from __future__ import absolute_import
 
 import os
+import sys
 
 from ..i18n import _
 
@@ -111,6 +112,7 @@
 
     def run(self):
         self.httpd.serve_forever()
+        sys.exit(0)
 
 
 def createapp(baseui, repo, webconf):
diff --git a/mercurial/wireprotoserver.py b/mercurial/wireprotoserver.py
index 58e7ee23ddbd2e90ce4a256a2f2d182c03d0b73e_bWVyY3VyaWFsL3dpcmVwcm90b3NlcnZlci5weQ==..b7b8a15381614a36e857ef95461ef30db936c5e3_bWVyY3VyaWFsL3dpcmVwcm90b3NlcnZlci5weQ== 100644
--- a/mercurial/wireprotoserver.py
+++ b/mercurial/wireprotoserver.py
@@ -8,7 +8,6 @@
 
 import contextlib
 import struct
-import sys
 import threading
 
 from .i18n import _
@@ -851,7 +850,6 @@
     def serve_forever(self):
         self.serveuntil(threading.Event())
         self._ui.restorefinout(self._fin, self._fout)
-        sys.exit(0)
 
     def serveuntil(self, ev):
         """Serve until a threading.Event is set."""