We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
nosetests crashes under IPv4 (error: getsockaddrarg: bad family)
Turns out tornado is really eager to use IPv6. Unless you expressly hand the server the address, it doesn't even check for socket IPv6 support. I'll submit a pull request for the one-line fix in dummyserver/server.py momentarily.

Source: https://groups.google.com/group/python-tornado/browse_thread/thread/3ec04536e57a2833?pli=1


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `dummyserver/server.py`
Content:
```
1 #!/usr/bin/env python
2 
3 """
4 Dummy server used for unit testing.
5 """
6 from __future__ import print_function
7 
8 import logging
9 import os
10 import sys
11 import threading
12 import socket
13 
14 import tornado.wsgi
15 import tornado.httpserver
16 import tornado.ioloop
17 
18 from dummyserver.handlers import TestingApp
19 
20 
21 log = logging.getLogger(__name__)
22 
23 CERTS_PATH = os.path.join(os.path.dirname(__file__), 'certs')
24 DEFAULT_CERTS = {
25     'certfile': os.path.join(CERTS_PATH, 'server.crt'),
26     'keyfile': os.path.join(CERTS_PATH, 'server.key'),
27 }
28 DEFAULT_CA = os.path.join(CERTS_PATH, 'cacert.pem')
29 DEFAULT_CA_BAD = os.path.join(CERTS_PATH, 'client_bad.pem')
30 
31 
32 # Different types of servers we have:
33 
34 
35 class SocketServerThread(threading.Thread):
36     """
37     :param socket_handler: Callable which receives a socket argument for one
38         request.
39     :param ready_lock: Lock which gets released when the socket handler is
40         ready to receive requests.
41     """
42     def __init__(self, socket_handler, host='localhost', port=8081,
43                  ready_lock=None):
44         threading.Thread.__init__(self)
45 
46         self.socket_handler = socket_handler
47         self.host = host
48         self.port = port
49         self.ready_lock = ready_lock
50 
51     def _start_server(self):
52         sock = socket.socket()
53         sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
54         sock.bind((self.host, self.port))
55 
56         # Once listen() returns, the server socket is ready
57         sock.listen(1)
58 
59         if self.ready_lock:
60             self.ready_lock.release()
61 
62         self.socket_handler(sock)
63 
64     def run(self):
65         self.server = self._start_server()
66 
67 
68 class TornadoServerThread(threading.Thread):
69     def __init__(self, host='localhost', port=8081, scheme='http', certs=None):
70         threading.Thread.__init__(self)
71 
72         self.host = host
73         self.port = port
74         self.scheme = scheme
75         self.certs = certs
76 
77     def _start_server(self):
78         container = tornado.wsgi.WSGIContainer(TestingApp())
79 
80         if self.scheme == 'https':
81             http_server = tornado.httpserver.HTTPServer(container,
82                                                         ssl_options=self.certs)
83         else:
84             http_server = tornado.httpserver.HTTPServer(container)
85 
86         http_server.listen(self.port)
87         return http_server
88 
89     def run(self):
90         self.server = self._start_server()
91         self.ioloop = tornado.ioloop.IOLoop.instance()
92         self.ioloop.start()
93 
94     def stop(self):
95         self.server.stop()
96         self.ioloop.stop()
97 
98 
99 if __name__ == '__main__':
100     log.setLevel(logging.DEBUG)
101     log.addHandler(logging.StreamHandler(sys.stderr))
102 
103     from urllib3 import get_host
104 
105     url = "http://localhost:8081"
106     if len(sys.argv) > 1:
107         url = sys.argv[1]
108 
109     print("Starting WGI server at: %s" % url)
110 
111     scheme, host, port = get_host(url)
112     t = TornadoServerThread(scheme=scheme, host=host, port=port)
113     t.start()
114
```
--- 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/dummyserver/server.py b/dummyserver/server.py
--- a/dummyserver/server.py
+++ b/dummyserver/server.py
@@ -83,7 +83,7 @@
         else:
             http_server = tornado.httpserver.HTTPServer(container)
 
-        http_server.listen(self.port)
+        http_server.listen(self.port, address=self.host)
         return http_server
 
     def run(self):
