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`


Discussion:

Resolving patch:
diff --git a/docker/api/container.py b/docker/api/container.py
--- a/docker/api/container.py
+++ b/docker/api/container.py
@@ -1018,7 +1018,10 @@ def restart(self, container, timeout=10):
         """
         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 @@ def stop(self, container, timeout=None):
         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
--- a/tests/integration/api_container_test.py
+++ b/tests/integration/api_container_test.py
@@ -1165,6 +1165,15 @@ def test_restart(self):
         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
--- a/tests/unit/api_container_test.py
+++ b/tests/unit/api_container_test.py
@@ -1335,7 +1335,7 @@ def test_restart_container(self):
             '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 @@ def test_restart_container_with_dict_instead_of_id(self):
             'POST',
             url_prefix + 'containers/3cc2351ab11b/restart',
             params={'t': 2},
-            timeout=DEFAULT_TIMEOUT_SECONDS
+            timeout=(DEFAULT_TIMEOUT_SECONDS + 2)
         )
 
     def test_remove_container(self):