Discussion:
callback firing many (infinite) times for stdin
Richard Jones
2008-11-06 18:30:23 UTC
Permalink
I'm trying to use libevent for reading data from stdin, and my callback
is being called over and over, even though i'm only sending a couple of
bytes to stdin.
Here's the jist of it:

Main:
event_init();
event_set(&ev, 0 /*stdin*/, EV_READ | EV_PERSIST, read_stdin, NULL);
event_add(&ev, NULL);
event_dispatch();


void read_stdin(int fd, short flags, void *data)
{
int b;
b = read(fd, buffer, 0);
fprintf(stderr, "Read %d bytes from %d:", b, fd);
fprintf(stderr, "'%.*s' ", b, buffer);
return;
}

Now if i do:

mkfifo fifo
./prog < fifo

and then in another shell:
echo -n "12345" > fifo

i see:
Read 5 bytes from 0:'12345' Read 0 bytes from 0:'' Read 0 bytes from
0:'' Read 0 bytes from 0:'' ...

And the callback keeps firing forever, even tho I'm not sending any more
data. I can't find anything in the docs about having to acknowledge
events or dismiss them once fired or anything - what am i missing?

Thanks,
RJ
Thomas Harning
2008-11-06 18:37:08 UTC
Permalink
......
Read 5 bytes from 0:'12345' Read 0 bytes from 0:'' Read 0 bytes from
0:'' Read 0 bytes from 0:'' ...
And the callback keeps firing forever, even tho I'm not sending any more
data. I can't find anything in the docs about having to acknowledge
events or dismiss them once fired or anything - what am i missing?
The successful read of zero bytes indicates end-of-stream.
Richard Jones
2008-11-06 18:48:25 UTC
Permalink
Post by Thomas Harning
......
Read 5 bytes from 0:'12345' Read 0 bytes from 0:'' Read 0 bytes from
0:'' Read 0 bytes from 0:'' ...
And the callback keeps firing forever, even tho I'm not sending any more
data. I can't find anything in the docs about having to acknowledge
events or dismiss them once fired or anything - what am i missing?
The successful read of zero bytes indicates end-of-stream.
Ah, so the < redirection in the shell is sending eof.. If i use:

tail -f fifo | ./prog

i'm able to write multiple times to the fifo, which does what i want as
tail -f doesnt pass on eof.. that's doing what i want now.

thanks,
RJ
Tero Marttila
2008-11-06 19:03:48 UTC
Permalink
Post by Richard Jones
Post by Thomas Harning
......
Read 5 bytes from 0:'12345' Read 0 bytes from 0:'' Read 0 bytes from
0:'' Read 0 bytes from 0:'' ...
And the callback keeps firing forever, even tho I'm not sending any more
data. I can't find anything in the docs about having to acknowledge
events or dismiss them once fired or anything - what am i missing?
The successful read of zero bytes indicates end-of-stream.
No, the EOF comes from the `echo` process closing the write end of the fifo/pipe that your shell opened for it.

The < redirection just causes your shell to pass in the read end of the fifo as the stdin fd (and in fact, the shell
will block on opening the fifo, and only exec proc once echo opens the fifo for write).
Post by Richard Jones
tail -f fifo | ./prog
i'm able to write multiple times to the fifo, which does what i want as
tail -f doesnt pass on eof.. that's doing what i want now.
`tail -f` just does nanosleep+read, an alternative solution is to have prog re-open the fifo (using O_NONBLOCK) after
getting EOF from read, this works properly on linux, at least.
Post by Richard Jones
thanks,
RJ
_______________________________________________
Libevent-users mailing list
http://monkeymail.org/mailman/listinfo/libevent-users
Loading...