Git Product home page Git Product logo

Comments (1)

anki-code avatar anki-code commented on June 25, 2024

After tracing this I understand the case. In the nutshell: if the last command in the pipe has no waiting for stdin (e.g. echo, ls, head -n0, etc) the CommandPipeline will stop immediately and

  • If we have callable alias in pipe this leads to IO errors e.g. callias | echo will lead to descriptors error because callias is working in thread and operating with std but std was closed by the main thread after finishing last process (echo).

  • If we have process in the pipe it will continue working in the background e.g. sleep 999 | echo will return prompt but has sleep in the background without any notifications. If you repeat this in Bash it simply waits for the end of each process in the pipe.

We need to refactor core logic of CommandPipeline.iterraw(). I described the goal here.

Here is tracing two cases that are the result of the current logic.

Case 1: race condition in prompt

Steps:

  1. Run a | head -n0. Here -n0 leads to stop head immediately. This code will be executed:

    xonsh/xonsh/base_shell.py

    Lines 387 to 390 in 9e86938

    tee = Tee(encoding=enc, errors=err)
    ts0 = time.time()
    try:
    exc_info = run_compiled_code(code, self.ctx, None, "single")

    • Here we create Tee - wrapper for std with memory buffer.
    • And run the command with case of callable alias. It works in thread AND NOT blocking the execution.
  2. While callable alias is working we achieve this line in the same code block:

    tee.close()

    • Here memory buffer will be closed BUT the callable alias still executing. We have something like race condition.
  3. When callable alias tries to operate with memory buffer it already closed and we see I/O operation on closed file. When I'm saying "memory buffer" I mean this memory buffer that Tee manages and _TeeStd operates:

    self.memory = io.TextIOWrapper(

    self.stdout = _TeeStd("stdout", self.memory)

Case 2: callable alias and loop

If we run:

for i in range(0,10):
    a | head -n0

We will have multiple randomly spreaded "Bad file descriptor" errors. I tried to trace them but gave it up because it's obvious that the errors are from intersection of multiple threads that are working at once because of head -n0 stopped immediately.

from xonsh.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.