# Buggy code
def shutdown(self):
    with self.shutdown_lock:
        if not self.is_running:
            self.logger.warn("Webhook Server already stopped.")
            return
        else:
            super(WebhookServer, self).shutdown()
            self.is_running = False


# Traceback (ConnectionResetError)
2018-01-10 03:54:56,480 - telegram.ext.updater - ERROR - unhandled exception
Traceback (most recent call last):
File "/usr/lib/python3.5/socketserver.py", line 313, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python3.5/socketserver.py", line 341, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python3.5/socketserver.py", line 354, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/local/lib/python3.5/dist-packages/telegram/utils/webhookhandler.py", line 62, in __init__
super(WebhookHandler, self).__init__(request, client_address, server)
File "/usr/lib/python3.5/socketserver.py", line 681, in __init__
self.handle()
File "/usr/lib/python3.5/http/server.py", line 422, in handle
self.handle_one_request()
File "/usr/lib/python3.5/http/server.py", line 390, in handle_one_request
self.raw_requestline = self.rfile.readline(65537)
File "/usr/lib/python3.5/socket.py", line 575, in readinto
return self._sock.recv_into(b)
ConnectionResetError: [Errno 104] Connection reset by peer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/telegram/ext/updater.py", line 149, in _thread_wrapper
target(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/telegram/ext/updater.py", line 353, in _start_webhook
self.httpd.serve_forever(poll_interval=1)
File "/usr/local/lib/python3.5/dist-packages/telegram/utils/webhookhandler.py", line 41, in serve_forever
super(WebhookServer, self).serve_forever(poll_interval)
File "/usr/lib/python3.5/socketserver.py", line 234, in serve_forever
self._handle_request_noblock()
File "/usr/lib/python3.5/socketserver.py", line 315, in _handle_request_noblock
self.handle_error(request, client_address)
File "/usr/lib/python3.5/socketserver.py", line 371, in handle_error
print('Exception happened during processing of request from', end=' ')
BrokenPipeError: [Errno 32] Broken pipe
Exception in thread updater:
Traceback (most recent call last):
File "/usr/lib/python3.5/socketserver.py", line 313, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python3.5/socketserver.py", line 341, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python3.5/socketserver.py", line 354, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/local/lib/python3.5/dist-packages/telegram/utils/webhookhandler.py", line 62, in __init__
super(WebhookHandler, self).__init__(request, client_address, server)
File "/usr/lib/python3.5/socketserver.py", line 681, in __init__
self.handle()
File "/usr/lib/python3.5/http/server.py", line 422, in handle
self.handle_one_request()
File "/usr/lib/python3.5/http/server.py", line 390, in handle_one_request
self.raw_requestline = self.rfile.readline(65537)
File "/usr/lib/python3.5/socket.py", line 575, in readinto
return self._sock.recv_into(b)
ConnectionResetError: [Errno 104] Connection reset by peer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.5/dist-packages/telegram/ext/updater.py", line 149, in _thread_wrapper
target(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/telegram/ext/updater.py", line 353, in _start_webhook
self.httpd.serve_forever(poll_interval=1)
File "/usr/local/lib/python3.5/dist-packages/telegram/utils/webhookhandler.py", line 41, in serve_forever
super(WebhookServer, self).serve_forever(poll_interval)
File "/usr/lib/python3.5/socketserver.py", line 234, in serve_forever
self._handle_request_noblock()
File "/usr/lib/python3.5/socketserver.py", line 315, in _handle_request_noblock
self.handle_error(request, client_address)
File "/usr/lib/python3.5/socketserver.py", line 371, in handle_error
print('Exception happened during processing of request from', end=' ')
BrokenPipeError: [Errno 32] Broken pipe

2018-01-10 03:54:56,601 - telegram.ext.dispatcher - CRITICAL - stopping due to exception in another thread
2018-01-10 03:54:56,601 - telegram.ext.dispatcher - DEBUG - Dispatcher thread stopped
2018-01-10 03:54:56,601 - telegram.ext.updater - DEBUG - dispatcher - ended

# Fixed code
def shutdown(self):
    with self.shutdown_lock:
        if not self.is_running:
            self.logger.warning("Webhook Server already stopped.")
            return
        else:
            super(WebhookServer, self).shutdown()
            self.is_running = False