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

Re: [Libevent-users] Disabling callbacks and closing - block until callbacks finished



Hi,


You are in a bit of a problem here.


You could assign a unique id for each connection (uint64 should suffice). All active connection ids be located in a hash set.


In any case I suggest the cleanup be done in the libevent thread (if possible). It will save you alot of trouble.


Sent from my LG Mobile


------ Original message------

From: Azat Khuzhin

Date: Sat, Nov 8, 2014 17:23

To: libevent-users@xxxxxxxxxxxxx;

Subject:Re: [Libevent-users] Disabling callbacks and closing - block until callbacks finished


On Mon, Oct 13, 2014 at 08:14:09AM +0000, nick@xxxxxxxx wrote:
> Situation:
> ----------
>
> I am building a multithreaded socket server built with Libevent.   The event
> loop is run in its own thread and each callback creates a task into a queue.
> A pool of worker threads then processes the tasks, (of course ensuring that
> tasks for any given connection are processed in the correct order).
>
> When a close event arrives, a task is created to close the connection and
> free all related structures.   When this task is processed by a worker
> thread, it does the following things:
>
> 1. Disable all callbacks
> 2. Close the connection
> 3. Remove all pending tasks in the task queue for this connection
>
> Problem:
> --------
>
> The problem is that after the connection is closed, there could still be
> events in the Libevent event loop either waiting to be processed in a
> callback or actually being processed by the event thread (i.e. callback
> running).   This could lead to tasks being created for the connection
> _after_ step 3 which would result in tasks in the task queue for a closed
> connection - which I want to avoid.

If there will be events in the libevent event loop that waiting to be
processes, after bufferevent_free(), than you callbacks will not be
triggered, because of bufferevent_setcb() from bufferevent_free().

If there are events that actually being processed by the event thread
(i.e. callback running), that buffevent_free() will block, because
readcb called under the lock and bufferevent_free() called under the
lock too.

So I don't see the problem here.

Cheers,
Azat.

>
> Solution?
> ---------
>
> I could use a mutex to lock the connection in the event callbacks, but I do
> not want to introduce blocking methods in the event loop thread.
>
> I could ensure that a connection is valid when I take a task.
>
> But ideally I would like to the bufferevent close method, or the callback
> disable methods to block until all curently running callbacks for that
> bufferevent have finished.   This way I could be sure that no tasks can be
> created after the connection is closed.
>
> Question:
> ---------
>
> Do the methods work like this, or is there a way to achieve this?
>
> Many thanks in advance.
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.