The bug is actually in your C++ program; it re-uses the in variable for both of its prints, which means if the second call to getline doesn't return anything (and it doesn't in your case, because the EOF of stdin is reached after the first getline call), the contents returned by the first getline call will be printed twice, because they're still saved inside in. This is obvious if you tweak your C++ program slightly to reset in between the two calls to getline:
#include <iostream>
using namespace std;
int main()
{
string in;
getline(cin, in);
cout << in << endl;
in = ""; // reset in
getline(cin, in);
cout << in << endl;
return 0;
}
Output:
b'Why is this twice'
done
Edit:
Based on the requirements you gave in the comments, you want something like this:
from subprocess import Popen, PIPE, STDOUT
p = Popen(['./inout'], stdout=PIPE, stdin=PIPE, stderr=STDOUT, bufsize=0) # bufsize=0 keeps stdout from being buffered.
# Send first line to inout
p.stdin.write(b'Why is this twice\n')
# Read first line out output
first = p.stdout.readline()
print("got {}".format(first))
# Get line from user
a = input()
# Send second line to inout
p.stdin.write('{}\n'.format(a).encode('utf-8'))
# Read second line from inout
second = p.stdout.readline()
print('also got {}'.format(second))
p.wait()
print("done")
Output:
got b'Why is this twice\n'
sdf # This was typed by me
b'sdf\n'
done