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

[Libevent-users] [HELP] How to safely free a defer callback bufferevent in readcb



Hi,

I am working on a http proxy program based on le-proxy. the proxy has
a feature to block requests to some special urls, then return to the
client a 403 forbidden or 302 redirect response.

I tried to do it in bufferevent readcb, here are the code sample i wrote:

static void
readcb(struct bufferevent *bev, void *ctx)
{
  struct bufferevent *partner = (struct bufferevent *)ctx;

  int r = session_handle(bev, partner);
  if (r == 1) { //the url need to be blocked and the output evbuffer
of bev has been set to "HTTP/1.0 403\r\n\r\n"
    bufferevent_disable(bev, EV_READ);
    struct evbuffer *b = bufferevent_get_input(bev);
    evbuffer_drain(b, evbuffer_get_length(b));

    if (partner) {
      bufferevent_disable(partner, EV_READ|EV_WRITE);
      bufferevent_free(partner);
    }
    if (evbuffer_get_length(bufferevent_get_output(bev))) {
      bufferevent_setcb(bev, NULL, close_on_finished_writecb, eventcb, NULL);
    }
    else {
      bufferevent_free(bev);
    }
  }
  //otherwise
  .....
}

with defer callback, when the above code is called, (ie. a request to
the to-be-blocked url is found)
I got the following warnings:
[warn] Epoll MOD on fd 10 failed.  Old events were 6; read change was
2; write change was 0.: Bad file descriptor

The warning does not appear if the BEV_OPT_DEFER_CALLBACKS is not set.

Can anyone please explain where this warning comes from, and how to
avoid such a warning?


Thanks very much.

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