Thanks to visit codestin.com
Credit goes to github.com

Skip to content

spawn's .before might contain input of sendline #821

@mmesiti

Description

@mmesiti

I have noticed that on one of the systems I am working one command "breaks" the spawn, in the sense that after it is run, the .before member contains content that actually comes from the last call to sendline().

This actually breaks the jupyter bash kernel on my system (see takluyver/bash_kernel#151).

import pexpect
import os

bashrc= os.path.join(os.path.dirname(pexpect.__file__), 'bashrc.sh')
child = pexpect.spawn("bash",
                      ['--rcfile',bashrc], 
                      echo=False,
                      encoding = 'utf-8',
                      codec_errors='replace')

child.expect_exact(["$"], timeout=2) 

# This mimics, in a simplified way, what happens in REPLWrapper.run_command()
def run_command(command):
    print("-----------------------------------------")
    print(f"Command: {command}")
    print("-----------------------------------------")
    child.sendline(command)
    child.expect_exact(["$"], timeout=2000) 

    out= u''.join([child.before])
    print(f"Output:\n{out}")


# this works fine
run_command("pwd")
# this works fine too
run_command("ls")

breaking_command = "enroot import docker://nvcr.io\#nvidia/pytorch:21.04-py3 &> enroot-import-output.txt"

# Now this breaks the spawn .before member, even if **there is no output**
run_command(breaking_command) 

# The output of the subsequent commands shows the problem
run_command("ls")

# here too
run_command(" {echo $?; } > /dev/null")

Output:

-----------------------------------------
Command: pwd
-----------------------------------------
Output:
/home/hk-project-scs/hs2454/jupyter

-----------------------------------------
Command: ls
-----------------------------------------
Output:
alpine.sqsh		  error.txt		     reslog.txt
beforetype.txt		  logfile.txt		     send_logfile.txt
child_output.txt	  output		     start-jupyter.sh
config			  pty_reproducer_attempt.py  Untitled.ipynb
enroot-import-output.txt  read_logfile.txt	     venv
ENV			  reproducer.py		     write_logfile.txt
error_codes.txt		  requirements.txt

-----------------------------------------
Command: enroot import docker://nvcr.io\#nvidia/pytorch:21.04-py3 &> enroot-import-output.txt
-----------------------------------------
Output:

-----------------------------------------
Command: ls
-----------------------------------------
Output:
ls # <<<<<<<<< THIS SHOULD NOT BE HERE
alpine.sqsh		  error.txt			 requirements.txt
beforetype.txt		  logfile.txt			 reslog.txt
child_output.txt	  nvidia+pytorch+21.04-py3.sqsh  send_logfile.txt
config			  output			 start-jupyter.sh
enroot-import-output.txt  pty_reproducer_attempt.py	 Untitled.ipynb
ENV			  read_logfile.txt		 venv
error_codes.txt		  reproducer.py			 write_logfile.txt

-----------------------------------------
Command:  {echo $?; } > /dev/null
-----------------------------------------
Output:
 {echo # <<<<<<< THIS SHOULD NOT BE HERE (truncated because there's a $ which is the prompt...)

I will do some additional, lower-level tests and check if I manage to understand more.

Notes:

  • the "breaking command" causes the issue every time I try it on that particular machine. On a different machine it does not.
  • replacing that "breaking command" with a sleep 240 (which lasts a similar amount of time) makes the problem disappear.
  • Shorter commands invocation of the same tool as in the "breaking command" do not trigger the issue either.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions