Skip to content

Add test for preexec_fn fd double close issue #479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import subprocess
import sys
import tempfile
import textwrap
import time
import unittest

Expand Down Expand Up @@ -796,7 +797,69 @@ async def test():


class Test_UV_Process(_TestProcess, tb.UVTestCase):
pass
def test_process_preexec_fn_double_close(self):
script = textwrap.dedent("""
import os
import sys
import threading
import queue
import concurrent.futures

pid = os.getpid()
q = queue.Queue()
evt = threading.Event()
r, w = os.pipe()
pipe = os.pipe
close = os.close


def mock_pipe():
rv = pipe()
q.put(rv[1])
return rv


def mock_close(fd):
close(fd)
if os.getpid() == pid:
q.put(fd)
evt.wait()


os.pipe = mock_pipe
os.close = mock_close

import asyncio
import uvloop

uvloop.install()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the 3.11 test suite lands it might be worth conditionally using the modern asyncio.Runner(loop_factory=uvloop.new_event_loop)



def thread():
fd = q.get()
while True:
fd_close = q.get()
if fd == fd_close:
os.dup2(r, fd)
evt.set()
break
while os.read(fd, 32) != b"exit":
pass


async def test():
await asyncio.create_subprocess_exec(
sys.executable, "-c", "pass", preexec_fn=lambda: True
)
os.write(w, b"exit")


with concurrent.futures.ThreadPoolExecutor() as executor:
fut = executor.submit(thread)
asyncio.run(test())
fut.result()
""")
subprocess.check_call([sys.executable, '-c', script])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
subprocess.check_call([sys.executable, '-c', script])
subprocess.run([sys.executable, '-c', script], check=True)



class Test_AIO_Process(_TestProcess, tb.AIOTestCase):
Expand Down