Repository: python-telegram-bot/python-telegram-bot

Issue:
Webhook Broken Pipe after connection reset, kills dispatcher
### Steps to reproduce
1.
Standard Webhook with updater and dispatcher:
```
updater = Updater(TOKEN) PORT = int(os.environ.get('PORT', '5000')) # add handlers 
updater.start_webhook(listen="0.0.0.0", port=PORT, url_path=TOKEN) 
updater.bot.set_webhook("https://home.co.za/" + TOKEN) 
dp = updater.dispatcher dp.add_error_handler(error) 
updater.idle()
```


### Expected behaviour
Bot should run and remain responsive

### Actual behaviour
It runs,  but after a few hours becomes unresponsive.

### Configuration
**Operating System:**
Linux Cappucino 4.4.0-104-generic #127-Ubuntu SMP Mon Dec 11 12:16:50 UTC 2017 i686 athlon i686 GNU/Linux

**Version of Python, python-telegram-bot & dependencies:**

```
python-telegram-bot 9.0.0
certifi 2017.11.05
future 0.16.0
Python 3.5.2 (default, Nov 23 2017, 16:37:01)  [GCC 5.4.0 20160609]
```

### Logs
```
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
```

  


Discussion:

Resolving patch:
diff --git a/telegram/utils/webhookhandler.py b/telegram/utils/webhookhandler.py
--- a/telegram/utils/webhookhandler.py
+++ b/telegram/utils/webhookhandler.py
@@ -62,12 +62,17 @@ def serve_forever(self, poll_interval=0.5):
     def shutdown(self):
         with self.shutdown_lock:
             if not self.is_running:
-                self.logger.warn('Webhook Server already stopped.')
+                self.logger.warning('Webhook Server already stopped.')
                 return
             else:
                 super(WebhookServer, self).shutdown()
                 self.is_running = False
 
+    def handle_error(self, request, client_address):
+        """Handle an error gracefully."""
+        self.logger.debug('Exception happened during processing of request from %s',
+                          client_address, exc_info=True)
+
 
 # WebhookHandler, process webhook calls
 # Based on: https://github.com/eternnoir/pyTelegramBotAPI/blob/master/


Test patch: