prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Running program to completion now prints `Terminated` in the console window
  * Python program contains `print("Hello world")`
  * Debug the above code without any breakpoints
  * I.e. let it run to completion
  
  This did not happen in the past.
  
  The output is as follows:
  ```
  pydev debugger: New process is launching (breakpoints won't work in the new process).
  pydev debugger: To debug that process please enable 'Attach to subprocess automatically while debugging?' option in the debugger settings.
  
  a
  Terminated
  ```
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `ptvsd/daemon.py`
  Content:
  ```
  1 import atexit
  2 import os
  3 import platform
  4 import signal
  5 import sys
  6 
  7 from ptvsd import wrapper
  8 from ptvsd.socket import close_socket
  9 
  10 
  11 def _wait_on_exit():
  12     if sys.__stdout__ is not None:
  13         try:
  14             import msvcrt
  15         except ImportError:
  16             sys.__stdout__.write('Press Enter to continue . . . ')
  17             sys.__stdout__.flush()
  18             sys.__stdin__.read(1)
  19         else:
  20             sys.__stdout__.write('Press any key to continue . . . ')
  21             sys.__stdout__.flush()
  22             msvcrt.getch()
  23 
  24 
  25 class DaemonClosedError(RuntimeError):
  26     """Indicates that a Daemon was unexpectedly closed."""
  27     def __init__(self, msg='closed'):
  28         super(DaemonClosedError, self).__init__(msg)
  29 
  30 
  31 class Daemon(object):
  32     """The process-level manager for the VSC protocol debug adapter."""
  33 
  34     exitcode = 0
  35 
  36     def __init__(self, wait_on_exit=_wait_on_exit,
  37                  addhandlers=True, killonclose=True):
  38         self.wait_on_exit = wait_on_exit
  39         self.addhandlers = addhandlers
  40         self.killonclose = killonclose
  41 
  42         self._closed = False
  43 
  44         self._pydevd = None
  45         self._server = None
  46         self._client = None
  47         self._adapter = None
  48 
  49     @property
  50     def pydevd(self):
  51         return self._pydevd
  52 
  53     @property
  54     def server(self):
  55         return self._server
  56 
  57     @property
  58     def client(self):
  59         return self._client
  60 
  61     @property
  62     def adapter(self):
  63         return self._adapter
  64 
  65     def start(self, server=None):
  66         """Return the "socket" to use for pydevd after setting it up."""
  67         if self._closed:
  68             raise DaemonClosedError()
  69         if self._pydevd is not None:
  70             raise RuntimeError('already started')
  71         self._pydevd = wrapper.PydevdSocket(
  72             self._handle_pydevd_message,
  73             self._handle_pydevd_close,
  74             self._getpeername,
  75             self._getsockname,
  76         )
  77         self._server = server
  78         return self._pydevd
  79 
  80     def set_connection(self, client):
  81         """Set the client socket to use for the debug adapter.
  82 
  83         A VSC message loop is started for the client.
  84         """
  85         if self._closed:
  86             raise DaemonClosedError()
  87         if self._pydevd is None:
  88             raise RuntimeError('not started yet')
  89         if self._client is not None:
  90             raise RuntimeError('connection already set')
  91         self._client = client
  92 
  93         self._adapter = wrapper.VSCodeMessageProcessor(
  94             client,
  95             self._pydevd.pydevd_notify,
  96             self._pydevd.pydevd_request,
  97             self._handle_vsc_disconnect,
  98             self._handle_vsc_close,
  99         )
  100         name = 'ptvsd.Client' if self._server is None else 'ptvsd.Server'
  101         self._adapter.start(name)
  102         if self.addhandlers:
  103             self._add_atexit_handler()
  104             self._set_signal_handlers()
  105         return self._adapter
  106 
  107     def close(self):
  108         """Stop all loops and release all resources."""
  109         if self._closed:
  110             raise DaemonClosedError('already closed')
  111         self._closed = True
  112 
  113         if self._adapter is not None:
  114             normal, abnormal = self._adapter._wait_options()
  115             if (normal and not self.exitcode) or (abnormal and self.exitcode):
  116                 self.wait_on_exit()
  117 
  118         if self._pydevd is not None:
  119             close_socket(self._pydevd)
  120         if self._client is not None:
  121             self._release_connection()
  122 
  123     # internal methods
  124 
  125     def _add_atexit_handler(self):
  126         def handler():
  127             if not self._closed:
  128                 self.close()
  129             if self._adapter is not None:
  130                 # TODO: Do this in VSCodeMessageProcessor.close()?
  131                 self._adapter._wait_for_server_thread()
  132         atexit.register(handler)
  133 
  134     def _set_signal_handlers(self):
  135         if platform.system() == 'Windows':
  136             return None
  137 
  138         def handler(signum, frame):
  139             if not self._closed:
  140                 self.close()
  141             sys.exit(0)
  142         signal.signal(signal.SIGHUP, handler)
  143 
  144     def _release_connection(self):
  145         if self._adapter is not None:
  146             # TODO: This is not correct in the "attach" case.
  147             self._adapter.handle_pydevd_stopped(self.exitcode)
  148             self._adapter.close()
  149         close_socket(self._client)
  150 
  151     # internal methods for PyDevdSocket().
  152 
  153     def _handle_pydevd_message(self, cmdid, seq, text):
  154         if self._adapter is not None:
  155             self._adapter.on_pydevd_event(cmdid, seq, text)
  156 
  157     def _handle_pydevd_close(self):
  158         if self._closed:
  159             return
  160         self.close()
  161 
  162     def _getpeername(self):
  163         if self._client is None:
  164             raise NotImplementedError
  165         return self._client.getpeername()
  166 
  167     def _getsockname(self):
  168         if self._client is None:
  169             raise NotImplementedError
  170         return self._client.getsockname()
  171 
  172     # internal methods for VSCodeMessageProcessor
  173 
  174     def _handle_vsc_disconnect(self, kill=False):
  175         if not self._closed:
  176             self.close()
  177         if kill and self.killonclose:
  178             os.kill(os.getpid(), signal.SIGTERM)
  179 
  180     def _handle_vsc_close(self):
  181         if self._closed:
  182             return
  183         self.close()
  184
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/ptvsd/daemon.py b/ptvsd/daemon.py
  --- a/ptvsd/daemon.py
  +++ b/ptvsd/daemon.py
  @@ -40,6 +40,7 @@
           self.killonclose = killonclose
   
           self._closed = False
  +        self._exiting_via_atexit_handler = False
   
           self._pydevd = None
           self._server = None
  @@ -124,6 +125,7 @@
   
       def _add_atexit_handler(self):
           def handler():
  +            self._exiting_via_atexit_handler = True
               if not self._closed:
                   self.close()
               if self._adapter is not None:
  @@ -174,7 +176,7 @@
       def _handle_vsc_disconnect(self, kill=False):
           if not self._closed:
               self.close()
  -        if kill and self.killonclose:
  +        if kill and self.killonclose and not self._exiting_via_atexit_handler:
               os.kill(os.getpid(), signal.SIGTERM)
   
       def _handle_vsc_close(self):
  
verification_info: {"golden_diff": "diff --git a/ptvsd/daemon.py b/ptvsd/daemon.py\n--- a/ptvsd/daemon.py\n+++ b/ptvsd/daemon.py\n@@ -40,6 +40,7 @@\n         self.killonclose = killonclose\n \n         self._closed = False\n+        self._exiting_via_atexit_handler = False\n \n         self._pydevd = None\n         self._server = None\n@@ -124,6 +125,7 @@\n \n     def _add_atexit_handler(self):\n         def handler():\n+            self._exiting_via_atexit_handler = True\n             if not self._closed:\n                 self.close()\n             if self._adapter is not None:\n@@ -174,7 +176,7 @@\n     def _handle_vsc_disconnect(self, kill=False):\n         if not self._closed:\n             self.close()\n-        if kill and self.killonclose:\n+        if kill and self.killonclose and not self._exiting_via_atexit_handler:\n             os.kill(os.getpid(), signal.SIGTERM)\n \n     def _handle_vsc_close(self):\n", "issue": "Running program to completion now prints `Terminated` in the console window\n* Python program contains `print(\"Hello world\")`\r\n* Debug the above code without any breakpoints\r\n* I.e. let it run to completion\r\n\r\nThis did not happen in the past.\r\n\r\nThe output is as follows:\r\n```\r\npydev debugger: New process is launching (breakpoints won't work in the new process).\r\npydev debugger: To debug that process please enable 'Attach to subprocess automatically while debugging?' option in the debugger settings.\r\n\r\na\r\nTerminated\r\n```\n", "before_files": [{"content": "import atexit\nimport os\nimport platform\nimport signal\nimport sys\n\nfrom ptvsd import wrapper\nfrom ptvsd.socket import close_socket\n\n\ndef _wait_on_exit():\n    if sys.__stdout__ is not None:\n        try:\n            import msvcrt\n        except ImportError:\n            sys.__stdout__.write('Press Enter to continue . . . ')\n            sys.__stdout__.flush()\n            sys.__stdin__.read(1)\n        else:\n            sys.__stdout__.write('Press any key to continue . . . ')\n            sys.__stdout__.flush()\n            msvcrt.getch()\n\n\nclass DaemonClosedError(RuntimeError):\n    \"\"\"Indicates that a Daemon was unexpectedly closed.\"\"\"\n    def __init__(self, msg='closed'):\n        super(DaemonClosedError, self).__init__(msg)\n\n\nclass Daemon(object):\n    \"\"\"The process-level manager for the VSC protocol debug adapter.\"\"\"\n\n    exitcode = 0\n\n    def __init__(self, wait_on_exit=_wait_on_exit,\n                 addhandlers=True, killonclose=True):\n        self.wait_on_exit = wait_on_exit\n        self.addhandlers = addhandlers\n        self.killonclose = killonclose\n\n        self._closed = False\n\n        self._pydevd = None\n        self._server = None\n        self._client = None\n        self._adapter = None\n\n    @property\n    def pydevd(self):\n        return self._pydevd\n\n    @property\n    def server(self):\n        return self._server\n\n    @property\n    def client(self):\n        return self._client\n\n    @property\n    def adapter(self):\n        return self._adapter\n\n    def start(self, server=None):\n        \"\"\"Return the \"socket\" to use for pydevd after setting it up.\"\"\"\n        if self._closed:\n            raise DaemonClosedError()\n        if self._pydevd is not None:\n            raise RuntimeError('already started')\n        self._pydevd = wrapper.PydevdSocket(\n            self._handle_pydevd_message,\n            self._handle_pydevd_close,\n            self._getpeername,\n            self._getsockname,\n        )\n        self._server = server\n        return self._pydevd\n\n    def set_connection(self, client):\n        \"\"\"Set the client socket to use for the debug adapter.\n\n        A VSC message loop is started for the client.\n        \"\"\"\n        if self._closed:\n            raise DaemonClosedError()\n        if self._pydevd is None:\n            raise RuntimeError('not started yet')\n        if self._client is not None:\n            raise RuntimeError('connection already set')\n        self._client = client\n\n        self._adapter = wrapper.VSCodeMessageProcessor(\n            client,\n            self._pydevd.pydevd_notify,\n            self._pydevd.pydevd_request,\n            self._handle_vsc_disconnect,\n            self._handle_vsc_close,\n        )\n        name = 'ptvsd.Client' if self._server is None else 'ptvsd.Server'\n        self._adapter.start(name)\n        if self.addhandlers:\n            self._add_atexit_handler()\n            self._set_signal_handlers()\n        return self._adapter\n\n    def close(self):\n        \"\"\"Stop all loops and release all resources.\"\"\"\n        if self._closed:\n            raise DaemonClosedError('already closed')\n        self._closed = True\n\n        if self._adapter is not None:\n            normal, abnormal = self._adapter._wait_options()\n            if (normal and not self.exitcode) or (abnormal and self.exitcode):\n                self.wait_on_exit()\n\n        if self._pydevd is not None:\n            close_socket(self._pydevd)\n        if self._client is not None:\n            self._release_connection()\n\n    # internal methods\n\n    def _add_atexit_handler(self):\n        def handler():\n            if not self._closed:\n                self.close()\n            if self._adapter is not None:\n                # TODO: Do this in VSCodeMessageProcessor.close()?\n                self._adapter._wait_for_server_thread()\n        atexit.register(handler)\n\n    def _set_signal_handlers(self):\n        if platform.system() == 'Windows':\n            return None\n\n        def handler(signum, frame):\n            if not self._closed:\n                self.close()\n            sys.exit(0)\n        signal.signal(signal.SIGHUP, handler)\n\n    def _release_connection(self):\n        if self._adapter is not None:\n            # TODO: This is not correct in the \"attach\" case.\n            self._adapter.handle_pydevd_stopped(self.exitcode)\n            self._adapter.close()\n        close_socket(self._client)\n\n    # internal methods for PyDevdSocket().\n\n    def _handle_pydevd_message(self, cmdid, seq, text):\n        if self._adapter is not None:\n            self._adapter.on_pydevd_event(cmdid, seq, text)\n\n    def _handle_pydevd_close(self):\n        if self._closed:\n            return\n        self.close()\n\n    def _getpeername(self):\n        if self._client is None:\n            raise NotImplementedError\n        return self._client.getpeername()\n\n    def _getsockname(self):\n        if self._client is None:\n            raise NotImplementedError\n        return self._client.getsockname()\n\n    # internal methods for VSCodeMessageProcessor\n\n    def _handle_vsc_disconnect(self, kill=False):\n        if not self._closed:\n            self.close()\n        if kill and self.killonclose:\n            os.kill(os.getpid(), signal.SIGTERM)\n\n    def _handle_vsc_close(self):\n        if self._closed:\n            return\n        self.close()\n", "path": "ptvsd/daemon.py"}], "after_files": [{"content": "import atexit\nimport os\nimport platform\nimport signal\nimport sys\n\nfrom ptvsd import wrapper\nfrom ptvsd.socket import close_socket\n\n\ndef _wait_on_exit():\n    if sys.__stdout__ is not None:\n        try:\n            import msvcrt\n        except ImportError:\n            sys.__stdout__.write('Press Enter to continue . . . ')\n            sys.__stdout__.flush()\n            sys.__stdin__.read(1)\n        else:\n            sys.__stdout__.write('Press any key to continue . . . ')\n            sys.__stdout__.flush()\n            msvcrt.getch()\n\n\nclass DaemonClosedError(RuntimeError):\n    \"\"\"Indicates that a Daemon was unexpectedly closed.\"\"\"\n    def __init__(self, msg='closed'):\n        super(DaemonClosedError, self).__init__(msg)\n\n\nclass Daemon(object):\n    \"\"\"The process-level manager for the VSC protocol debug adapter.\"\"\"\n\n    exitcode = 0\n\n    def __init__(self, wait_on_exit=_wait_on_exit,\n                 addhandlers=True, killonclose=True):\n        self.wait_on_exit = wait_on_exit\n        self.addhandlers = addhandlers\n        self.killonclose = killonclose\n\n        self._closed = False\n        self._exiting_via_atexit_handler = False\n\n        self._pydevd = None\n        self._server = None\n        self._client = None\n        self._adapter = None\n\n    @property\n    def pydevd(self):\n        return self._pydevd\n\n    @property\n    def server(self):\n        return self._server\n\n    @property\n    def client(self):\n        return self._client\n\n    @property\n    def adapter(self):\n        return self._adapter\n\n    def start(self, server=None):\n        \"\"\"Return the \"socket\" to use for pydevd after setting it up.\"\"\"\n        if self._closed:\n            raise DaemonClosedError()\n        if self._pydevd is not None:\n            raise RuntimeError('already started')\n        self._pydevd = wrapper.PydevdSocket(\n            self._handle_pydevd_message,\n            self._handle_pydevd_close,\n            self._getpeername,\n            self._getsockname,\n        )\n        self._server = server\n        return self._pydevd\n\n    def set_connection(self, client):\n        \"\"\"Set the client socket to use for the debug adapter.\n\n        A VSC message loop is started for the client.\n        \"\"\"\n        if self._closed:\n            raise DaemonClosedError()\n        if self._pydevd is None:\n            raise RuntimeError('not started yet')\n        if self._client is not None:\n            raise RuntimeError('connection already set')\n        self._client = client\n\n        self._adapter = wrapper.VSCodeMessageProcessor(\n            client,\n            self._pydevd.pydevd_notify,\n            self._pydevd.pydevd_request,\n            self._handle_vsc_disconnect,\n            self._handle_vsc_close,\n        )\n        name = 'ptvsd.Client' if self._server is None else 'ptvsd.Server'\n        self._adapter.start(name)\n        if self.addhandlers:\n            self._add_atexit_handler()\n            self._set_signal_handlers()\n        return self._adapter\n\n    def close(self):\n        \"\"\"Stop all loops and release all resources.\"\"\"\n        if self._closed:\n            raise DaemonClosedError('already closed')\n        self._closed = True\n\n        if self._adapter is not None:\n            normal, abnormal = self._adapter._wait_options()\n            if (normal and not self.exitcode) or (abnormal and self.exitcode):\n                self.wait_on_exit()\n\n        if self._pydevd is not None:\n            close_socket(self._pydevd)\n        if self._client is not None:\n            self._release_connection()\n\n    # internal methods\n\n    def _add_atexit_handler(self):\n        def handler():\n            self._exiting_via_atexit_handler = True\n            if not self._closed:\n                self.close()\n            if self._adapter is not None:\n                # TODO: Do this in VSCodeMessageProcessor.close()?\n                self._adapter._wait_for_server_thread()\n        atexit.register(handler)\n\n    def _set_signal_handlers(self):\n        if platform.system() == 'Windows':\n            return None\n\n        def handler(signum, frame):\n            if not self._closed:\n                self.close()\n            sys.exit(0)\n        signal.signal(signal.SIGHUP, handler)\n\n    def _release_connection(self):\n        if self._adapter is not None:\n            # TODO: This is not correct in the \"attach\" case.\n            self._adapter.handle_pydevd_stopped(self.exitcode)\n            self._adapter.close()\n        close_socket(self._client)\n\n    # internal methods for PyDevdSocket().\n\n    def _handle_pydevd_message(self, cmdid, seq, text):\n        if self._adapter is not None:\n            self._adapter.on_pydevd_event(cmdid, seq, text)\n\n    def _handle_pydevd_close(self):\n        if self._closed:\n            return\n        self.close()\n\n    def _getpeername(self):\n        if self._client is None:\n            raise NotImplementedError\n        return self._client.getpeername()\n\n    def _getsockname(self):\n        if self._client is None:\n            raise NotImplementedError\n        return self._client.getsockname()\n\n    # internal methods for VSCodeMessageProcessor\n\n    def _handle_vsc_disconnect(self, kill=False):\n        if not self._closed:\n            self.close()\n        if kill and self.killonclose and not self._exiting_via_atexit_handler:\n            os.kill(os.getpid(), signal.SIGTERM)\n\n    def _handle_vsc_close(self):\n        if self._closed:\n            return\n        self.close()\n", "path": "ptvsd/daemon.py"}]}