[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[Libevent-users] Strange behaviour with bufferevents



Hi,

I've been writing a libevent-based client[0] for Graphite[1] which has a
simple TCP line-based protocol so I've based it around buffevents. My
client is designed so I can set up multiple connections to different
Graphite servers and receive callbacks when each connection is
connected/disconnected. In the case of disconnecting, each connection
has its own evtimer-based reconnect callback.

However I'm noticing some strange behaviour when one of the servers is
intentionally stopped and not accepting connections. I've distilled it
into a simplified test case[2] that demonstrates the problem.

On lines #165 and #168 I create two "connections", the first one is to
the server that is down, the second one is up, (for the sake of the test
you can simulate the server with netcat). Then I initiate the connections
by queueing an evtimer which initially fires immediately, and then is
fired every 5 seconds after that until a connection is made. I'm
initiating the non-working connection first.

When I launch the test case, I'm getting strange behaviour in that my
event callback for the non-working connection is called three times in
quick succession with the events argument set to BEV_EVENT_CONNECTED,
BEV_EVENT_ERROR|BEV_EVENT_READING, and finally BEV_EVENT_ERROR. Then,
every 5 seconds I get the expected single BEV_EVENT_ERROR callback.

The event callback for the working connection is called once correctly with
BEV_EVENT_CONNECTED as I would expect.

If I swap lines #170 & #171 around so I initiate the working connection
before the non-working one, then everything works as expected, I get a
BEV_EVENT_CONNECTED event for the working connection, and a single
BEV_EVENT_ERROR event for the non-working one, both immediately and every
5 seconds subsequently.

I tried removing BEV_OPT_DEFER_CALLBACKS from the flags when I create the
bufferevents and that changes the behaviour slightly in that I just get
BEV_EVENT_ERROR|BEV_EVENT_READING immediately, followed by
BEV_EVENT_ERROR every 5 seconds.

I can't see what I'm doing wrong as if both servers are up then I don't
see any strange behaviour. I don't understand how I'm getting the
BEV_EVENT_CONNECTED event when I can't manually telnet to that
address/port. Can anyone see what I'm doing wrong?

I'm currently using Mac OS X 10.8.3 with libevent 2.0.21 installed from
MacPorts. I've tried the test case on CentOS 6.x with the same version of
libevent and I can't reproduce it which makes me think it's something
specific to either OS X or the OS X support in libevent perhaps?

Thanks

Matt

[0] https://github.com/bodgit/libevent-graphite
[1] http://graphite.readthedocs.org
[2] https://gist.github.com/bodgit/5387076
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.