title: Python program hangs forever when called from subprocess
body: <p>The pip test suite employs subprocess calls to run integration tests. Recently a PR was placed which removed some older compatability code. Specically it replaced a <code>b()</code> function with explicitly uses of the <code>b""</code> literal. However this has seemingly broken something to where a particular subprocess call will hang forever. To make matters worse it only hangs forever on Python 3.3 (maybe only Python 3.3.5) and it cannot easily be reproduced outside of Travis.</p>
  
  <p>Relevant Pull Requests:</p>
  
  <ul>
  <li><a href="https://github.com/pypa/pip/pull/1901">https://github.com/pypa/pip/pull/1901</a></li>
  <li><a href="https://github.com/pypa/pip/pull/1900">https://github.com/pypa/pip/pull/1900</a></li>
  <li><a href="https://github.com/pypa/pip/pull/1878">https://github.com/pypa/pip/pull/1878</a></li>
  </ul>
  
  <p>A similar problem occurs with other Pull Requests, however they fail on different versions of Python and different test cases. These Pull Requests are:</p>
  
  <ul>
  <li><a href="https://github.com/pypa/pip/pull/1882">https://github.com/pypa/pip/pull/1882</a></li>
  <li><a href="https://github.com/pypa/pip/pull/1912">https://github.com/pypa/pip/pull/1912</a> (Python 3.3 again)</li>
  </ul>
  
  <p>Another user has reported a similar issue to me today in IRC, they say they can reproduce it locally on Ubuntu 14.04 with Python 3.3 from deadsnakes (but not on OSX) and not only on Travis like I've mostly been able too thus far. They've sent me steps to reproduce which are:</p>
  
  <pre class="lang-sh prettyprint-override"><code>$ git clone git@github.com:xavfernandez/pip.git
  $ cd pip
  $ git checkout debug_stuck
  $ pip install pytest==2.5.2 scripttest==1.3 virtualenv==1.11.6 mock==1.0.1 pretend==1.0.8 setuptools==4.0
  $ # The below should pass just fine
  $ py.test -k test_env_vars_override_config_file -v -s
  $ # Now edit pip/req/req_set.py and remove method remove_me_to_block or change its content to print('KO') or pass
  $ # The below should hang forever
  $ py.test -k test_env_vars_override_config_file -v -s
  </code></pre>
  
  <p>In the above example, the <code>remove_me_to_block</code> method is not called anywhere, just the mere existence of it is enough to make the test not block, and the non existence of it (or changing it's contents) is enough to make the test block forever.</p>
  
  <p>Most of the debugging has been with the changes in this PR (<a href="https://github.com/pypa/pip/pull/1901">https://github.com/pypa/pip/pull/1901</a>). Having pushed one commit at a time the tests passed until this particular commit was applied - <a href="https://github.com/dstufft/pip/commit/d296df620916b4cd2379d9fab988cbc088e28fe0">https://github.com/dstufft/pip/commit/d296df620916b4cd2379d9fab988cbc088e28fe0</a>. Specifically either the change to use <code>b'\r\n'</code> or <code>(entry + endline).encode("utf-8")</code> will trigger it, however neither of these things are in the execution path for <code>pip install -vvv INITools</code> which is the command that it fails being able to execute.</p>
  
  <p>In attempting to trace down the problem I've noticed that if I replace at least one call to <code>"something".encode("utf8")</code> with <code>(lambda: "something")().encode("utf8")</code> it works.</p>
  
  <p>Another issue while attempting to debug this, has been that various things I've tried (adding print statements, no-op <code>atexit</code> functions, using trollious for async subprocess) will simply shift the problem from a particular test case on a particular Python version to different test cases on different versions of Python.</p>
  
  <p>I am aware of the fact that the <code>subprocess</code> module can deadlock if you read/write from <code>subprocess.Popen().stdout/stderr/stdin</code> directly. However This code is using the <code>communicate()</code> method which is supposed to work around these issues. It is inside of the <code>wait()</code> call that <code>communicate()</code> does that the process hangs forever waiting for the pip process to exit.</p>
  
  <p>Other information:</p>
  
  <ul>
  <li>It is very heisenbug-ey, I've managed to make it go away or shift based on various things that should not have any affect on it.</li>
  <li>I've traced the execution inside of pip itself all the way through to the end of the code paths until <code>sys.exit()</code> is called.</li>
  <li>Replacing <code>sys.exit()</code> with <code>os._exit()</code> fixes all the hanging issues, however I'd rather not do that as we're then skipping the clean up that the Python interpreter does.</li>
  <li>There are no additional threads running (verified with <code>threading.enumerate</code>).</li>
  <li>I've had some combination of changes which have had it hang even without <code>subprocess.PIPE</code> being used for stdout/stderr/stdin, however other combinations will have it <em>not</em> hang if those are not used (or it'll shift to a different test case/python version).</li>
  <li>It does not appear to be timing related, any particular commit will either fail 100% of the time on the affect test cases/Pythons or fail 0% of the time.</li>
  <li>Often times the code that was changed isn't even being executed by that particular code path in the pip subprocess, however the mere existence of the change seems to break it.</li>
  <li>I've tried disabling bytecode generation using <code>PYTHONDONTWRITEBYTECODE=1</code> and that had an effect in one combination, but in others it's had no effect.</li>
  <li>The command that the subprocess calls does <em>not</em> hang in every invocation (similar commands are issued through the test suite) however it does always hang in the exact same place for a particular commit.</li>
  <li>So far i've been completely unable to reproduce this outside of being called via <code>subproccess</code> in the test suite, however I don't know for a fact if it is or isn't related to that.</li>
  </ul>
  
  <p>I'm completely at a loss for what could be causing this.</p>
  
  <p><strong>UPDATE #1</strong></p>
  
  <p>Using <code>faulthandler.dump_traceback_later()</code> I got this result:</p>
  
  <pre><code>Timeout (0:00:05)!
  Current thread 0x00007f417bd92740:
    File "/tmp/pytest-10/test_env_vars_override_config_file0/pip_src/pip/_vendor/cachecontrol/filewrapper.py", line 24 in __getattr__
    [ Duplicate Lines Snipped ]
    File "/tmp/pytest-10/test_env_vars_override_config_file0/pip_src/pip/_vendor/cachecontrol/filewrapper.py", line 24 in __getattr__
    File "/tmp/pytest-10/test_env_vars_override_config_file0/pip_src/pip/_vendor/requests/packages/urllib3/response.py", line 287 in closed
  Timeout (0:00:05)!
  Current thread 0x00007f417bd92740:
    File "/tmp/pytest-10/test_env_vars_override_config_file0/pip_src/pip/_vendor/cachecontrol/filewrapper.py", line 24 in __getattr__
    [ Duplicate Lines Snipped ]
    File "/tmp/pytest-10/test_env_vars_override_config_file0/pip_src/pip/_vendor/cachecontrol/filewrapper.py", line 24 in __getattr__
    File "/tmp/pytest-10/test_env_vars_override_config_file0/pip_src/pip/_vendor/requests/packages/urllib3/response.py", line 287 in closed
  Timeout (0:00:05)!
  Current thread 0x00007f417bd92740:
    File "/tmp/pytest-10/test_env_vars_override_config_file0/pip_src/pip/_vendor/cachecontrol/filewrapper.py", line 24 in __getattr__
    [ Duplicate Lines Snipped ]
    File "/tmp/pytest-10/test_env_vars_override_config_file0/pip_src/pip/_vendor/cachecontrol/filewrapper.py", line 24 in __getattr__
  Timeout (0:00:05)!
  Current thread 0x00007f417bd92740:
    File "/tmp/pytest-10/test_env_vars_override_config_file0/pip_src/pip/_vendor/cachecontrol/filewrapper.py", line 24 in __getattr__
    [ Duplicate Lines Snipped ]
    File "/tmp/pytest-10/test_env_vars_override_config_file0/pip_src/pip/_vendor/cachecontrol/filewrapper.py", line 24 in __getattr__
  Timeout (0:00:05)!
  Current thread 0x00007f417bd92740:
    File "/tmp/pytest-10/test_env_vars_override_config_file0/pip_src/pip/_vendor/cachecontrol/filewrapper.py", line 24 in __getattr__
    [ Duplicate Lines Snipped ]
    File "/tmp/pytest-10/test_env_vars_override_config_file0/pip_src/pip/_vendor/cachecontrol/filewrapper.py", line 24 in __getattr__
  Timeout (0:00:05)!
  Current thread 0x00007f417bd92740:
    File "/tmp/pytest-10/test_env_vars_override_config_file0/pip_src/pip/_vendor/cachecontrol/filewrapper.py", line 24 in __getattr__
    [ Duplicate Lines Snipped ]
    File "/tmp/pytest-10/test_env_vars_override_config_file0/pip_src/pip/_vendor/cachecontrol/filewrapper.py", line 24 in __getattr__
  Timeout (0:00:05)!
  Current thread 0x00007f417bd92740:
    File "/tmp/pytest-10/test_env_vars_override_config_file0/pip_src/pip/_vendor/cachecontrol/filewrapper.py", line 24 in __getattr__
    [ Duplicate Lines Snipped ]
    File "/tmp/pytest-10/test_env_vars_override_config_file0/pip_src/pip/_vendor/cachecontrol/filewrapper.py", line 24 in __getattr__
  Timeout (0:00:05)!
  Current thread 0x00007f417bd92740:
    File "/tmp/pytest-10/test_env_vars_override_config_file0/pip_src/pip/_vendor/cachecontrol/filewrapper.py", line 24 in __getattr__
    [ Duplicate Lines Snipped ]
    File "/tmp/pytest-10/test_env_vars_override_config_file0/pip_src/pip/_vendor/cachecontrol/filewrapper.py", line 24 in __getattr__
    File "/tmp/pytest-10/test_env_vars_override_config_file0/pip_src/pip/_vendor/requests/packages/urllib3/response.py", line 285 in closed
    File "/tmp/pytest-10/test_env_vars_override_config_file0/pip_src/pip/_vendor/cachecontrol/filewrapper.py", line 24 in __getattr__
    [ Duplicate Lines Snipped ]
    File "/tmp/pytest-10/test_env_vars_override_config_file0/pip_src/pip/_vendor/cachecontrol/filewrapper.py", line 24 in __getattr__
  </code></pre>
  
  <p>This suggests to me that maybe the problem is something to do with the garbage collection and urllib3? The <code>Filewrapper</code> in <code>pip._vendor.cachecontrol.filewrapper</code> is used as a wrapper around a urllib3 response object (which subclasses <code>io.IOBase</code>) so that we can tee the <code>read()</code> method to store the results of each read call in a buffer as well as returning it, and then once the file has been completely consumed run a callback with the contents of that buffer so that we can store the item in the cache. Could this be interacting with the GC in some way?</p>
  
  <p><strong>Update #2</strong></p>
  
  <p>If I add a <code>def __del__(self): pass</code> method to the Filewrapper class, then everything works correctly in the cases I've tried. I tested to ensure that this wasn't because I just happened to define a method (which "fixes" it sometimes) by changing that to <code>def __del2__(self): pass</code> and it started failing again. I'm not sure <em>why</em> this works exactly and a no-op <code>__del__</code> method seems like it's less than optimal.</p>
  
  <p><strong>Update #3</strong></p>
  
  <p>Adding a <code>import gc; gc.set_debug(gc.DEBUG_UNCOLLECTABLE)</code> printed stuff to stderr twice during the execution of the pip command that has been hanging, they are:</p>
  
  <pre><code>gc: uncollectable &lt;CallbackFileWrapper 0x7f66385c1cd0&gt;
  gc: uncollectable &lt;dict 0x7f663821d5a8&gt;
  gc: uncollectable &lt;functools.partial 0x7f663831de10&gt;
  gc: uncollectable &lt;_io.BytesIO 0x7f663804dd50&gt;
  gc: uncollectable &lt;method 0x7f6638219170&gt;
  gc: uncollectable &lt;tuple 0x7f663852bd40&gt;
  gc: uncollectable &lt;HTTPResponse 0x7f663831c7d0&gt;
  gc: uncollectable &lt;PreparedRequest 0x7f66385c1a90&gt;
  gc: uncollectable &lt;dict 0x7f663852cb48&gt;
  gc: uncollectable &lt;dict 0x7f6637fdcab8&gt;
  gc: uncollectable &lt;HTTPHeaderDict 0x7f663831cb90&gt;
  gc: uncollectable &lt;CaseInsensitiveDict 0x7f66385c1ad0&gt;
  gc: uncollectable &lt;dict 0x7f6638218ab8&gt;
  gc: uncollectable &lt;RequestsCookieJar 0x7f663805d7d0&gt;
  gc: uncollectable &lt;dict 0x7f66382140e0&gt;
  gc: uncollectable &lt;dict 0x7f6638218680&gt;
  gc: uncollectable &lt;list 0x7f6638218e18&gt;
  gc: uncollectable &lt;dict 0x7f6637f14878&gt;
  gc: uncollectable &lt;dict 0x7f663852c5a8&gt;
  gc: uncollectable &lt;dict 0x7f663852cb00&gt;
  gc: uncollectable &lt;method 0x7f6638219d88&gt;
  gc: uncollectable &lt;DefaultCookiePolicy 0x7f663805d590&gt;
  gc: uncollectable &lt;list 0x7f6637f14518&gt;
  gc: uncollectable &lt;list 0x7f6637f285a8&gt;
  gc: uncollectable &lt;list 0x7f6637f144d0&gt;
  gc: uncollectable &lt;list 0x7f6637f14ab8&gt;
  gc: uncollectable &lt;list 0x7f6637f28098&gt;
  gc: uncollectable &lt;list 0x7f6637f14c20&gt;
  gc: uncollectable &lt;list 0x7f6637f145a8&gt;
  gc: uncollectable &lt;list 0x7f6637f14440&gt;
  gc: uncollectable &lt;list 0x7f663852c560&gt;
  gc: uncollectable &lt;list 0x7f6637f26170&gt;
  gc: uncollectable &lt;list 0x7f663821e4d0&gt;
  gc: uncollectable &lt;list 0x7f6637f2d050&gt;
  gc: uncollectable &lt;list 0x7f6637f14fc8&gt;
  gc: uncollectable &lt;list 0x7f6637f142d8&gt;
  gc: uncollectable &lt;list 0x7f663821d050&gt;
  gc: uncollectable &lt;list 0x7f6637f14128&gt;
  gc: uncollectable &lt;tuple 0x7f6637fa8d40&gt;
  gc: uncollectable &lt;tuple 0x7f66382189e0&gt;
  gc: uncollectable &lt;tuple 0x7f66382183f8&gt;
  gc: uncollectable &lt;tuple 0x7f663866cc68&gt;
  gc: uncollectable &lt;tuple 0x7f6637f1e710&gt;
  gc: uncollectable &lt;tuple 0x7f6637fc77a0&gt;
  gc: uncollectable &lt;tuple 0x7f6637f289e0&gt;
  gc: uncollectable &lt;tuple 0x7f6637f19f80&gt;
  gc: uncollectable &lt;tuple 0x7f6638534d40&gt;
  gc: uncollectable &lt;tuple 0x7f6637f259e0&gt;
  gc: uncollectable &lt;tuple 0x7f6637f1c7a0&gt;
  gc: uncollectable &lt;tuple 0x7f6637fc8c20&gt;
  gc: uncollectable &lt;tuple 0x7f6638603878&gt;
  gc: uncollectable &lt;tuple 0x7f6637f23440&gt;
  gc: uncollectable &lt;tuple 0x7f663852c248&gt;
  gc: uncollectable &lt;tuple 0x7f6637f2a0e0&gt;
  gc: uncollectable &lt;tuple 0x7f66386a6ea8&gt;
  gc: uncollectable &lt;tuple 0x7f663852f9e0&gt;
  gc: uncollectable &lt;tuple 0x7f6637f28560&gt;
  </code></pre>
  
  <p>and then</p>
  
  <pre><code>gc: uncollectable &lt;CallbackFileWrapper 0x7f66385c1350&gt;
  gc: uncollectable &lt;dict 0x7f6638c33320&gt;
  gc: uncollectable &lt;HTTPResponse 0x7f66385c1590&gt;
  gc: uncollectable &lt;functools.partial 0x7f6637f03ec0&gt;
  gc: uncollectable &lt;_io.BytesIO 0x7f663804d600&gt;
  gc: uncollectable &lt;dict 0x7f6637f1f680&gt;
  gc: uncollectable &lt;method 0x7f663902d3b0&gt;
  gc: uncollectable &lt;tuple 0x7f663852be18&gt;
  gc: uncollectable &lt;HTTPMessage 0x7f66385c1c10&gt;
  gc: uncollectable &lt;HTTPResponse 0x7f66385c1450&gt;
  gc: uncollectable &lt;PreparedRequest 0x7f66385cac50&gt;
  gc: uncollectable &lt;dict 0x7f6637f2f248&gt;
  gc: uncollectable &lt;dict 0x7f6637f28b90&gt;
  gc: uncollectable &lt;dict 0x7f6637f1e638&gt;
  gc: uncollectable &lt;list 0x7f6637f26cb0&gt;
  gc: uncollectable &lt;list 0x7f6637f2f638&gt;
  gc: uncollectable &lt;HTTPHeaderDict 0x7f66385c1f90&gt;
  gc: uncollectable &lt;CaseInsensitiveDict 0x7f66385b2890&gt;
  gc: uncollectable &lt;dict 0x7f6638bd9200&gt;
  gc: uncollectable &lt;RequestsCookieJar 0x7f663805da50&gt;
  gc: uncollectable &lt;dict 0x7f6637f28a28&gt;
  gc: uncollectable &lt;dict 0x7f663853aa28&gt;
  gc: uncollectable &lt;list 0x7f663853a6c8&gt;
  gc: uncollectable &lt;dict 0x7f6638ede5f0&gt;
  gc: uncollectable &lt;dict 0x7f6637f285f0&gt;
  gc: uncollectable &lt;dict 0x7f663853a4d0&gt;
  gc: uncollectable &lt;method 0x7f663911f710&gt;
  gc: uncollectable &lt;DefaultCookiePolicy 0x7f663805d210&gt;
  gc: uncollectable &lt;list 0x7f6637f28ab8&gt;
  gc: uncollectable &lt;list 0x7f6638215050&gt;
  gc: uncollectable &lt;list 0x7f663853a200&gt;
  gc: uncollectable &lt;list 0x7f6638215a28&gt;
  gc: uncollectable &lt;list 0x7f663853a950&gt;
  gc: uncollectable &lt;list 0x7f663853a998&gt;
  gc: uncollectable &lt;list 0x7f6637f21638&gt;
  gc: uncollectable &lt;list 0x7f6637f0cd40&gt;
  gc: uncollectable &lt;list 0x7f663853ac68&gt;
  gc: uncollectable &lt;list 0x7f6637f22c68&gt;
  gc: uncollectable &lt;list 0x7f663853a170&gt;
  gc: uncollectable &lt;list 0x7f6637fa6a28&gt;
  gc: uncollectable &lt;list 0x7f66382153b0&gt;
  gc: uncollectable &lt;list 0x7f66386a5e60&gt;
  gc: uncollectable &lt;list 0x7f663852f2d8&gt;
  gc: uncollectable &lt;list 0x7f66386a3320&gt;
      [&lt;pip._vendor.cachecontrol.filewrapper.CallbackFileWrapper object at 0x7f66385c1cd0&gt;, &lt;pip._vendor.cachecontrol.filewrapper.CallbackFileWrapper object at 0x7f66385c1350&gt;]
  </code></pre>
  
  <p>Is that useful information? I've never used that flag before so I have no idea if that is unusual or not.</p>