Skip to content
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

PtyProcess.read() returns a different value every call #60

Open
gggal123 opened this issue Feb 14, 2021 · 1 comment
Open

PtyProcess.read() returns a different value every call #60

gggal123 opened this issue Feb 14, 2021 · 1 comment

Comments

@gggal123
Copy link

gggal123 commented Feb 14, 2021

This is a very severe bug. When calling Ptyprocess.read() the value returned is different almost every time:

ptyprocess.PtyProcess.spawn(['openssl', "ec", '-noout', '-text', '-in', '/opt/key/s128r1.key']).read()

The output:
image

And again with the same params:
image

And again:
image

I don't know what is causing this but this is very weird.

@takluyver
Copy link
Member

You've got a race condition. The child process is writing data to the pty while the parent process is reading it. How quickly they happen will determine how much data there is for the parent to read.

This is expected behaviour, because the ptyproc.read() doesn't have the common Python convenience feature that calling it with no arguments waits for all the data before returning. It behaves more like the standard C read() function, in that it returns as soon as any data is available.

To read all the output until the child process finishes, you need to do something like this:

ptyproc = ptyprocess.PtyProcess.spawn(['openssl', "ec", '-noout', '-text', '-in', '/opt/key/s128r1.key'])

out = []
while True:
    try:
        out.append(ptyproc.read())
    except EOFError:
        break

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants