lcb_make_http_request is failing and the wait is hanging forever

Hello,

When I execute lcb_make_http_request in a loop, after a number of executions (which is variable) it will return a fail and the wait will hang forever.
Here is the code:

    cmd.version = 0;
    cmd.v.v0.path = "_design/test/_view/chaincode?connection_timeout=60000&limit=10&skip=0";
    cmd.v.v0.npath = strlen(cmd.v.v0.path);
    cmd.v.v0.body = bytes;
    cmd.v.v0.nbody = nbytes;
    cmd.v.v0.method = LCB_HTTP_METHOD_GET;
    cmd.v.v0.chunked = 1;
    cmd.v.v0.content_type = "application/json";
 
    for (int i =0;  i<= 50000; i++)
    {
        oprc = lcb_make_http_request(instance, NULL, LCB_HTTP_TYPE_VIEW, &cmd, &req);
       if (oprc != LCB_SUCCESS)
        {
            fprintf(stderr, "Failed to get view : %d\n", i);
        }
        oprc = lcb_wait(instance);
 
    }

After 28229 executions I get: Failed to get view : 28230 and the wait will then hang forever.

Can you please advise on this?

Thanks.

1 Answer

« Back to question.

lcb_wait() by design invokes the event loop and waits for any pending events to complete. The function should only be called if you have at least one pending callback to be invoked.

If a Couchbase operation (e.g. store, get, arithmetic, http, etc) return with a failure status code, it means that for some reason the library could not schedule your request. As a result it means your callback will never be invoked and lcb_wait() should not be called to wait for it.

Note that the operation may still fail when the callback is invoked, but these failures will not be scheduling failures, but will be either unexpected network errors or negative protocol responses.

So basically you want something like:

lcb_error_t err = lcb_make_http_request(....);
if (err == LCB_SUCCESS) {
    lcb_wait(instance);
} else {
    fprintf(stderr, "Couldn't schedule: [%d] %s\n", err, lcb_strerror(instance, err));
}

Thanks for the information. I have changed the code to invoke the lcb_wait() only in case of success. As expected the operation is still failing with the error: Couldn't schedule: [24] Connection failure.
At the end of the loop somehow the lcb_make_http_request returns success so the lcb_wait() is again called but it hangs out.
Do you have any ideea why this Connection failure could happen after a number of http requests? I have checked with netstat and it seems that on the client side for each lcb_make_http_request a different port is used. I see a very long list like:

tcp 0 0 172.16.134.79:48789 172.16.136.151:8092 TIME_WAIT
tcp 0 0 172.16.134.79:52582 172.16.136.151:8092 TIME_WAIT
tcp 0 0 172.16.134.79:57150 172.16.136.151:8092 TIME_WAIT
tcp 0 0 172.16.134.79:46971 172.16.136.151:8092 TIME_WAIT
tcp 0 0 172.16.134.79:56882 172.16.136.151:8092 TIME_WAIT

Thanks.