Closed TimonNoethlichs closed 1 week ago
Ah, great news! I can trigger it with the library itself:
Sending one message from the server:
ssl_server_.Get("/silent", [](const httplib::Request& http_request, httplib::Response& http_response) {
http_response.set_chunked_content_provider(
"text/event-stream",
[initial_message_sent = false](size_t offset, httplib::DataSink& sink) mutable {
if (!initial_message_sent) {
std::string data = "a";
sink.write(data.c_str(), data.size());
initial_message_sent = true;
}
std::this_thread::sleep_for(2s);
return true;
},
[](bool success) {});
});
And the client sends a message in reply to the server's event:
#include <chrono>
#include <iostream>
#include <thread>
#define CPPHTTPLIB_OPENSSL_SUPPORT
#include "cpp-httplib/httplib.h"
int main() {
using namespace std::chrono_literals;
httplib::SSLClient client{"10.149.108.178"};
client.enable_server_certificate_verification(false);
client.Get(
"/silent",
httplib::Headers{},
[&](const httplib::Response& response) { return true; },
[&](const char* data, size_t data_length) {
if (client.is_socket_open()) {
std::string data = "asdf";
std::ignore = write(client.socket(), data.c_str(), data.size());
}
return true;
});
while (true) {
std::this_thread::sleep_for(120s);
}
return 0;
}
In my experience, closing an event-stream requires a server-side call to sink.done(), which you can try
@TimonNoethlichs I don't fully understand what you are trying to do, but my sample SSE server code might be helpful. https://github.com/yhirose/cpp-httplib/blob/52a18c78a52b1bcffc10506fe5fdc76568b3c646/example/ssesvr.cc#L71-L87
Hi, first off: we really enjoy the library and it works really well. So thank you all! But we are facing the following issue:
Given you are using Firefox to connect to a server-sent event stream on a SSLServer. When closing the tab in Firefox and the stream does not send anymore data thereafter. Then the stream's socket stays in the state CLOSE_WAIT indefinitely.
After a few times the thread pool is exhausted and the server is unresponsive.
This is the minimal example code for the server:
In the following I try to summarize my findings:
I modified the httplib.h file for better strace logging:
When I first connect to the running server using Firefox via https://10.149.108.178/silent:
and strace repeats this pattern:
Closing the tab in Firefox gives me:
and strace repeats this pattern:
The decrypted Wireshark logs shows:
and the message the strace shows in
Is the close_notify from the wireshark log.
Here I have the decrypted Wireshark log for a curl equivalent:
Let me know if you need any other information. Thank you!