Skip to content

asyncio shell subprocess does not close properly #100055

Closed
@abauske

Description

@abauske

Environment:
Windows 10, Python 3.9.0

consider this example script:

#!/usr/bin/env python3
import asyncio

async def execAsync():
    cmd = 'timeout 10'
    process = await asyncio.create_subprocess_shell(cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT)
    try:
        while process.returncode is None:
            output = await process.stdout.readline()
            print(output)
            await asyncio.sleep(0)  # in case of a continuous streem of b'' give other processes time to do their stuff
        await process.wait()  # this seems to do nothing of help
    except asyncio.CancelledError:
        process.terminate()  # this seems to do nothing of help
        await process.wait()  # this seems to do nothing of help
    finally:
        process.terminate()  # this seems to do nothing of help
        # process._transport.close()  # This is required for me. "ValueError: I/O operation on closed pipe" otherwise
        await process.wait()  # this seems to do nothing of help

async def main():
    canReader = asyncio.create_task(execAsync())
    await asyncio.sleep(1)
    canReader.cancel()
    await canReader
    return 0

if __name__ == '__main__':
    exit(asyncio.run(main()))

with process._transport.close() commented (as above) results in the following error:

Expand
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0000018B72FF4A60>
Traceback (most recent call last):
File "<path to user folder>\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 115, in __del__
  _warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
File "<path to user folder>\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 79, in __repr__
  info.append(f'fd={self._sock.fileno()}')
File "<path to user folder>\AppData\Local\Programs\Python\Python39\lib\asyncio\windows_utils.py", line 102, in fileno
  raise ValueError("I/O operation on closed pipe")
ValueError: I/O operation on closed pipe
Exception ignored in: <function BaseSubprocessTransport.__del__ at 0x0000018B72FEE160>
Traceback (most recent call last):
File "<path to user folder>\AppData\Local\Programs\Python\Python39\lib\asyncio\base_subprocess.py", line 125, in __del__
  _warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
File "<path to user folder>\AppData\Local\Programs\Python\Python39\lib\asyncio\base_subprocess.py", line 78, in __repr__
  info.append(f'stdout={stdout.pipe}')
File "<path to user folder>\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 79, in __repr__
  info.append(f'fd={self._sock.fileno()}')
File "<path to user folder>\AppData\Local\Programs\Python\Python39\lib\asyncio\windows_utils.py", line 102, in fileno
  raise ValueError("I/O operation on closed pipe")
ValueError: I/O operation on closed pipe

with process._transport.close() it finishes without an error. but _transport seems to not be made to be accessed. Is there something I have missed to fix this?

See this Stackoverflow where I have found that solution (and decided to create this issue as this seems not intended).

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedtopic-asyncio

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions