title: "filedescriptor out of range in select()" when using python's subprocess with rsync
body: <p>the code below is used to sync uploaded picture to another place. it works, but after a period of time(about 10 days), the service is unusable , showing error: 'filedescriptor out of range in select()', but restart service solves the problem.</p>
  
  <pre><code># sync.py
  
  def sync_file(source_pic, hashval, retry_num=3):
  
      pic_path = os.path.join(gen_dir(hashval), os.path.split(source_pic)[1])
      filename = tempfile.mkstemp()[1]
      with open(filename, 'w') as f:
          f.write(pic_path)
  
      for sync_path in options.sync_paths:
          try_num = 0
          rsync_cmd = ['rsync','-au', '--files-from='+filename, options.pic_path, sync_path]
  
          while try_num &lt; retry_num:
              proc = subprocess.Popen(rsync_cmd,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
              stdout_value, stderr_value = proc.communicate()
  
              if len(stderr_value) != 0:
                  logger.error('sync failed: %s' % stderr_value)
                  try_num = try_num + 1
                  #raise UploadException('sync failed')
              else:
                  break
  
      os.remove(filename)
  </code></pre>
  
  <p>log info:</p>
  
  <pre><code>File "/path/to/sync.py", line 25, in sync_file
      stdout_value, stderr_value = proc.communicate()
  File "/usr/lib/python2.6/subprocess.py", line 691, in communicate
      return self._communicate(input)
  File "/usr/lib/python2.6/subprocess.py", line 1211, in _communicate
      rlist, wlist, xlist = select.select(read_set, write_set, [])
      ValueError: filedescriptor out of range in select()
  </code></pre>
  
  <p>are there unclosed file descriptors that cause the error? it seems subprocess doesn't close file descriptor, so when it runs 1024 times, the file descriptor is out of range. (we are using python 2.6, subprocess is forced to use select.select() which has a limit of 1024 file descriptors even epoll is available)</p>