We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
windwos raise OSError: [Errno 9] Bad file descriptor
the file ./utils/socket.py line 31 hava code `os.read(socket.fileno(), n)` but windows not support

my call container.exec_run("xxx", socket=False) raise:

Traceback (most recent call last):
  File "E:/projects/python/judgeClient/judge.py", line 34, in <module>
    ret, s = container.exec_run("javac Main.java", workdir="/judge", socket=False)
  File "D:\Python35\lib\site-packages\docker\models\containers.py", line 185, in exec_run
    resp['Id'], detach=detach, tty=tty, stream=stream, socket=socket
  File "D:\Python35\lib\site-packages\docker\utils\decorators.py", line 19, in wrapped
    return f(self, resource_id, *args, **kwargs)
  File "D:\Python35\lib\site-packages\docker\api\exec_api.py", line 165, in exec_start
    return self._read_from_socket(res, stream, tty)
  File "D:\Python35\lib\site-packages\docker\api\client.py", line 373, in _read_from_socket
    return six.binary_type().join(gen)
  File "D:\Python35\lib\site-packages\docker\utils\socket.py", line 75, in frames_iter
    n = next_frame_size(socket)
  File "D:\Python35\lib\site-packages\docker\utils\socket.py", line 62, in next_frame_size
    data = read_exactly(socket, 8)
  File "D:\Python35\lib\site-packages\docker\utils\socket.py", line 47, in read_exactly
    next_data = read(socket, n - len(data))
  File "D:\Python35\lib\site-packages\docker\utils\socket.py", line 32, in read
    return os.read(socket.fileno(), n)
