Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sporadically http clients show the error that the server connection has been reset #1870

Closed
RainerSchielke opened this issue Jun 27, 2024 · 4 comments

Comments

@RainerSchielke
Copy link
Contributor

Sporadically our java client logs the message "HttpURLConnection.getInputStream java.io.IOException: Error writing to server" which ocurred because the receiving server closed the connection.

Our Windows-http client shows the windows error code 12030, which means "The connection with the server has been reset or terminated"

I increased the receive buffer size to 256k and the number of threads to 64. This reduced the occurence to about once in 1000 requests.

Then I added a retry in my clients, which helped. But I am not happy with that.

Btw. we do not use "keep alive", at server initialization we call set_keep_alive_timeout (0). Without that, every requests delays for 5 seconds.

Why resets the httplib server the connection sporadically ?

@lukasberbuer
Copy link

lukasberbuer commented Jul 11, 2024

I face the same problem getting sporadic ERR_CONNECTION_RESET from the JavaScript client with following setup:

// single threaded server
#define CPPHTTPLIB_THREAD_POOL_COUNT 1
#include <httplib.h>

httplib::Server server;
server.set_keep_alive_timeout(0);

// setup endpoints...

server.listen("0.0.0.0", 8080);

Any ideas?

Edit: I'm using version 0.12.2

@RainerSchielke
Copy link
Contributor Author

Looks like a keep-alive problem. I came up with it when I read the issue #1825 which was fixed with disabling keep-alive on the client side. Probably the function process_server_socket_core causes this:

Screenshot 2024-07-17 175043
I assume that if the number of keep alive connections is exhausted, a connection is closed and that causes the client error.
The solution is quite simple: call set_keep_alive_max_count () to increase the keep_alive_max_count to more than 5 e.g. 100 and the problem disappears. Other servers like tomcat or jetty allow 100 to 256 keep alive connections.
My very well working setup ist now:

set_keep_alive_timeout (5); // default
set_keep_alive_max_count (300);

@yhirose I suggest to increase the default value for keep_alive_max_count_ or at least mention that in the documentation.

@lukasberbuer
Copy link

Thank you for the deep dive @RainerSchielke.

Following settings seem to work for me:

#define CPPHTTPLIB_THREAD_POOL_COUNT 2
#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND 5
#define CPPHTTPLIB_KEEPALIVE_MAX_COUNT 300

Whenever I switch to a single-threaded server (#define CPPHTTPLIB_THREAD_POOL_COUNT 1), it seems to work but I face another issues of hanging requests (#454).

@yhirose
Copy link
Owner

yhirose commented Aug 9, 2024

@RainerSchielke thanks for the report. The documentation already mentions it as below, and I feel it's enough for those who know the HTTP/1.1 Keep-Alive connection spec.

image

https://github.com/yhirose/cpp-httplib?tab=readme-ov-file#keep-alive-connection

But of course, any pull request which updates README is welcome!

@yhirose yhirose closed this as completed Aug 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants