Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Multithreading support in pluggable module, but with problems



An idea explaining what's happening:
– the streams are pipes,
– if writer (zpin) ends first, reader's stdin is "Bad descriptor", not
to be fclose()-d,
– this explains random behavior, ~50% of fclose() failures.

That said, I think FILE has internal buffers to be freed, that's clearly
visible from debugger ("p *stream"). So it's not obvious to "not call
fclose()". Maybe I shouldn't use stdlib.h for pipe created by Zsh?
Handle it by system read(), write(), fcntl(), close(). Or has my
thinking no sense – pipes created by Zsh can be always fclosed() on both
ends ?

Below code tries to create a pipe that instantly closes, and the output
is:

2, 0, Operation timed out
Operation timed out

so looks like stream is not trashed by "echo a" instantly exiting.. But
popen() might be different from what's Zsh does.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/errno.h>
#include <fcntl.h>

int main( int argc, char *argv[] ) {
    FILE *my_end;
    my_end = popen( "/bin/echo a", "r" );

    char buf[32];
    int count = fread( buf, 1, 32, my_end );
    sleep( 1 );

    int flags = fcntl( my_end->_file, F_GETFD );
    printf( "%d, %d, %s\n", count, flags, strerror( errno ) );
    fclose( my_end );
    printf( "%s\n", strerror( errno ) );
    fflush( stdout );
    return 0;
}


That said, after guarding fclose() by successful fcntl(), I did 100 runs
in a loop of 32 concurrent workers spawning, and no segfault. That said
in debugger the execution might hang, with backtrace:

(lldb) bt
* thread #1: tid = 0x25367, 0x00007fff8be4683a
libsystem_kernel.dylib`close + 10, stop reason = signal SIGSTOP
  * frame #0: 0x00007fff8be4683a libsystem_kernel.dylib`close + 10
    frame #1: 0x00000001000b48ea zsh-5.3.1-dev-0`zclose(fd=12) + 266 at
    utils.c:2107
    frame #2: 0x00000001000b4ae6 zsh-5.3.1-dev-0`redup(x=12, y=0) + 246
    at utils.c:2026
    frame #3: 0x000000010002e7fc
    zsh-5.3.1-dev-0`fixfds(save=0x00007fff5fbfe4f0) + 76 at exec.c:4137
    frame #4: 0x000000010002da2b
    zsh-5.3.1-dev-0`execcmd_exec(state=0x00007fff5fbff600,
    eparams=0x00007fff5fbfe600, input=13, output=0, how=18, last1=2) +
    20139 at exec.c:3976
    frame #5: 0x0000000100028599
    zsh-5.3.1-dev-0`execpline2(state=0x00007fff5fbff600, pcode=387,
    how=18, input=13, output=0, last1=0) + 441 at exec.c:1861

-- 
  Sebastian Gniazdowski
  psprint2@xxxxxxxxxxxx



Messages sorted by: Reverse Date, Date, Thread, Author