Author Pankrat
Recipients Pankrat
Date 2008-03-17.13:31:50
SpamBayes Score 0.0472516
Marked as misclassified No
Message-id <>
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.
Date User Action Args
2008-03-17 13:31:52Pankratsetspambayes_score: 0.0472516 -> 0.0472516
recipients: + Pankrat
2008-03-17 13:31:52Pankratsetspambayes_score: 0.0472516 -> 0.0472516
messageid: <>
2008-03-17 13:31:51Pankratlinkissue2320 messages
2008-03-17 13:31:51Pankratcreate