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

[Libevent-users] segfault issue



Hi!

I'm using bufferevent in my proxy project and I have a bug. The entire program is single-thread, but apparently the error callback can be called while something is still running (ie. a parser) into server/client read callbacks, causing a segmentation fault, since the error callback destroy the Connection class.
Is it the expected behavior? Should I add a state to check when a callback is running and not delete on error callback directly?

The related code is listed below (server_error_cb, server_read_cb functions are similar, but not listed):

##############################################

// static
void Connection::client_error_cb(struct bufferevent* bufev, short error,
                                 void* arg) {
  Connection* conn = static_cast<Connection*>(arg);
  bool done = false;

  DLOG(2, "%s: called", __func__);

  if (error & EVBUFFER_EOF) {
    // connection has been closed, do any clean up here
    done = true;
  } else if (error & EVBUFFER_ERROR) {
    // check errno to see what error occurred
    done = true;
  }
  if (done) {
    // TODO: disconnect / states (delete or mark)
    delete conn;
  }
}

// static
void Connection::client_read_cb(struct bufferevent* bufev, void* arg) {
  Connection* conn = static_cast<Connection*>(arg);
  struct evbuffer* input = EVBUFFER_INPUT(bufev);
  size_t len;

  DLOG(2, "%s: called", __func__);

  // TODO: disconnect / states (set)

  while (1) {
    len = EVBUFFER_LENGTH(input);
    if (len <= 0)
      break;

    if (conn->server_port == MSN_PORT) {
      if (msn::parse_packet(0, input, conn) == -1)
        evbuffer_drain(input, len);
    } else {
      DLOG(1, " ... read %ld bytes", len);

      bufferevent_write_buffer(conn->server_bufev, input);
      evbuffer_drain(input, len);
    }
  }

  // TODO: disconnect / states (unset)

  //TODO: disconnect / states (check for delete)
}

##############################################

Thanks in advance,


--
w