babelouest / ulfius

Web Framework to build REST APIs, Webservices or any HTTP endpoint in C language. Can stream large amount of data, integrate JSON data with Jansson, and create websocket services
https://babelouest.github.io/ulfius
GNU Lesser General Public License v2.1
1.07k stars 183 forks source link

ulfius_send_http_request get a error: U_ERROR_LIBCURL #205

Closed zhangsen1860 closed 2 years ago

zhangsen1860 commented 2 years ago

Hello, when I use the function ulfius_send_http_request to send rest requests, I got some U_ERROR_LIBCURL errors. And there seems to be a certain rule, if I send 1000 requests, I got half U_OK and half U_ERROR_LIBCURL, and mostly like this: 1st, 3rd, 5th, 7th, 9th... got U_OK, 2nd, 4th, 6th, 8th, 10th... got error.

How could I handle this?

babelouest commented 2 years ago

Hello @zhangsen1860 ,

It's hard to say what's wrong with your code with so few information. You can start by enabling logs, so you'll have a better idea at where and why it fails:

int main(void) {
// ...
y_init_logs("my app", Y_LOG_MODE_CONSOLE, Y_LOG_LEVEL_DEBUG, NULL, "Starting my app");

// ...
if (ulfius_send_http_request(req, resp) != U_OK) {
  y_log_message(Y_LOG_LEVEL_ERROR, "Error ulfius_send_http_request");
}
// ...

y_close_logs();
return 0;
}
zhangsen1860 commented 2 years ago

I enabled the logs, and I got the logs below:

2021-12-03T13:15:46 - rest_server ERROR: Ulfius - Error curl_easy_perform 2021-12-03T13:15:46 - rest_server DEBUG: Ulfius - libcurl error: 56, error message 'Failure when receiving data from the peer' 2021-12-03T13:15:47 - rest_server ERROR: Ulfius - Error curl_easy_perform 2021-12-03T13:15:47 - rest_server DEBUG: Ulfius - libcurl error: 52, error message 'Server returned nothing (no headers, no data)' 2021-12-03T13:15:48 - rest_server ERROR: Ulfius - Error curl_easy_perform 2021-12-03T13:15:48 - rest_server DEBUG: Ulfius - libcurl error: 52, error message 'Server returned nothing (no headers, no data)' 2021-12-03T13:15:48 - rest_server ERROR: Ulfius - Error curl_easy_perform 2021-12-03T13:15:48 - rest_server DEBUG: Ulfius - libcurl error: 52, error message 'Server returned nothing (no headers, no data)' 2021-12-03T13:15:49 - rest_server ERROR: Ulfius - Error curl_easy_perform 2021-12-03T13:15:49 - rest_server DEBUG: Ulfius - libcurl error: 52, error message 'Server returned nothing (no headers, no data)' 2021-12-03T13:15:50 - rest_server ERROR: Ulfius - Error curl_easy_perform 2021-12-03T13:15:50 - rest_server DEBUG: Ulfius - libcurl error: 56, error message 'Failure when receiving data from the peer' 2021-12-03T13:15:51 - rest_server ERROR: Ulfius - Error curl_easy_perform 2021-12-03T13:15:51 - rest_server DEBUG: Ulfius - libcurl error: 56, error message 'Failure when receiving data from the peer' 2021-12-03T13:15:53 - rest_server ERROR: Ulfius - Error curl_easy_perform 2021-12-03T13:15:53 - rest_server DEBUG: Ulfius - libcurl error: 52, error message 'Server returned nothing (no headers, no data)' 2021-12-03T13:15:54 - rest_server ERROR: Ulfius - Error curl_easy_perform 2021-12-03T13:15:54 - rest_server DEBUG: Ulfius - libcurl error: 56, error message 'Failure when receiving data from the peer'

My client-side code: //... ulfius_init_request(&request); ulfius_set_request_properties(&request, U_OPT_HTTP_VERB, "POST", U_OPT_HTTP_URL, url, U_OPT_TIMEOUT, 300, U_OPT_BINARY_BODY, jsonstr, o_strlen(jsonstr), U_OPT_HEADER_PARAMETER, "Content-Type", "application/json", U_OPT_NONE); // Required to close the parameters list ulfius_init_response(&response); res = ulfius_send_http_request(&request, &response); if (res != U_OK) { zlog(ZERROR, REST_COMMFAIL, "Communication error! errorcode = %d.", res); } //...

server-side code: //... ulfius_set_string_body_response(response, 200, appresstr); if(post_params) free(post_params); if(appresstr) free(appresstr); return U_CALLBACK_CONTINUE; //...

I don't know why the libcurl response the error.

babelouest commented 2 years ago

I don't know why the libcurl response the error.

I don't know either but the libcurl error is quite straightforward: 'Failure when receiving data from the peer' 'Server returned nothing (no headers, no data)'

at first it seems the problem isn't on the client side, but rather on the server side or the network, You can try to log and count every time your server callback function is called and compare to the number of client requests.

zhangsen1860 commented 2 years ago

Actually, I have captured the network package, and found some http request had no reponse. Also , the server callback function is not called. It means when the error occured, the erver callback function is not called.

zhangsen1860 commented 2 years ago

And I want to know under what circumstances will the server callback function not be called

babelouest commented 2 years ago

And I want to know under what circumstances will the server callback function not be called

Maybe if there are too much connections at the same time and the server can't handle them, maybe your network drops packets, maybe other reasons.

zhangsen1860 commented 2 years ago

This phenomenon will occur after 300 consecutive calls. After this phenomenon, continue to call, and there will be a failure every two calls. And I add log to ulfius, find no receive data in ulfius, It seems the connection is not ESTABLISHED. This is very confused me. Thank you for your reply, I will doble check my code for some clus.

babelouest commented 2 years ago

Hello @zhangsen1860 , do you have more information, or even better, a sample code to reproduce your problem?

zhangsen1860 commented 2 years ago

Hello, the problem is solved. There is something wrong with the server-side, in the server I called another library and needed to create a handle every time, I deleted this part and the problem had disappeared. I modified my code, but I still don't know the root cause of the problem.

babelouest commented 2 years ago

Maybe the library you use isn't thread safe. In that case you can try and use mutexes in the callback function for example. But since your problem doesn't seem to be ulfius related, I think this issue can be closed then. What do you think?

zhangsen1860 commented 2 years ago

Yes, I think so.