zsh-users
 help / color / mirror / code / Atom feed
* runaway zselect
@ 2009-05-17  4:43 Atom Smasher
  2009-05-17  7:58 ` Atom Smasher
  2009-05-17 21:47 ` Bart Schaefer
  0 siblings, 2 replies; 4+ messages in thread
From: Atom Smasher @ 2009-05-17  4:43 UTC (permalink / raw)
  To: zsh-users

i would expect zselect to return an error and stop the while loop when "w" 
stops producing output. instead i'm seeing an infinite loop of blank 
lines, after "w" finishes:

while zselect -r 0
do
   read -r foo
   print -r -- $foo
done < <( w )

same thing reading from a coproc with "done <& p"

the weird thing is that i have it working as desired (reading from a 
coproc) in a script, but i can't reproduce it outside of the script... the 
weirder thing is that i can get this working when i test it:
 	sysread -i 0 -o 1 -t 200 <& p
  but in the script mentioned above, it fails:
 	coprocess: bad file descriptor

any ideas? thanks...


-- 
         ...atom

  ________________________
  http://atom.smasher.org/
  762A 3B98 A3C3 96C9 C6B7 582A B88D 52E4 D9F5 7808
  -------------------------------------------------

 	"People have so manipulated the concept of freedom that it
 	 finally boils down to the right of the stronger and richer to
 	 take from the weaker and poorer whatever they still have."
 		-- Theodor Adorno


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

* Re: runaway zselect
  2009-05-17  4:43 runaway zselect Atom Smasher
@ 2009-05-17  7:58 ` Atom Smasher
  2009-05-17 21:47 ` Bart Schaefer
  1 sibling, 0 replies; 4+ messages in thread
From: Atom Smasher @ 2009-05-17  7:58 UTC (permalink / raw)
  To: zsh-users

On Sun, 17 May 2009, Atom Smasher wrote:

> in a script, but i can't reproduce it outside of the script... the 
> weirder thing is that i can get this working when i test it:
> 	sysread -i 0 -o 1 -t 200 <& p
> but in the script mentioned above, it fails:
===========

doh!!

the script needs zsh/system loaded!


-- 
         ...atom

  ________________________
  http://atom.smasher.org/
  762A 3B98 A3C3 96C9 C6B7 582A B88D 52E4 D9F5 7808
  -------------------------------------------------

 	"The Final Act of the Uruguay Round, marking the conclusion of
 	 the most ambitious trade negotiation of our century, will
 	 give birth - in Morocco - to the World Trade Organization,
 	 the third pillar of the New World Order, along with the
 	 United Nations and the International Monetary Fund."
 		-- Part of full-page advertisement
 		by the government of Morocco in
 		The New York Times (April 1994)


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

* Re: runaway zselect
  2009-05-17  4:43 runaway zselect Atom Smasher
  2009-05-17  7:58 ` Atom Smasher
@ 2009-05-17 21:47 ` Bart Schaefer
  2009-05-17 23:28   ` Atom Smasher
  1 sibling, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2009-05-17 21:47 UTC (permalink / raw)
  To: zsh-users

On May 17,  4:43pm, Atom Smasher wrote:
}
} i would expect zselect to return an error and stop the while loop when
} "w" stops producing output.

That's not how the select() system call works.  The descriptor is still
open on the reading end of the pipe even though the descriptor on the
writing end of the pipe has been closed.  That means from select()'s
point of view, the descriptor is available for reading, even though the
only thing that can be read from it is the end-of-file condition.

If this were not the case, zselect would fail as soon as the writer
exits, even if all output from the writer had not yet been consumed.
(Remember that there is OS-level buffering involved, so the writing
end may be closed before the pipe is "empty".)

What you need to fix this is to test the result of "read" as well.
Either:

    while zselect -r 0 && read -r foo
    do
	print -r -- $foo
    done <<( w )

Or equivalently:

    while zselect -r 0
    do
	read -r foo || break
	print -r -- $foo
    done <<( w )


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

* Re: runaway zselect
  2009-05-17 21:47 ` Bart Schaefer
@ 2009-05-17 23:28   ` Atom Smasher
  0 siblings, 0 replies; 4+ messages in thread
From: Atom Smasher @ 2009-05-17 23:28 UTC (permalink / raw)
  To: zsh-users

cool... thanks!


On Sun, 17 May 2009, Bart Schaefer wrote:

> On May 17,  4:43pm, Atom Smasher wrote:
> }
> } i would expect zselect to return an error and stop the while loop when
> } "w" stops producing output.
>
> That's not how the select() system call works.  The descriptor is still 
> open on the reading end of the pipe even though the descriptor on the 
> writing end of the pipe has been closed.  That means from select()'s 
> point of view, the descriptor is available for reading, even though the 
> only thing that can be read from it is the end-of-file condition.
>
> If this were not the case, zselect would fail as soon as the writer 
> exits, even if all output from the writer had not yet been consumed. 
> (Remember that there is OS-level buffering involved, so the writing end 
> may be closed before the pipe is "empty".)
>
> What you need to fix this is to test the result of "read" as well. 
> Either:
>
>    while zselect -r 0 && read -r foo
>    do
> 	print -r -- $foo
>    done <<( w )
>
> Or equivalently:
>
>    while zselect -r 0
>    do
> 	read -r foo || break
> 	print -r -- $foo
>    done <<( w )
>
>


-- 
         ...atom

  ________________________
  http://atom.smasher.org/
  762A 3B98 A3C3 96C9 C6B7 582A B88D 52E4 D9F5 7808
  -------------------------------------------------

 	"I am committed to helping Ohio deliver its electoral
 	 votes to the president [Bush] next year"
 		-- Walden O'Dell, CEO of Diebold
 		August 2003


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

end of thread, other threads:[~2009-05-17 23:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-05-17  4:43 runaway zselect Atom Smasher
2009-05-17  7:58 ` Atom Smasher
2009-05-17 21:47 ` Bart Schaefer
2009-05-17 23:28   ` Atom Smasher

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