Comments (6)
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.
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.
FWIW the adj_timeout implementation in bufferevent_openssl suffers from the same bug.
from libevent.
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.
Oh say; the underlying issue is #5.
from libevent.
Should work now in master -- I added the new API I mentioned above.
from libevent.
Related Issues (20)
- [bug] Event activated by event_active on another thread may loss while event timeout HOT 4
- clang: error: linker command failed with exit code 1 (use -v to see invocation) make[1]: *** [sample/le-proxy] Error 1 HOT 1
- Fix MbedTLS and ZLIB for windows on CI HOT 1
- May bug in evhttp_connection_free_on_completion when the evhttp_connection fail to connect HOT 2
- Security advisory link is broken HOT 2
- find_dependency for MBedTLS failed for LibeventConfig.cmake HOT 1
- Test Failure and Warnings in http/bad_request under SELECT Event Mechanism on Raspberry Pi HOT 2
- install DESTINATIONs inconsistent.
- How to implement a websocket client? HOT 6
- bufferevent_write does not immediately write data to the socket HOT 4
- Use checksec to scan the libevent_pthreads-2.1.so.7.0.1 file FORTIFY item is 'No' HOT 5
- How to deal with bufferevent pointer when used externally? HOT 5
- Upgrading libevent based TCP server implementation from commit id 665d79f1 to ec8d7a5a results in random crashes HOT 5
- evbuffer use after free HOT 1
- Problem building when MACOSX_DEPLOYMENT_TARGET set on maxOS HOT 6
- question about `bufferevent_filter_new` and `bufferevent_getcb` HOT 1
- stack-buffer-overflow at evutil_parse_sockaddr_port HOT 15
- HTTP Server sample fails to compile for Windows x64
- feature request: websocket api including client and server
- bufferevent_replacefd close socket when bufferevent‘s socket fd is eq new fd HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from libevent.