http_client: don't arm the websocket timeout after a failed connect #204
@@ -582,7 +582,8 @@ void http_co_t::start_ws_connection()
|
||||
{
|
||||
stackin();
|
||||
resolve_and_connect();
|
||||
if (request_timeout > 0)
|
||||
// As in send_request(): skip it if the connection has already failed
|
||||
if (request_timeout > 0 && response_callback && timeout_id < 0)
|
||||
{
|
||||
timeout_id = tfd->set_timer(request_timeout, false, [this](int timer_id)
|
||||
{
|
||||
|
||||
@@ -10,13 +10,16 @@
|
||||
|
||||
static const std::string request = "GET / HTTP/1.1\r\nHost: test\r\nConnection: keep-alive\r\n\r\n";
|
||||
|
||||
// A request that fails inside http_request() must not leave its timeout timer armed.
|
||||
enum { FAIL_REQUEST, FAIL_REQUEST_NEXT_IN_CB, FAIL_WEBSOCKET };
|
||||
|
||||
// A request or websocket that fails inside http_request() or open_websocket() must not
|
||||
// leave its timeout timer armed, also when its callback sends the next request.
|
||||
// TCP connect() to a multicast address fails at once with ENETUNREACH on Linux.
|
||||
// The leftover timer used to fail the client's next request with a false timeout
|
||||
// (or run on a freed client when the failed request's callback closed it)
|
||||
static void test_sync_error_leaves_no_timer()
|
||||
static void test_sync_error_leaves_no_timer(int mode)
|
||||
{
|
||||
printf("test_sync_error_leaves_no_timer\n");
|
||||
printf("test_sync_error_leaves_no_timer %d\n", mode);
|
||||
// Timers fire only on tick(), sockets are never polled
|
||||
timerfd_manager_t tfd(nullptr);
|
||||
tfd.set_fd_handler = [](int, bool, std::function<void(int, int)>) {};
|
||||
@@ -25,32 +28,48 @@ static void test_sync_error_leaves_no_timer()
|
||||
assert(ctx && err == "");
|
||||
http_co_t *cli = http_init(ctx);
|
||||
|
||||
std::string err1;
|
||||
http_request(cli, "224.0.0.1:1", request, { .timeout = 1000, .keepalive = true }, [&](http_message_t *msg)
|
||||
// Stays connecting: only its own 5 s timeout can end it
|
||||
std::string err1, err2, err3;
|
||||
auto send_next = [&](std::string & err_out)
|
||||
{
|
||||
http_request(cli, "127.0.0.1:1", request, { .timeout = 5000, .keepalive = true }, [&](http_message_t *msg)
|
||||
{
|
||||
err_out = msg->error;
|
||||
});
|
||||
};
|
||||
auto on_fail = [&](http_message_t *msg)
|
||||
{
|
||||
err1 = msg->error;
|
||||
});
|
||||
if (mode == FAIL_REQUEST_NEXT_IN_CB)
|
||||
send_next(err2);
|
||||
};
|
||||
if (mode == FAIL_WEBSOCKET)
|
||||
open_websocket(cli, "224.0.0.1:1", "test", "/", { .timeout = 1000 }, on_fail);
|
||||
else
|
||||
http_request(cli, "224.0.0.1:1", request, { .timeout = 1000, .keepalive = true }, on_fail);
|
||||
assert(err1 != "");
|
||||
|
||||
// Stays connecting: only its own 5 s timeout can end it
|
||||
std::string err2;
|
||||
http_request(cli, "127.0.0.1:1", request, { .timeout = 5000, .keepalive = true }, [&](http_message_t *msg)
|
||||
{
|
||||
err2 = msg->error;
|
||||
});
|
||||
if (mode != FAIL_REQUEST_NEXT_IN_CB)
|
||||
send_next(err2);
|
||||
tfd.tick((timespec){ .tv_sec = 2 });
|
||||
assert(err2 == "");
|
||||
// End the second request early: a second timer armed for it would fail the third one
|
||||
http_close(cli);
|
||||
send_next(err3);
|
||||
tfd.tick((timespec){ .tv_sec = 4 });
|
||||
assert(err2 == "HTTP request timed out");
|
||||
assert(err3 == "");
|
||||
tfd.tick((timespec){ .tv_sec = 2 });
|
||||
assert(err3 == "HTTP request timed out");
|
||||
|
||||
http_close(cli);
|
||||
http_destroy(cli);
|
||||
http_context_destroy(ctx);
|
||||
printf("test_sync_error_leaves_no_timer passed\n");
|
||||
printf("test_sync_error_leaves_no_timer %d passed\n", mode);
|
||||
}
|
||||
|
||||
int main(int narg, char *args[])
|
||||
{
|
||||
test_sync_error_leaves_no_timer();
|
||||
test_sync_error_leaves_no_timer(FAIL_REQUEST);
|
||||
test_sync_error_leaves_no_timer(FAIL_REQUEST_NEXT_IN_CB);
|
||||
test_sync_error_leaves_no_timer(FAIL_WEBSOCKET);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user