Coder Social home page Coder Social logo

Comments (6)

diwakergupta avatar diwakergupta commented on June 17, 2024

Here's a sample program:

#include <errno.h>
#include <stdio.h>
#include <time.h>

#include <event.h>

void read_callback(struct bufferevent* be, void* ctx) {
    printf("Got read callback, disabling timeout\n");
    // This *should* disable READ and WRITE timeouts, but doesn't
    bufferevent_set_timeouts(be, NULL, NULL);
}

void write_callback(struct bufferevent* be, void* ctx) {
    printf("Finished write\n");
}

void event_callback(struct bufferevent* be, int16_t what, void* ctx) {
    printf("Got a callback %d\n", what);
    if (what & BEV_EVENT_CONNECTED) {
        printf("CONNECTED\n");
        bufferevent_enable(be, EV_READ | EV_WRITE);
        bufferevent_setcb(be, read_callback, write_callback, event_callback, ctx);
        // set a read timeout of 2 seconds
        struct timeval tv = {2, 0};

        bufferevent_set_timeouts(be, &tv, NULL);
        char* hello = "hello";
        bufferevent_write(be, (uint8_t*) hello, 5);
        return;
    }

    if (what & BEV_EVENT_EOF) {
        printf("EOF\n");
    }
    if (what & BEV_EVENT_TIMEOUT) {
        printf("TIMEOUT\n");
    }
    if (what & BEV_EVENT_ERROR) {
        printf("ERROR %d\n", evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR()));
    }
    if (what & BEV_EVENT_READING) {
        printf("READING\n");
    }
    if (what & BEV_EVENT_WRITING) {
        printf("WRITING\n");
    }
    struct event_base* eb = (struct event_base*) ctx;
    event_base_loopbreak(eb);
}

int main(int argc, char* argv[]) {
    evthread_use_pthreads();

    struct event_base* eb = event_base_new();
    struct bufferevent* be = bufferevent_socket_new(eb, -1,
        BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS |
        BEV_OPT_THREADSAFE | BEV_OPT_UNLOCK_CALLBACKS);
    bufferevent_setcb(be, NULL, NULL, event_callback, eb);

    if (bufferevent_socket_connect_hostname(be, NULL, AF_INET,
            "localhost", 21234) < 0) {
        printf("Failed");
    }

    printf("Starting event loop\n");
    event_base_dispatch(eb);
    printf("Exiting event loop\n");
    bufferevent_free(be);
    event_base_free(eb);
}

I run it against a simple echo server. What I expect to see is that we get a connection callback, then a write callback followed by a read callback. After that, the program should just loop waiting for more I/O but instead, I still see the 2s timeout even though the read_callback explicitly disables all timeouts on that bufferevent.

from libevent.

diwakergupta avatar diwakergupta commented on June 17, 2024

I believe the problem is the way adj_timeouts is invoked. Since I'm using bufferevent_socket_new, adj_timeouts maps to be_socket_adj_timeouts. However, that function does not do all of the cleanup done by _bufferevent_generic_adj_timeouts. In particular, even though the caller passes in NULL values, the ev_read/ev_write events never get deleted.

from libevent.

diwakergupta avatar diwakergupta commented on June 17, 2024

FWIW the adj_timeout implementation in bufferevent_openssl suffers from the same bug.

from libevent.

nmathewson avatar nmathewson commented on June 17, 2024

Ugh; the real problem here is that the code assumes that "event_add(ev, NULL);" will clear an existing timeout on an event, but it won't. I'm leery of changing the behavior of event_add(ev,NULL), though, since existing code may be relying on it. I think we need a new API for removing only the timeout on an event but leaving the rest added.

Calling event_del() isn't right here, since we don't want to remove the read event--we only want to remove its timeout. Nor can we say event_del(ev); event_add(ev,NULL); -- that would lose an invocation of the event if it's currently activated.

from libevent.

nmathewson avatar nmathewson commented on June 17, 2024

Oh say; the underlying issue is #5.

from libevent.

nmathewson avatar nmathewson commented on June 17, 2024

Should work now in master -- I added the new API I mentioned above.

from libevent.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.