skywind3000 / asyncrun.vim

:rocket: Run Async Shell Commands in Vim 8.0 / NeoVim and Output to the Quickfix Window !!
https://www.vim.org/scripts/script.php?script_id=5431
MIT License
1.84k stars 109 forks source link

unknown bug (duplicate output maybe) #271

Closed yaoyhu closed 1 year ago

yaoyhu commented 1 year ago

I am writing a simple code about creating process, the output in terminal like below:

➜ tmp gcc p2.c -o p2; ./p2

pid:80230
child:80231
parent of 80231 (wc = 80231) (pid = 80230)

But with :AsyncRun gcc p2.c -o p2; ./p2:

  5 || [gcc p2.c; ./a.out]                                                                                                                                                                                           
  4 || pid:78520                                                                                                                                                                                                     
  3 || child:78526                                                                                                                                                                                                   
  2 || pid:78520    ----------> what is this????                                                                                                                                                                                              
  1 || parent of 78526 (wc = 78526) (pid = 78520)                                                                                                                                                                    
6   || [Finished in 0 seconds] 

image

skywind3000 commented 1 year ago

It is not a tty, in C standard library, if the process is running in a pipe, the stdout will be buffered until you call fflush or process exits.

So, the printf in line 7 is buffered with the later printf in line 11 or line 15 and the actual display happens in returns.

Since there is a fork, you will see two pid:xxxx.

To eliminate this, you should add a fflush(stdout); after line 7, this will ensure to display all the outputs of previous printfs.