I am using libevent 2.0.4 alpha in my C application.
- [err] event.c:1961: Assertion !(ev->ev_flags & ~EVLIST_ALL) failed in event_del_internal
Aborted - Segmentation Fault. I've traced down that it occurs in the event_base_loop call
In a nutshell, I have a linked list of servers. I loop through every server, if I'm connected to that server then I create an event for the socket. I then run the event loop with a timeout. I use the timeout to break out of the event loop and check if the termination condition has been encountered in another thread. I also run the event loop with EVLOOP_ONCE to break out of the loop as soon as 1 event triggers to re-evaluate the linked list of servers (new connections established, current connections terminated, ...)
When I break of the event loop, I delete the events and free them.
Can anyone please tell me what I'm doing wrong here? Is it an internal libevent problem, or is my code messing up somewhere?
Thanks
Cheers
while (!do_terminate) {
int number_of_servers;
struct server **servers_array;
struct event **events;
number_of_servers = linked_list_to_array(servers, (void ***) & servers_array, SERVER_NODE, sizeof (struct server *));
events = (struct event **) malloc(number_of_servers * sizeof (struct event *));
int i;
int number_of_events_added;
number_of_events_added = 0;
for (i = 0; i < number_of_servers; i++) {
struct server *server;
server = servers_array[i];
pthread_rwlock_rdlock(&(server->lock));
if (server->is_socket_connected) {
struct client_connector_receive_incoming_packet_callback_arguments *client_connector_receive_incoming_packet_callback_arguments;
client_connector_receive_incoming_packet_callback_arguments = (struct client_connector_receive_incoming_packet_callback_arguments *) malloc(sizeof (struct client_connector_receive_incoming_packet_callback_arguments));
client_connector_receive_incoming_packet_callback_arguments->server = server;
client_connector_receive_incoming_packet_callback_arguments->client_connector_receive_queue = client_connector_receive_queue;
client_connector_receive_incoming_packet_callback_arguments->dump_log_queue = dump_log_queue;
events[i] = event_new(event_base, server->socket_descriptor, EV_READ, client_connector_receive_incoming_packet_callback, client_connector_receive_incoming_packet_callback_arguments);
event_add(events[i], NULL);
number_of_events_added++;
}
pthread_rwlock_unlock(&(server->lock));
}
if (number_of_events_added) {
struct timeval timeout;
timeout.tv_sec = DEFAULT_TIMEOUT_SECONDS;
timeout.tv_usec = DEFAULT_TIMEOUT_MICRO_SECONDS;
event_base_loopexit(event_base, &timeout);
event_base_loop(event_base, EVLOOP_ONCE);
for (i = 0; i < number_of_servers; i++) {
event_del(events[i]);
event_free(events[i]);
}
if (events) {
free(events);
events = NULL;
}
}
else
sleep(DEFAULT_TIMEOUT_SECONDS);
pthread_mutex_lock(do_terminate_lock);
do_terminate = *(client_connector_receive_incoming_packets_arguments->do_terminate);
pthread_mutex_unlock(do_terminate_lock);
}