Cygwin: FIFO: fix hit_eof
According to Posix, a FIFO open for reading is at EOF if it is empty and there are no writers open. The only way to test this is to poll the fifo_client_handlers as in raw_read and select.cc:peek_fifo. The current hit_eof instead relies on the value of nconnected, which can be out of date. On the one hand, it doesn't take into account writers that were connected but have since closed. On the other hand, it doesn't take into account writers that are in the process of opening but haven't yet connected. Fix this by introducing a maybe_eof method that tentatively assumes EOF if there are no connected writers after polling. Then check for writers currently opening (via a new 'writer_opening' event), and wait for the fifo_reader_thread to record any new connection that was made while we were polling. To handle the needs of peek_fifo, replace the get_fc_handle method by a get_fc_handler method, and add a fifo_client_handler::get_state method. Remove the is_connected method, which was used only in peek_fifo and is no longer needed. Remove the nconnected data member, which was used only for the flawed hit_eof. Add some comments about events to fhandler.h.
This commit is contained in:
@@ -866,31 +866,45 @@ peek_fifo (select_record *s, bool from_select)
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (fh->hit_eof ())
|
||||
{
|
||||
select_printf ("read: %s, saw EOF", fh->get_name ());
|
||||
gotone = s->read_ready = true;
|
||||
if (s->except_selected)
|
||||
gotone += s->except_ready = true;
|
||||
goto out;
|
||||
}
|
||||
|
||||
fh->fifo_client_lock ();
|
||||
int nconnected = 0;
|
||||
for (int i = 0; i < fh->get_nhandlers (); i++)
|
||||
if (fh->is_connected (i))
|
||||
if (fh->get_fc_handler (i).get_state () >= fc_connected)
|
||||
{
|
||||
int n = pipe_data_available (s->fd, fh, fh->get_fc_handle (i),
|
||||
false);
|
||||
if (n > 0)
|
||||
nconnected++;
|
||||
switch (fh->get_fc_handler (i).pipe_state ())
|
||||
{
|
||||
select_printf ("read: %s, ready for read: avail %d, client %d",
|
||||
fh->get_name (), n, i);
|
||||
case FILE_PIPE_CONNECTED_STATE:
|
||||
fh->get_fc_handler (i).get_state () = fc_connected;
|
||||
break;
|
||||
case FILE_PIPE_DISCONNECTED_STATE:
|
||||
fh->get_fc_handler (i).get_state () = fc_disconnected;
|
||||
nconnected--;
|
||||
break;
|
||||
case FILE_PIPE_CLOSING_STATE:
|
||||
fh->get_fc_handler (i).get_state () = fc_closing;
|
||||
break;
|
||||
case FILE_PIPE_INPUT_AVAILABLE_STATE:
|
||||
fh->get_fc_handler (i).get_state () = fc_input_avail;
|
||||
select_printf ("read: %s, ready for read", fh->get_name ());
|
||||
fh->fifo_client_unlock ();
|
||||
gotone += s->read_ready = true;
|
||||
goto out;
|
||||
default:
|
||||
fh->get_fc_handler (i).get_state () = fc_error;
|
||||
nconnected--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
fh->maybe_eof (!nconnected);
|
||||
fh->fifo_client_unlock ();
|
||||
if (fh->maybe_eof () && fh->hit_eof ())
|
||||
{
|
||||
select_printf ("read: %s, saw EOF", fh->get_name ());
|
||||
gotone += s->read_ready = true;
|
||||
if (s->except_selected)
|
||||
gotone += s->except_ready = true;
|
||||
}
|
||||
}
|
||||
out:
|
||||
if (s->write_selected)
|
||||
|
Reference in New Issue
Block a user