Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Multithreading support in pluggable module, but with problems
- X-seq: zsh-workers 40527
- From: Sebastian Gniazdowski <psprint2@xxxxxxxxxxxx>
- To: zsh-workers@xxxxxxx
- Subject: Re: Multithreading support in pluggable module, but with problems
- Date: Sat, 11 Feb 2017 08:43:59 -0800
- Dkim-signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=fastmail.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=mesmtp; bh=1Q2SMYaLENzBzlRAVXXQjOafut c=; b=hCg7Fp2U+7SxrSzBJCWUtgcYn0QFSaT+M1DlaiLB/Onpj2pQw03bCFoiQB u5TFFI2TnqHq7e1mOjxxV7xo3jGqvI0G3lUeZFpj/CGIxcUQK4sbJDTdIrFcLuWn Qwok6g4fCYQdRx8sVrrp8xCKADptqR4QfZn5KhW2iUFkEr8dU=
- Dkim-signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=smtpout; bh=1Q 2SMYaLENzBzlRAVXXQjOafutc=; b=W381ZrhJR0X/wkTYAOGo37lzevNBceUglS 5koo1qUppPbNX61Sw0BnaYmBrhyGdWUs2K0pjnWQ+W1uArXqF6BzUCNgJFLvH81D pR1vwoC44fRfbm7LUMSTi0qvMSOyEtFRQnjaKOLH9/48HYL8FCT6KcFwicbLfT1S BMjZ1hwXI=
- In-reply-to: <1486817916.2745591.877718432.2CE18595@webmail.messagingengine.com>
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <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@xxxxxxxxxxxx
Messages sorted by:
Reverse Date,
Date,
Thread,
Author