zsh-workers
 help / color / mirror / code / Atom feed
From: "Jun T." <takimoto-j@kba.biglobe.ne.jp>
To: zsh-workers@zsh.org
Subject: accessing zpty after child has finished (Mac OS X)
Date: Tue, 17 Dec 2013 01:33:00 +0900	[thread overview]
Message-ID: <C87F9A07-1C5B-46BA-B2F7-DB07880651A7@kba.biglobe.ne.jp> (raw)

On Mac OS X, 'make check' succeeds normally, but zpty still has
some problems:

mac$ zpty x date
(date finishes immediately)
mac$ zpty -r x      # (1): no output
mac$ zpty
(20359) x: date     # should be (finished)
mac$ zpty -w x foo
(2): zsh goes into an infinite loop (100% cpu usage).

Suppose the child on the slave side of the pty has already finished,
and we try to read from the master by 'ret = read(master, buf, 1)'.
On Linux, what we get is either
  the data written to the slave but not yet read from the master,
  or ret = -1 (with errno set to EIO) if no such data remains.

On Mac OS X, the data not yet read by the master is lost,
and read() always returns 0 (not -1). 

The infinite loop (2) is in ptywritestr(); the for loop starting at
line 676 of zpty.c never breaks and loops forever, because checkptycmd()
(called at line 689) doesn't set cmd->fin. The following patch solves
this problem, and I hope it does no harm on other OS.

I feel the data loss (1) is OK at least for now.
The only way I can think of to avoid the data loss is to open the
slave in the parent and keep the file descriptor open in the parent.
But I believe it causes trouble on other OS.


diff --git a/Src/Modules/zpty.c b/Src/Modules/zpty.c
index fca0cc1..b0c339b 100644
--- a/Src/Modules/zpty.c
+++ b/Src/Modules/zpty.c
@@ -510,7 +510,7 @@ checkptycmd(Ptycmd cmd)
 
     if (cmd->read != -1 || cmd->fin)
 	return;
-    if ((r = read(cmd->fd, &c, 1)) < 0) {
+    if ((r = read(cmd->fd, &c, 1)) <= 0) {
 	if (kill(cmd->pid, 0) < 0) {
 	    cmd->fin = 1;
 	    zclose(cmd->fd);
 



             reply	other threads:[~2013-12-16 16:33 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-16 16:33 Jun T. [this message]
2013-12-16 18:15 ` Bart Schaefer

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=C87F9A07-1C5B-46BA-B2F7-DB07880651A7@kba.biglobe.ne.jp \
    --to=takimoto-j@kba.biglobe.ne.jp \
    --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).