Repository: urllib3/urllib3

Issue:
Creating socket to check if IPv6 is enabled on AppEngine
Module `urlib3/util/connection.py` executes `_has_ipv6` function while being imported. For checking if IPv6 is enabled this function is creating socket:

            sock = socket.socket(socket.AF_INET6)
            sock.bind((host, 0))
            has_ipv6 = True

From documentation: https://urllib3.readthedocs.io/en/latest/reference/urllib3.contrib.html using socket should be enabled only if `GAE_USE_SOCKETS_HTTPLIB` is set to `true`. So before calling that function there should be check if we want to use sockets on App Engine.


Discussion:
@apietrzak I'm not sure I understand. Can you give me a bit more details? Does this cause failures? Does it incorrectly detect ipv6 support?
@theacodes 
On appengine you have a daily quota on creating sockets: https://cloud.google.com/appengine/quotas#Sockets 

As you can see in this documentation page:
https://urllib3.readthedocs.io/en/latest/reference/urllib3.contrib.html
there is a way to use urllib3 without sockets. For this purpose  an additional "pool manager" was created which is using URLfetch api:
```
from urllib3.contrib.appengine import AppEngineManager, is_appengine_sandbox
if is_appengine_sandbox():
    # AppEngineManager uses AppEngine's URLFetch API behind the scenes
    http = AppEngineManager()
else:
    # PoolManager uses a socket-level API behind the scenes
    http = PoolManager()

```

The code above always creates a socket. You can check it by adding for example `import pdb;pdb.set_trace()` to util/connections.py code. 
So, unfortunately for us, this is a bug. Additionaly this check is for each machine on app engine (Only import urllib3 runs the checking). App engine is a cloud service, so the machines frequently are spawned and killed.

Example code:
![image](https://user-images.githubusercontent.com/20789218/46474294-c7988d00-c7e2-11e8-8a6a-79059757ef06.png)
And logs:
![image](https://user-images.githubusercontent.com/20789218/46474319-dbdc8a00-c7e2-11e8-947c-dc3d67ad7908.png)

Gotcha, that makes sense. Thank you for clarifying. 
I'm not sure if I can do anything here. We make a bit of a mistake in the interface here.

`HAS_IPV6` is a global constant in a public module that has existed for years. We can't make it lazy (because it's a global constant) and we can't really do anything in `contrib.appengine` to interrupt it, as the import for that module happens way before `contrib.appengine` has any say in the process.

We could figure out how to use some of the environment detection logic in `contrib.appengine` within `_is_ipv6()` to short-circuit the socket stuff. But it feels super gross.

Resolving patch:
diff --git a/src/urllib3/contrib/_appengine_environ.py b/src/urllib3/contrib/_appengine_environ.py
new file mode 100644
--- /dev/null
+++ b/src/urllib3/contrib/_appengine_environ.py
@@ -0,0 +1,30 @@
+"""
+This module provides means to detect the App Engine environment.
+"""
+
+import os
+
+
+def is_appengine():
+    return (is_local_appengine() or
+            is_prod_appengine() or
+            is_prod_appengine_mvms())
+
+
+def is_appengine_sandbox():
+    return is_appengine() and not is_prod_appengine_mvms()
+
+
+def is_local_appengine():
+    return ('APPENGINE_RUNTIME' in os.environ and
+            'Development/' in os.environ['SERVER_SOFTWARE'])
+
+
+def is_prod_appengine():
+    return ('APPENGINE_RUNTIME' in os.environ and
+            'Google App Engine/' in os.environ['SERVER_SOFTWARE'] and
+            not is_prod_appengine_mvms())
+
+
+def is_prod_appengine_mvms():
+    return os.environ.get('GAE_VM', False) == 'true'
diff --git a/src/urllib3/contrib/appengine.py b/src/urllib3/contrib/appengine.py
--- a/src/urllib3/contrib/appengine.py
+++ b/src/urllib3/contrib/appengine.py
@@ -41,7 +41,6 @@
 from __future__ import absolute_import
 import io
 import logging
-import os
 import warnings
 from ..packages.six.moves.urllib.parse import urljoin
 
@@ -58,6 +57,7 @@
 from ..response import HTTPResponse
 from ..util.timeout import Timeout
 from ..util.retry import Retry
+from . import _appengine_environ
 
 try:
     from google.appengine.api import urlfetch
@@ -280,26 +280,10 @@ def _get_retries(self, retries, redirect):
         return retries
 
 
-def is_appengine():
-    return (is_local_appengine() or
-            is_prod_appengine() or
-            is_prod_appengine_mvms())
+# Alias methods from _appengine_environ to maintain public API interface.
 
-
-def is_appengine_sandbox():
-    return is_appengine() and not is_prod_appengine_mvms()
-
-
-def is_local_appengine():
-    return ('APPENGINE_RUNTIME' in os.environ and
-            'Development/' in os.environ['SERVER_SOFTWARE'])
-
-
-def is_prod_appengine():
-    return ('APPENGINE_RUNTIME' in os.environ and
-            'Google App Engine/' in os.environ['SERVER_SOFTWARE'] and
-            not is_prod_appengine_mvms())
-
-
-def is_prod_appengine_mvms():
-    return os.environ.get('GAE_VM', False) == 'true'
+is_appengine = _appengine_environ.is_appengine
+is_appengine_sandbox = _appengine_environ.is_appengine_sandbox
+is_local_appengine = _appengine_environ.is_local_appengine
+is_prod_appengine = _appengine_environ.is_prod_appengine
+is_prod_appengine_mvms = _appengine_environ.is_prod_appengine_mvms
diff --git a/src/urllib3/util/connection.py b/src/urllib3/util/connection.py
--- a/src/urllib3/util/connection.py
+++ b/src/urllib3/util/connection.py
@@ -1,6 +1,7 @@
 from __future__ import absolute_import
 import socket
 from .wait import NoWayToWaitForSocketError, wait_for_read
+from ..contrib import _appengine_environ
 
 
 def is_connection_dropped(conn):  # Platform-specific
@@ -105,6 +106,13 @@ def _has_ipv6(host):
     sock = None
     has_ipv6 = False
 
+    # App Engine doesn't support IPV6 sockets and actually has a quota on the
+    # number of sockets that can be used, so just early out here instead of
+    # creating a socket needlessly.
+    # See https://github.com/urllib3/urllib3/issues/1446
+    if _appengine_environ.is_appengine_sandbox():
+        return False
+
     if socket.has_ipv6:
         # has_ipv6 returns true if cPython was compiled with IPv6 support.
         # It does not tell us if the system has IPv6 support enabled. To


Test patch:
diff --git a/test/test_util.py b/test/test_util.py
--- a/test/test_util.py
+++ b/test/test_util.py
@@ -540,6 +540,13 @@ def test_has_ipv6_enabled_and_working(self):
                 instance.bind.return_value = True
                 assert _has_ipv6('::1')
 
+    def test_has_ipv6_disabled_on_appengine(self):
+        gae_patch = patch(
+            'urllib3.contrib._appengine_environ.is_appengine_sandbox',
+            return_value=True)
+        with gae_patch:
+            assert not _has_ipv6('::1')
+
     def test_ip_family_ipv6_enabled(self):
         with patch('urllib3.util.connection.HAS_IPV6', True):
             assert allowed_gai_family() == socket.AF_UNSPEC