I propose changing the socket servers to not suppress exceptions that are meant to exit the interpreter. This is most applicable to single threaded servers, but my patch does the same thing for multithreading servers. It no longer catches exceptions that are not derived from the Exception class, such as KeyboardInterrupt and SystemExit. The shutdown_request() method is still called in all cases though. I also added a test for the forking server’s handle_error() method.
Looking at this again, I think I should make the forking server’s handle_error() method only be called for Exception subclasses as well. So I am posting a new patch that also does this.
Patch v3: * Changed the structure of exception handling for the forking server so that there is only one os._exit(status) call site * Rewrote tests to log results to a temporary file. This avoids sending a string of code to a Python subprocess. I use threading.Event and os.waitpid() to synchronize. * Tests now call the handle_request() public API, and create a trivial TCP connection to trigger a request. * Added doc string explaining simple_subprocess() Let me know what you think.
Here is an updated version for 3.6. I also wrote an entry for What’s New. It seems my change could help people trying to make a single-threaded server quit the server loop (Issue 13749).