wexpect
wexpect copied to clipboard
Port overload when running multiple instances leads to timeout
Describe the bug
When executing multiple instances of a script, wexpect overloads on the same port and will typically timeout on the first use of expect()
. In some cases, I've observed a race condition where the ConsoleReader
of the second process connects to the port of the first and things get a little mixed up...
To Reproduce
Call this script from two seperate terminals without specifying a port. The second instance will time-out on expect('>')
.
Then call them again but specify unique ports. All is well.
import wexpect
import os
import sys
pid = os.getpid()
if len(sys.argv) > 1:
port = sys.argv[1]
else:
port = None
print(f'Started process {pid}, using port {port}')
if not port:
child = wexpect.host.SpawnSocket(f'cmd.exe', interact=True)
else:
child = wexpect.host.SpawnSocket(f'cmd.exe', interact=True, port=int(port))
child.expect('>')
print("Prompt detected...")
string = f'echo Hello From {pid}'
child.sendline(string)
child.expect(string)
print(child.match)
wait = input("Press Enter to close.")
An obvious workaround is to specify a unique port to the constructor but it took me a while to work out what was going on...
Expected behavior If a port is not specified, I would expect the default port to be a random free port. Or at a minimum, maybe this behaviour could be documented?
Screenshots N/A
Environment:
- Windows 10.0.19044.2132
- Python 3.7.0
- wexpect 4.0.0
Additional context N/A