Repository: docker/docker-py

Issue:
ContainerApiMixin::restart(timeout=...) uses default read timeout which results in ReadTimeout exception
ReadTimeout is thrown when you restart or stop a container with a high timeout (e.g. 120sec) in case when the container needs over 60 seconds to stop. This requires users to write code like this:
```
try:
    my_container.client.api.timeout = 130
    my_container.restart(timeout=110)
finally:
    my_container.client.api.timeout = docker.constants.DEFAULT_TIMEOUT_SECONDS
```
or write their own `restart` method.

IMO better solution would be to use read timeout that is `max(docker.constants.DEFAULT_TIMEOUT_SECONDS, user_timeout + SOME_CONSTANT)`, because when someone stops a container with specified timeout (`user_timeout`) it's expected that there might be no data to read from the socket for at least `user_timeout`

Comments:

Patch:
diff --git a/docker/api/config.py b/docker/api/config.py
index b46b09c7..767bef26 100644
--- a/docker/api/config.py
+++ b/docker/api/config.py
@@ -6,7 +6,7 @@ from .. import utils
 
 
 class ConfigApiMixin(object):
-    @utils.minimum_version('1.25')
+    @utils.minimum_version('1.30')
     def create_config(self, name, data, labels=None):
         """
             Create a config
@@ -35,7 +35,7 @@ class ConfigApiMixin(object):
             self._post_json(url, data=body), True
         )
 
-    @utils.minimum_version('1.25')
+    @utils.minimum_version('1.30')
     @utils.check_resource('id')
     def inspect_config(self, id):
         """
@@ -53,7 +53,7 @@ class ConfigApiMixin(object):
         url = self._url('/configs/{0}', id)
         return self._result(self._get(url), True)
 
-    @utils.minimum_version('1.25')
+    @utils.minimum_version('1.30')
     @utils.check_resource('id')
     def remove_config(self, id):
         """
@@ -73,7 +73,7 @@ class ConfigApiMixin(object):
         self._raise_for_status(res)
         return True
 
-    @utils.minimum_version('1.25')
+    @utils.minimum_version('1.30')
     def configs(self, filters=None):
         """
             List configs
diff --git a/docker/api/container.py b/docker/api/container.py
index cb97b794..05676f11 100644
--- a/docker/api/container.py
+++ b/docker/api/container.py
@@ -1018,7 +1018,10 @@ class ContainerApiMixin(object):
         """
         params = {'t': timeout}
         url = self._url("/containers/{0}/restart", container)
-        res = self._post(url, params=params)
+        conn_timeout = self.timeout
+        if conn_timeout is not None:
+            conn_timeout += timeout
+        res = self._post(url, params=params, timeout=conn_timeout)
         self._raise_for_status(res)
 
     @utils.check_resource('container')
@@ -1107,9 +1110,10 @@ class ContainerApiMixin(object):
         else:
             params = {'t': timeout}
         url = self._url("/containers/{0}/stop", container)
-
-        res = self._post(url, params=params,
-                         timeout=(timeout + (self.timeout or 0)))
+        conn_timeout = self.timeout
+        if conn_timeout is not None:
+            conn_timeout += timeout
+        res = self._post(url, params=params, timeout=conn_timeout)
         self._raise_for_status(res)
 
     @utils.check_resource('container')


Test patch:
diff --git a/tests/integration/api_container_test.py b/tests/integration/api_container_test.py
index e2125186..afd439f9 100644
--- a/tests/integration/api_container_test.py
+++ b/tests/integration/api_container_test.py
@@ -1165,6 +1165,15 @@ class RestartContainerTest(BaseAPIIntegrationTest):
         assert info2['State']['Running'] is True
         self.client.kill(id)
 
+    def test_restart_with_low_timeout(self):
+        container = self.client.create_container(BUSYBOX, ['sleep', '9999'])
+        self.client.start(container)
+        self.client.timeout = 1
+        self.client.restart(container, timeout=3)
+        self.client.timeout = None
+        self.client.restart(container, timeout=3)
+        self.client.kill(container)
+
     def test_restart_with_dict_instead_of_id(self):
         container = self.client.create_container(BUSYBOX, ['sleep', '9999'])
         assert 'Id' in container
diff --git a/tests/unit/api_container_test.py b/tests/unit/api_container_test.py
index c33f129e..a7e183c8 100644
--- a/tests/unit/api_container_test.py
+++ b/tests/unit/api_container_test.py
@@ -1335,7 +1335,7 @@ class ContainerTest(BaseAPIClientTest):
             'POST',
             url_prefix + 'containers/3cc2351ab11b/restart',
             params={'t': 2},
-            timeout=DEFAULT_TIMEOUT_SECONDS
+            timeout=(DEFAULT_TIMEOUT_SECONDS + 2)
         )
 
     def test_restart_container_with_dict_instead_of_id(self):
@@ -1345,7 +1345,7 @@ class ContainerTest(BaseAPIClientTest):
             'POST',
             url_prefix + 'containers/3cc2351ab11b/restart',
             params={'t': 2},
-            timeout=DEFAULT_TIMEOUT_SECONDS
+            timeout=(DEFAULT_TIMEOUT_SECONDS + 2)
         )
 
     def test_remove_container(self):