zsh-workers
 help / color / mirror / code / Atom feed
From: Sebastian Gniazdowski <psprint2@fastmail.com>
To: zsh-workers@zsh.org
Subject: Re: Multithreading support in pluggable module, but with problems
Date: Sat, 11 Feb 2017 08:43:59 -0800	[thread overview]
Message-ID: <1486831439.2786402.877844408.05A3870D@webmail.messagingengine.com> (raw)
In-Reply-To: <1486817916.2745591.877718432.2CE18595@webmail.messagingengine.com>

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@fastmail.com


  reply	other threads:[~2017-02-11 16:44 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-11 12:58 Sebastian Gniazdowski
2017-02-11 16:43 ` Sebastian Gniazdowski [this message]
2017-02-11 19:46 ` Bart Schaefer
2017-02-11 20:14   ` Sebastian Gniazdowski
2017-02-11 20:39     ` Bart Schaefer
2017-02-11 20:56       ` Sebastian Gniazdowski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1486831439.2786402.877844408.05A3870D@webmail.messagingengine.com \
    --to=psprint2@fastmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).