@@ -106,7 +106,7 @@
     if len(sys.argv) > 1:
         url = sys.argv[1]
 
-    print("Starting WGI server at: %s" % url)
+    print("Starting WSGI server at: %s" % url)
 
     scheme, host, port = get_host(url)
     t = TornadoServerThread(scheme=scheme, host=host, port=port)

{"golden_diff": "diff --git a/dummyserver/server.py b/dummyserver/server.py\n--- a/dummyserver/server.py\n+++ b/dummyserver/server.py\n@@ -83,7 +83,7 @@\n         else:\n             http_server = tornado.httpserver.HTTPServer(container)\n \n-        http_server.listen(self.port)\n+        http_server.listen(self.port, address=self.host)\n         return http_server\n \n     def run(self):\n@@ -106,7 +106,7 @@\n     if len(sys.argv) > 1:\n         url = sys.argv[1]\n \n-    print(\"Starting WGI server at: %s\" % url)\n+    print(\"Starting WSGI server at: %s\" % url)\n \n     scheme, host, port = get_host(url)\n     t = TornadoServerThread(scheme=scheme, host=host, port=port)\n", "issue": "nosetests crashes under IPv4 (error: getsockaddrarg: bad family)\nTurns out tornado is really eager to use IPv6. Unless you expressly hand the server the address, it doesn't even check for socket IPv6 support. I'll submit a pull request for the one-line fix in dummyserver/server.py momentarily.\n\nSource: https://groups.google.com/group/python-tornado/browse_thread/thread/3ec04536e57a2833?pli=1\n\n", "before_files": [{"content": "#!/usr/bin/env python\n\n\"\"\"\nDummy server used for unit testing.\n\"\"\"\nfrom __future__ import print_function\n\nimport logging\nimport os\nimport sys\nimport threading\nimport socket\n\nimport tornado.wsgi\nimport tornado.httpserver\nimport tornado.ioloop\n\nfrom dummyserver.handlers import TestingApp\n\n\nlog = logging.getLogger(__name__)\n\nCERTS_PATH = os.path.join(os.path.dirname(__file__), 'certs')\nDEFAULT_CERTS = {\n    'certfile': os.path.join(CERTS_PATH, 'server.crt'),\n    'keyfile': os.path.join(CERTS_PATH, 'server.key'),\n}\nDEFAULT_CA = os.path.join(CERTS_PATH, 'cacert.pem')\nDEFAULT_CA_BAD = os.path.join(CERTS_PATH, 'client_bad.pem')\n\n\n# Different types of servers we have:\n\n\nclass SocketServerThread(threading.Thread):\n    \"\"\"\n    :param socket_handler: Callable which receives a socket argument for one\n        request.\n    :param ready_lock: Lock which gets released when the socket handler is\n        ready to receive requests.\n    \"\"\"\n    def __init__(self, socket_handler, host='localhost', port=8081,\n                 ready_lock=None):\n        threading.Thread.__init__(self)\n\n        self.socket_handler = socket_handler\n        self.host = host\n        self.port = port\n        self.ready_lock = ready_lock\n\n    def _start_server(self):\n        sock = socket.socket()\n        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n        sock.bind((self.host, self.port))\n\n        # Once listen() returns, the server socket is ready\n        sock.listen(1)\n\n        if self.ready_lock:\n            self.ready_lock.release()\n\n        self.socket_handler(sock)\n\n    def run(self):\n        self.server = self._start_server()\n\n\nclass TornadoServerThread(threading.Thread):\n    def __init__(self, host='localhost', port=8081, scheme='http', certs=None):\n        threading.Thread.__init__(self)\n\n        self.host = host\n        self.port = port\n        self.scheme = scheme\n        self.certs = certs\n\n    def _start_server(self):\n        container = tornado.wsgi.WSGIContainer(TestingApp())\n\n        if self.scheme == 'https':\n            http_server = tornado.httpserver.HTTPServer(container,\n                                                        ssl_options=self.certs)\n        else:\n            http_server = tornado.httpserver.HTTPServer(container)\n\n        http_server.listen(self.port)\n        return http_server\n\n    def run(self):\n        self.server = self._start_server()\n        self.ioloop = tornado.ioloop.IOLoop.instance()\n        self.ioloop.start()\n\n    def stop(self):\n        self.server.stop()\n        self.ioloop.stop()\n\n\nif __name__ == '__main__':\n    log.setLevel(logging.DEBUG)\n    log.addHandler(logging.StreamHandler(sys.stderr))\n\n    from urllib3 import get_host\n\n    url = \"http://localhost:8081\"\n    if len(sys.argv) > 1:\n        url = sys.argv[1]\n\n    print(\"Starting WGI server at: %s\" % url)\n\n    scheme, host, port = get_host(url)\n    t = TornadoServerThread(scheme=scheme, host=host, port=port)\n    t.start()\n", "path": "dummyserver/server.py"}], "after_files": [{"content": "#!/usr/bin/env python\n\n\"\"\"\nDummy server used for unit testing.\n\"\"\"\nfrom __future__ import print_function\n\nimport logging\nimport os\nimport sys\nimport threading\nimport socket\n\nimport tornado.wsgi\nimport tornado.httpserver\nimport tornado.ioloop\n\nfrom dummyserver.handlers import TestingApp\n\n\nlog = logging.getLogger(__name__)\n\nCERTS_PATH = os.path.join(os.path.dirname(__file__), 'certs')\nDEFAULT_CERTS = {\n    'certfile': os.path.join(CERTS_PATH, 'server.crt'),\n    'keyfile': os.path.join(CERTS_PATH, 'server.key'),\n}\nDEFAULT_CA = os.path.join(CERTS_PATH, 'cacert.pem')\nDEFAULT_CA_BAD = os.path.join(CERTS_PATH, 'client_bad.pem')\n\n\n# Different types of servers we have:\n\n\nclass SocketServerThread(threading.Thread):\n    \"\"\"\n    :param socket_handler: Callable which receives a socket argument for one\n        request.\n    :param ready_lock: Lock which gets released when the socket handler is\n        ready to receive requests.\n    \"\"\"\n    def __init__(self, socket_handler, host='localhost', port=8081,\n                 ready_lock=None):\n        threading.Thread.__init__(self)\n\n        self.socket_handler = socket_handler\n        self.host = host\n        self.port = port\n        self.ready_lock = ready_lock\n\n    def _start_server(self):\n        sock = socket.socket()\n        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n        sock.bind((self.host, self.port))\n\n        # Once listen() returns, the server socket is ready\n        sock.listen(1)\n\n        if self.ready_lock:\n            self.ready_lock.release()\n\n        self.socket_handler(sock)\n\n    def run(self):\n        self.server = self._start_server()\n\n\nclass TornadoServerThread(threading.Thread):\n    def __init__(self, host='localhost', port=8081, scheme='http', certs=None):\n        threading.Thread.__init__(self)\n\n        self.host = host\n        self.port = port\n        self.scheme = scheme\n        self.certs = certs\n\n    def _start_server(self):\n        container = tornado.wsgi.WSGIContainer(TestingApp())\n\n        if self.scheme == 'https':\n            http_server = tornado.httpserver.HTTPServer(container,\n                                                        ssl_options=self.certs)\n        else:\n            http_server = tornado.httpserver.HTTPServer(container)\n\n        http_server.listen(self.port, address=self.host)\n        return http_server\n\n    def run(self):\n        self.server = self._start_server()\n        self.ioloop = tornado.ioloop.IOLoop.instance()\n        self.ioloop.start()\n\n    def stop(self):\n        self.server.stop()\n        self.ioloop.stop()\n\n\nif __name__ == '__main__':\n    log.setLevel(logging.DEBUG)\n    log.addHandler(logging.StreamHandler(sys.stderr))\n\n    from urllib3 import get_host\n\n    url = \"http://localhost:8081\"\n    if len(sys.argv) > 1:\n        url = sys.argv[1]\n\n    print(\"Starting WSGI server at: %s\" % url)\n\n    scheme, host, port = get_host(url)\n    t = TornadoServerThread(scheme=scheme, host=host, port=port)\n    t.start()\n", "path": "dummyserver/server.py"}]}