Skip to content

Commit

Permalink
Fix for the broken I/O watch conditions on child exit under OSX
Browse files Browse the repository at this point in the history
Under OSX, after child death, the read watches receive input
conditions instead of error conditions, so we convert the termination
statuses (ERROR and EOF) into their respective conditions. Should not
hurt the other OS.
  • Loading branch information
zhekov committed Apr 18, 2015
1 parent bfa93af commit 902681d
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/spawn.c
Expand Up @@ -665,13 +665,14 @@ static gboolean spawn_read_cb(GIOChannel *channel, GIOCondition condition, gpoin
if (condition & (G_IO_IN | G_IO_PRI))
{
gsize chars_read;
GIOStatus status;

if (line_buffer)
{
gsize n = line_buffer->len;

while ((g_io_channel_read_chars(channel, line_buffer->str + n,
DEFAULT_IO_LENGTH, &chars_read, NULL) == G_IO_STATUS_NORMAL))
while ((status = g_io_channel_read_chars(channel, line_buffer->str + n,
DEFAULT_IO_LENGTH, &chars_read, NULL)) == G_IO_STATUS_NORMAL)
{
g_string_set_size(line_buffer, n + chars_read);

Expand Down Expand Up @@ -706,8 +707,8 @@ static gboolean spawn_read_cb(GIOChannel *channel, GIOCondition condition, gpoin
}
else
{
while (g_io_channel_read_chars(channel, buffer->str, sc->max_length,
&chars_read, NULL) == G_IO_STATUS_NORMAL)
while ((status = g_io_channel_read_chars(channel, buffer->str, sc->max_length,
&chars_read, NULL)) == G_IO_STATUS_NORMAL)
{
g_string_set_size(buffer, chars_read);
/* data only, failures are reported separately below */
Expand All @@ -717,6 +718,14 @@ static gboolean spawn_read_cb(GIOChannel *channel, GIOCondition condition, gpoin
break;
}
}

/* Under OSX, after child death, the read watches receive input conditions instead
of error conditions, so we convert the termination statuses into conditions.
Should not hurt the other OS. */
if (status == G_IO_STATUS_ERROR)
failure |= G_IO_ERR;
else if (status == G_IO_STATUS_EOF)
failure |= G_IO_HUP;
}

if (failure) /* we must signal the callback */
Expand All @@ -734,7 +743,7 @@ static gboolean spawn_read_cb(GIOChannel *channel, GIOCondition condition, gpoin
g_string_truncate(buffer, 0);
}

sc->cb.read(buffer, condition, sc->cb_data);
sc->cb.read(buffer, condition | failure, sc->cb_data);
}

if (buffer != sc->buffer)
Expand Down

0 comments on commit 902681d

Please sign in to comment.