【问题标题】:OSError: [Errno 24] when multithreading pwntoolsOSError: [Errno 24] 多线程 pwntools 时
【发布时间】:2020-07-16 03:07:08
【问题描述】:

我正在创建一个脚本,它将启动一些线程,并在二进制文件上运行输入。用法是python3 script.py binary input.txt。这是遇到此错误的可重现段:

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()

运行 5 秒左右后,我遇到了这个错误,以及错误:out of pty devices。 我在 ubuntu 20.04 上运行。

【问题讨论】:

  • 或许可以阅读pthread tutorial。请注意,Python 有一个GIL。下载Python的源代码学习一下,是开源的。另见errno(3)
  • 我添加了一些可重现的代码。我只是不明白如果我们在启动一个新进程之前等待每个进程退出,为什么操作系统会遇到这个问题:(p.poll(block=True))。感谢您的回复。
  • 轮询应该阻塞,直到进程完成,这意味着最多将有 10 个进程同时运行:docs.pwntools.com/en/stable/tubes/…

标签: python-3.x multithreading process pwntools


【解决方案1】:

原来 pwntools 进程 API 没有关闭 stderr 或 stdout,所以在从运行返回之前关闭它们,解决了问题。

【讨论】:

  • 唯一的问题是p.poll();您应该使用p.close() 来确保进程终止,或者使用with 语句
猜你喜欢
  • 2020-03-07
  • 1970-01-01
  • 2016-04-21
  • 2019-04-11
  • 1970-01-01
  • 2018-08-12
  • 1970-01-01
  • 2022-07-21
  • 2021-10-17
相关资源
最近更新 更多