[issue2320] Race condition in subprocess using stdin

Ludwig Hähne report at bugs.python.org
Mon Mar 17 14:31:52 CET 2008

New submission from Ludwig Hähne <wkx at gmx.li>:

Pythons subprocess module has a race condition when stdin is used. The
problem can be reproduced with the following script (based on the script
in issue "#1731717"/"msg32210" (slightly changed to use stdin):

import sys, os, threading, subprocess, time

class X(threading.Thread):
  def __init__(self, *args, **kwargs):
    super(X, self).__init__(*args, **kwargs)

def tt():
  s = subprocess.Popen(("cat"), stdin=subprocess.PIPE)
  s.communicate(input = '#')

for i in xrange(20):
  X(target = tt)

On multi-processor (or multi-core) machines the script hangs fairly

Protecting the Popen call with a lock solves the problem. I searched the
documentation if using stdin with subprocess.Popen was not thread-safe,
but found no indication.

Tested with Python 2.5.1, 2.5.2 and 2.6a1. The problem can be reproduced
with all mentioned versions.

components: Library (Lib)
messages: 63637
nosy: Pankrat
severity: normal
status: open
title: Race condition in subprocess using stdin
versions: Python 2.5, Python 2.6

Tracker <report at bugs.python.org>

More information about the Python-bugs-list mailing list