You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
asyncio.StreamReader hangs when reading from pipe and other process exits unexpectedly #87972
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2021-12-05.12:05:52.888>created_at=<Date2021-04-11.12:17:49.488>labels= ['3.8', 'library', 'invalid']
title='asyncio.StreamReader hangs when reading from pipe and other process exits unexpectedly'updated_at=<Date2021-12-05.12:05:52.887>user='https://github.com/kormang'
@kormang this is an expected behaviour, this is a problem even for the OS level, just because it is impossible to know when the reader needs to stop waiting, the best option here is to implement some timeout mechanism.
kormang commented on May 7, 2021
kormangmannequin
MannequinAuthor
More actions
@jaswdr Hm, this is was somewhat unexpected for me, since when reading directly from pipe, and EOF is sent.
Timeout was somewhat awkward in my case, since I don't know when other process will start sending, and how long it would take. On the other hand, I use asyncio loop, and can do this asynchronously, so I get notified when child process dies, by other means, and close the stream. So there are plenty of possible workarounds, but I'm not sure it is impossible to solve the problem on the library level yet. It would take more digging into implementation from my side, however.
takluyver commented on Nov 24, 2021
takluyvermannequin
Mannequin
More actions
In the example script, I believe you need to close the write end of the pipe in the parent after forking:
cpid=os.fork()
ifcpid==0:
# Write to pipe (child)else:
# Parentos.close(ctx)
# Read from pipe
This is the same with synchronous code: os.read(prx, 1) also hangs. You only get EOF when nothing has the write end open any more. All the asyncio machinery doesn't really make any difference to this.
For a similar reason, the code writing (the child, in this case) should close the read end of the pipe after forking. If the parent goes away but the child still has the read end open, then trying to write to the pipe can hang (if the buffer is already full). If the child has closed the read end, trying to write will give you a BrokenPipeError.
kormang commented on Dec 5, 2021
kormangmannequin
MannequinAuthor
More actions
Thanks, takluyver!
You are right. Synchronous code that I was comparing it to had os.close(ctx), but I forgot to add it in the async example, so I thought it was a bug.
Activity
kormang commentedon Apr 11, 2021
When using asyncio to read from pipe, if process on the other side of pipe crashes read operation hangs indefinitely.
Example:
prx, ctx = os.pipe()
@contextlib.asynccontextmanager
async def connect():
try:
loop = asyncio.get_event_loop()
await _connect_read(loop)
yield
finally:
close()
jaswdr commentedon May 3, 2021
@kormang this is an expected behaviour, this is a problem even for the OS level, just because it is impossible to know when the reader needs to stop waiting, the best option here is to implement some timeout mechanism.
kormang commentedon May 7, 2021
@jaswdr Hm, this is was somewhat unexpected for me, since when reading directly from pipe, and EOF is sent.
Timeout was somewhat awkward in my case, since I don't know when other process will start sending, and how long it would take. On the other hand, I use asyncio loop, and can do this asynchronously, so I get notified when child process dies, by other means, and close the stream. So there are plenty of possible workarounds, but I'm not sure it is impossible to solve the problem on the library level yet. It would take more digging into implementation from my side, however.
takluyver commentedon Nov 24, 2021
In the example script, I believe you need to close the write end of the pipe in the parent after forking:
This is the same with synchronous code: os.read(prx, 1) also hangs. You only get EOF when nothing has the write end open any more. All the asyncio machinery doesn't really make any difference to this.
For a similar reason, the code writing (the child, in this case) should close the read end of the pipe after forking. If the parent goes away but the child still has the read end open, then trying to write to the pipe can hang (if the buffer is already full). If the child has closed the read end, trying to write will give you a BrokenPipeError.
kormang commentedon Dec 5, 2021
Thanks, takluyver!
You are right. Synchronous code that I was comparing it to had os.close(ctx), but I forgot to add it in the async example, so I thought it was a bug.
Closing this issue.