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

Issue:
Bot stops responding 
<!--
Thanks for reporting issues of python-telegram-bot!
To make it easier for us to help you please enter detailed information below.

Please note, we only support the latest version of python-telegram-bot and
master branch. Please make sure to upgrade & recreate the issue on the latest
version prior to opening an issue.
-->
### Steps to reproduce
1. Run the bot, type /start

2. Send your message, but don't end the conversation so it is waiting in some state

3. Wait for ~ 10 minutes

### Expected behaviour
Bot should answer when I get back and write something

### Actual behaviour
Bot does not answer instantly, but after certain period of time

### Configuration
Microsoft Azure Cloud
Ubuntu 16.04



**Version of Python, python-telegram-bot & dependencies:**

``python-telegram-bot 5.3.0``
``urllib3 1.19.1``
``certifi 2016.09.26``
``future 0.16.0``
``Python 2.7.12 (default, Jul 18 2016, 15:02:52)  [GCC 4.8.4]``


### Logs

> Profiler-0 (err): 2017-02-28 08:23 +00:00: /usr/local/lib/python2.7/dist-packages/sklearn/cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
Profiler-0 (err):   "This module will be removed in 0.20.", DeprecationWarning)
Profiler-0 (err): 2017-02-28 08:51 +00:00: 2017-02-28 08:51:58,823 - urllib3.connectionpool - WARNING - Retrying (Retry(total=2, connect=None, read=None, redirect=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='api.telegram.org', port=443): Read timed out. (read timeout=<object object at 0x7fa6aa9590b0>)",)': /AAAAAAAAAAAAA:AAAAAAAAAAA/sendMessage



Discussion:
I have never experienced this kind of trouble, is it possible you're internet connection is not fully stable? Or maybe it has to do with #533 or #510 
I don't think it is a connection problem, because I host it on Azure Cloud Enterprise level machine.
I have tcpdump-ed all traffic and found no problems after analyzing it.
I have reproduced it on 2 machines and even on Docker container.
Seems like #533 or #510 is not my case.

The problem is still there. I have no idea what causes it
@maxevilmind 
Due to lack of better evidence we still suspect that the issue is actually #510 .
Since that issue is resolved, would you mind testing again your code against our latest version from pip (6.1.0)?
If you're still experiencing problems, would you mind trying to recreate the issue using the test script as discussed in the following comment: https://github.com/python-telegram-bot/python-telegram-bot/issues/510#issuecomment-305971298

Thank you for your patience.
@maxevilmind
I have also been facing this issue on my Azure VM. This problem is due to Azure VMs killing idle TCP/HTTP connection after 4 mins (by default. It can be set to up to 30 mins). You can keep the connection alive by changing your OS settings.

source: [https://github.com/MicrosoftDocs/azure-docs/blob/master/includes/guidance-tcp-session-timeout-include.md](https://github.com/MicrosoftDocs/azure-docs/blob/master/includes/guidance-tcp-session-timeout-include.md)

Add the following to your /etc/sysctl.conf:
```
net.ipv4.tcp_keepalive_time = 120
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 8
```

I have also written a guide [here](https://codesandbolts.com/azure-vm-tcp-settings/) if anyone facing this problem need more help in changing the settings.

Resolving patch:
diff --git a/telegram/utils/request.py b/telegram/utils/request.py
--- a/telegram/utils/request.py
+++ b/telegram/utils/request.py
@@ -19,6 +19,7 @@
 """This module contains methods to make POST and GET requests"""
 import os
 import socket
+import sys
 import logging
 import warnings
 
@@ -76,13 +77,20 @@ def __init__(self,
 
         self._connect_timeout = connect_timeout
 
+        sockopts = HTTPConnection.default_socket_options + [
+            (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)]
+
+        # TODO: Support other platforms like mac and windows.
+        if 'linux' in sys.platform:
+            sockopts.append((socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 120))
+            sockopts.append((socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 30))
+            sockopts.append((socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 8))
+
         kwargs = dict(
             maxsize=con_pool_size,
             cert_reqs='CERT_REQUIRED',
             ca_certs=certifi.where(),
-            socket_options=HTTPConnection.default_socket_options + [
-                (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),
-            ],
+            socket_options=sockopts,
             timeout=urllib3.Timeout(
                 connect=self._connect_timeout, read=read_timeout, total=None))
 


Test patch: