-2

I'm creating a script which will start some threads, and run an input on a binary. The usage is python3 script.py binary input.txt. Here is a reproducible segment that encounters this error:

from pwn import *
import threading

inputFile = ""
try:
    inputFile = open(sys.argv[2], 'r')
    inputStr = inputFile.read().strip()
    inputFile.close()
except OSError:
    sys.exit()

def run(testStr) :
    p = process("./"+sys.argv[1])
    p.sendline(testStr)
    p.shutdown()
    return p.poll(block = True)

def opens() :
    while True:
        run(inputStr)
        
for i in range(0,10):
    thread = threading.Thread(target = opens)
    thread.start()

After 5 or so seconds of run time, I run into this error, aswell as the error: out of pty devices. I am running on ubuntu 20.04.

maxphat
  • 37
  • 5
  • Read perhaps a [pthread tutorial](https://computing.llnl.gov/tutorials/pthreads/). Be aware that Python has a [GIL](https://en.wikipedia.org/wiki/Global_interpreter_lock). Download the source code of [Python](http://python.org) and study it, it is opensource. See also [errno(3)](https://man7.org/linux/man-pages/man3/errno.3.html) – Basile Starynkevitch Jul 16 '20 at 03:14
  • Ive added some reproducible code. I just don't understand why the OS would have a problem with this if we are waiting for each process to exit before starting a new one : (p.poll(block=True)). Thanks for responding. – maxphat Jul 16 '20 at 04:43
  • Poll should block until the process is complete, meaning that at most there will be 10 processes running at a singular time: https://docs.pwntools.com/en/stable/tubes/processes.html?highlight=poll#pwnlib.tubes.process.process.poll – maxphat Jul 16 '20 at 04:52

1 Answers1

0

It turns out pwntools process API does not close stderr or stdout, so closing them before returning from run, solves the problem.

maxphat
  • 37
  • 5
  • The problem is the sole `p.poll()`; you should use `p.close()` to make sure the process terminates, or use the `with` statement – Arusekk Apr 20 '21 at 17:06