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

Re: [Libevent-users] Socket stuck in close_wait state



Thank you for the explanation. This was very helpful and solved my problem.
 
Sent: Monday, June 18, 2018 at 11:01 AM
From: "Azat Khuzhin" <a3at.mail@xxxxxxxxx>
To: libevent-users@xxxxxxxxxxxxx
Subject: Re: [Libevent-users] Socket stuck in close_wait state
> Since bufferevent_free() is meant to close the socket I expected the socket to be closed on both sides, but my logic is obviously wrong.

bufferevent_free() and any close() cannot close the fd on the another
side, the code should do this, and this is what you bufferevent_free()
must do.

> void Close( bufferevent *bev, void *arg )
> {
> bufferevent_free( bev );

One may think that bufferevent_free() will call close() directly, but
this is not true for libevent 2.2 since bufferevents uses finalizers too
free the resources and this is the tricky part.

Finalizers will be runned from the loop *or* when freeing event_base.

> event_base_loopbreak( bufferevent_get_base( bev ) );

event_base_loopbreak() is unwanted here, you should be polite and exit
from the loop once it does not have any events.

> void Send()
> {
...
> event_base_dispatch( tcpbase );

*And this is the main problem of this snippet is missing event_base_free() here.*
(not only finalizers will not be runned but also internal event_base fds
will not be closed too, you can use strace/gdb/lsof and other tools to
dig into this or their analogs if you are not in linux).

P.S. and I hope you know about all other problems that this code have
(i.e. the reason why Send() is triggered multiple times after sending
one character with new line to stdin)

Regards,
Azat.
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users in the body.
*********************************************************************** To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with unsubscribe libevent-users in the body.