OSError: [Errno 9] Bad file descriptor


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `docker/utils/socket.py`
Content:
```
1 import errno
2 import os
3 import select
4 import struct
5 
6 import six
7 
8 try:
9     from ..transport import NpipeSocket
10 except ImportError:
11     NpipeSocket = type(None)
12 
13 
14 class SocketError(Exception):
15     pass
16 
17 
18 def read(socket, n=4096):
19     """
20     Reads at most n bytes from socket
21     """
22 
23     recoverable_errors = (errno.EINTR, errno.EDEADLK, errno.EWOULDBLOCK)
24 
25     if six.PY3 and not isinstance(socket, NpipeSocket):
26         select.select([socket], [], [])
27 
28     try:
29         if hasattr(socket, 'recv'):
30             return socket.recv(n)
31         return os.read(socket.fileno(), n)
32     except EnvironmentError as e:
33         if e.errno not in recoverable_errors:
34             raise
35 
36 
37 def read_exactly(socket, n):
38     """
39     Reads exactly n bytes from socket
40     Raises SocketError if there isn't enough data
41     """
42     data = six.binary_type()
43     while len(data) < n:
44         next_data = read(socket, n - len(data))
45         if not next_data:
46             raise SocketError("Unexpected EOF")
47         data += next_data
48     return data
49 
50 
51 def next_frame_size(socket):
52     """
53     Returns the size of the next frame of data waiting to be read from socket,
54     according to the protocol defined here:
55 
56     https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/attach-to-a-container
57     """
58     try:
59         data = read_exactly(socket, 8)
60     except SocketError:
61         return -1
62 
63     _, actual = struct.unpack('>BxxxL', data)
64     return actual
65 
66 
67 def frames_iter(socket):
68     """
69     Returns a generator of frames read from socket
70     """
71     while True:
72         n = next_frame_size(socket)
73         if n < 0:
74             break
75         while n > 0:
76             result = read(socket, n)
77             if result is None:
78                 continue
79             data_length = len(result)
80             if data_length == 0:
81                 # We have reached EOF
82                 return
83             n -= data_length
84             yield result
85 
86 
87 def socket_raw_iter(socket):
88     """
89     Returns a generator of data read from the socket.
90     This is used for non-multiplexed streams.
91     """
92     while True:
93         result = read(socket)
94         if len(result) == 0:
95             # We have reached EOF
96             return
97         yield result
98
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/docker/utils/socket.py b/docker/utils/socket.py
--- a/docker/utils/socket.py
+++ b/docker/utils/socket.py
@@ -1,6 +1,7 @@
 import errno
 import os
 import select
+import socket as pysocket
 import struct
 
 import six
@@ -28,6 +29,8 @@
     try:
         if hasattr(socket, 'recv'):
             return socket.recv(n)
+        if six.PY3 and isinstance(socket, getattr(pysocket, 'SocketIO')):
+            return socket.read(n)
         return os.read(socket.fileno(), n)
     except EnvironmentError as e:
         if e.errno not in recoverable_errors:

{"golden_diff": "diff --git a/docker/utils/socket.py b/docker/utils/socket.py\n--- a/docker/utils/socket.py\n+++ b/docker/utils/socket.py\n@@ -1,6 +1,7 @@\n import errno\n import os\n import select\n+import socket as pysocket\n import struct\n \n import six\n@@ -28,6 +29,8 @@\n     try:\n         if hasattr(socket, 'recv'):\n             return socket.recv(n)\n+        if six.PY3 and isinstance(socket, getattr(pysocket, 'SocketIO')):\n+            return socket.read(n)\n         return os.read(socket.fileno(), n)\n     except EnvironmentError as e:\n         if e.errno not in recoverable_errors:\n", "issue": "windwos raise OSError: [Errno 9] Bad file descriptor\nthe file ./utils/socket.py line 31 hava code `os.read(socket.fileno(), n)` but windows not support\r\n\r\nmy call container.exec_run(\"xxx\", socket=False) raise:\r\n\r\nTraceback (most recent call last):\r\n  File \"E:/projects/python/judgeClient/judge.py\", line 34, in <module>\r\n    ret, s = container.exec_run(\"javac Main.java\", workdir=\"/judge\", socket=False)\r\n  File \"D:\\Python35\\lib\\site-packages\\docker\\models\\containers.py\", line 185, in exec_run\r\n    resp['Id'], detach=detach, tty=tty, stream=stream, socket=socket\r\n  File \"D:\\Python35\\lib\\site-packages\\docker\\utils\\decorators.py\", line 19, in wrapped\r\n    return f(self, resource_id, *args, **kwargs)\r\n  File \"D:\\Python35\\lib\\site-packages\\docker\\api\\exec_api.py\", line 165, in exec_start\r\n    return self._read_from_socket(res, stream, tty)\r\n  File \"D:\\Python35\\lib\\site-packages\\docker\\api\\client.py\", line 373, in _read_from_socket\r\n    return six.binary_type().join(gen)\r\n  File \"D:\\Python35\\lib\\site-packages\\docker\\utils\\socket.py\", line 75, in frames_iter\r\n    n = next_frame_size(socket)\r\n  File \"D:\\Python35\\lib\\site-packages\\docker\\utils\\socket.py\", line 62, in next_frame_size\r\n    data = read_exactly(socket, 8)\r\n  File \"D:\\Python35\\lib\\site-packages\\docker\\utils\\socket.py\", line 47, in read_exactly\r\n    next_data = read(socket, n - len(data))\r\n  File \"D:\\Python35\\lib\\site-packages\\docker\\utils\\socket.py\", line 32, in read\r\n    return os.read(socket.fileno(), n)\r\nOSError: [Errno 9] Bad file descriptor\r\n\n", "before_files": [{"content": "import errno\nimport os\nimport select\nimport struct\n\nimport six\n\ntry:\n    from ..transport import NpipeSocket\nexcept ImportError:\n    NpipeSocket = type(None)\n\n\nclass SocketError(Exception):\n    pass\n\n\ndef read(socket, n=4096):\n    \"\"\"\n    Reads at most n bytes from socket\n    \"\"\"\n\n    recoverable_errors = (errno.EINTR, errno.EDEADLK, errno.EWOULDBLOCK)\n\n    if six.PY3 and not isinstance(socket, NpipeSocket):\n        select.select([socket], [], [])\n\n    try:\n        if hasattr(socket, 'recv'):\n            return socket.recv(n)\n        return os.read(socket.fileno(), n)\n    except EnvironmentError as e:\n        if e.errno not in recoverable_errors:\n            raise\n\n\ndef read_exactly(socket, n):\n    \"\"\"\n    Reads exactly n bytes from socket\n    Raises SocketError if there isn't enough data\n    \"\"\"\n    data = six.binary_type()\n    while len(data) < n:\n        next_data = read(socket, n - len(data))\n        if not next_data:\n            raise SocketError(\"Unexpected EOF\")\n        data += next_data\n    return data\n\n\ndef next_frame_size(socket):\n    \"\"\"\n    Returns the size of the next frame of data waiting to be read from socket,\n    according to the protocol defined here:\n\n    https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/attach-to-a-container\n    \"\"\"\n    try:\n        data = read_exactly(socket, 8)\n    except SocketError:\n        return -1\n\n    _, actual = struct.unpack('>BxxxL', data)\n    return actual\n\n\ndef frames_iter(socket):\n    \"\"\"\n    Returns a generator of frames read from socket\n    \"\"\"\n    while True:\n        n = next_frame_size(socket)\n        if n < 0:\n            break\n        while n > 0:\n            result = read(socket, n)\n            if result is None:\n                continue\n            data_length = len(result)\n            if data_length == 0:\n                # We have reached EOF\n                return\n            n -= data_length\n            yield result\n\n\ndef socket_raw_iter(socket):\n    \"\"\"\n    Returns a generator of data read from the socket.\n    This is used for non-multiplexed streams.\n    \"\"\"\n    while True:\n        result = read(socket)\n        if len(result) == 0:\n            # We have reached EOF\n            return\n        yield result\n", "path": "docker/utils/socket.py"}], "after_files": [{"content": "import errno\nimport os\nimport select\nimport socket as pysocket\nimport struct\n\nimport six\n\ntry:\n    from ..transport import NpipeSocket\nexcept ImportError:\n    NpipeSocket = type(None)\n\n\nclass SocketError(Exception):\n    pass\n\n\ndef read(socket, n=4096):\n    \"\"\"\n    Reads at most n bytes from socket\n    \"\"\"\n\n    recoverable_errors = (errno.EINTR, errno.EDEADLK, errno.EWOULDBLOCK)\n\n    if six.PY3 and not isinstance(socket, NpipeSocket):\n        select.select([socket], [], [])\n\n    try:\n        if hasattr(socket, 'recv'):\n            return socket.recv(n)\n        if six.PY3 and isinstance(socket, getattr(pysocket, 'SocketIO')):\n            return socket.read(n)\n        return os.read(socket.fileno(), n)\n    except EnvironmentError as e:\n        if e.errno not in recoverable_errors:\n            raise\n\n\ndef read_exactly(socket, n):\n    \"\"\"\n    Reads exactly n bytes from socket\n    Raises SocketError if there isn't enough data\n    \"\"\"\n    data = six.binary_type()\n    while len(data) < n:\n        next_data = read(socket, n - len(data))\n        if not next_data:\n            raise SocketError(\"Unexpected EOF\")\n        data += next_data\n    return data\n\n\ndef next_frame_size(socket):\n    \"\"\"\n    Returns the size of the next frame of data waiting to be read from socket,\n    according to the protocol defined here:\n\n    https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/attach-to-a-container\n    \"\"\"\n    try:\n        data = read_exactly(socket, 8)\n    except SocketError:\n        return -1\n\n    _, actual = struct.unpack('>BxxxL', data)\n    return actual\n\n\ndef frames_iter(socket):\n    \"\"\"\n    Returns a generator of frames read from socket\n    \"\"\"\n    while True:\n        n = next_frame_size(socket)\n        if n < 0:\n            break\n        while n > 0:\n            result = read(socket, n)\n            if result is None:\n                continue\n            data_length = len(result)\n            if data_length == 0:\n                # We have reached EOF\n                return\n            n -= data_length\n            yield result\n\n\ndef socket_raw_iter(socket):\n    \"\"\"\n    Returns a generator of data read from the socket.\n    This is used for non-multiplexed streams.\n    \"\"\"\n    while True:\n        result = read(socket)\n        if len(result) == 0:\n            # We have reached EOF\n            return\n        yield result\n", "path": "docker/utils/socket.py"}]}