zsh-workers
 help / color / mirror / code / Atom feed
* Multithreading support in pluggable module, but with problems
@ 2017-02-11 12:58 Sebastian Gniazdowski
  2017-02-11 16:43 ` Sebastian Gniazdowski
  2017-02-11 19:46 ` Bart Schaefer
  0 siblings, 2 replies; 6+ messages in thread
From: Sebastian Gniazdowski @ 2017-02-11 12:58 UTC (permalink / raw)
  To: zsh-workers

Hello,
being able to build modules outside Zsh I've created one that allows
(this works):

# zpin 'print -rl "a:b" "c:d"' | zpopulator -A my_hash
# print -r -- ${(kv)my_hash[@]}
a b c d

This is done entirely in background. zpin does fork() and this way makes
Zsh think it's finished – main process exits. zpopulator starts a thread
and makes main thread exit – this is also sufficient to make Zsh think
that there is no background job.

The aim is e.g. to do:

zpin "ls -R1 /home/user" | zpopulator -a my_files
# echo $zpworkers_count
1
<later..>
# echo $zpworkers_count
0

And work in shell as normal. Also, I plan to modify my syntax
highlighting so that paths are checked in background. This is to avoid
stalls when paths are on auto-mounted directory.

However I cannot resolve all problems:

1. I duplicate stdin of "zpopulator" (reading thread):

    /* Duplicate standard input */
    oconf->stream = fdopen( dup( fileno( stdin ) ), "r" );
    /* Prepare standard input replacement */
    oconf->r_devnull = fopen( "/dev/null", "r");
    /* Replace standard input with /dev/null */
    dup2( fileno( oconf->r_devnull ), STDIN_FILENO );
    fclose( oconf->r_devnull );

I tried with only first line of code, without last (fclose) line, and
result is always: 50% of runs has error:

"zpopulator: Warning: could not close input stream: Bad file descriptor"

This is from fclose(). There is a fcntl() check before:
        flags = fcntl( oconf->stream->_file, F_GETFD );

and it results in:

"zpopulator: Indeed bad descriptor 11: Bad file descriptor"

After fclose(), the ->_file, the descriptor, is even turned into -1.

Any idea why duplicated descriptor, used only in worker thread, is
invalid? What could Zsh do with stdin/out file descriptors of commands
it runs to cause such problems?

https://github.com/psprint/zpopulator

-- 
  Sebastian Gniazdowski
  psprint2@fastmail.com


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2017-02-11 20:56 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-11 12:58 Multithreading support in pluggable module, but with problems Sebastian Gniazdowski
2017-02-11 16:43 ` Sebastian Gniazdowski
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

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).