Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Fix import edge case crashes

Jason Boscoed6a4ecc00c9
diff --git a/include/http_client.h b/include/http_client.h
index 2611decf..972aa8e6 100644
--- a/include/http_client.h
+++ b/include/http_client.h
@@ -26,7 +26,7 @@ private:
static CURL* init_curl(const std::string& url, std::string& response);
- static CURL* init_curl_async(const std::string& url, deferred_req_res_t* req_res);
+ static CURL* init_curl_async(const std::string& url, deferred_req_res_t* req_res, curl_slist *chunk);
static size_t curl_req_send_callback(char* buffer, size_t size, size_t nitems, void *userdata);
diff --git a/src/http_client.cpp b/src/http_client.cpp
index b1962402..1ebe9145 100644
--- a/src/http_client.cpp
+++ b/src/http_client.cpp
@@ -21,8 +21,9 @@ long HttpClient::post_response(const std::string &url, const std::string &body,
long HttpClient::post_response_async(const std::string &url, http_req* request, http_res* response, HttpServer* server) {
deferred_req_res_t* req_res = new deferred_req_res_t{request, response, server};
std::unique_ptr<deferred_req_res_t> req_res_guard(req_res);
+ struct curl_slist *chunk = nullptr;
- CURL *curl = init_curl_async(url, req_res);
+ CURL *curl = init_curl_async(url, req_res, chunk);
if(curl == nullptr) {
return 500;
}
@@ -31,6 +32,8 @@ long HttpClient::post_response_async(const std::string &url, http_req* request,
curl_easy_perform(curl);
curl_easy_cleanup(curl);
+ curl_slist_free_all(chunk);
+
return 0;
}
@@ -228,9 +231,14 @@ size_t HttpClient::curl_write_async(char *buffer, size_t size, size_t nmemb, voi
}
size_t HttpClient::curl_write_async_done(void *context, curl_socket_t item) {
- //LOG(INFO) << "curl_write_async_done";
-
+ LOG(INFO) << "curl_write_async_done";
deferred_req_res_t* req_res = static_cast<deferred_req_res_t *>(context);
+
+ if(req_res->req->_req == nullptr) {
+ // underlying client request is dead, don't try to send anymore data
+ return 0;
+ }
+
req_res->res->body = "";
req_res->res->final = true;
@@ -242,7 +250,7 @@ size_t HttpClient::curl_write_async_done(void *context, curl_socket_t item) {
return 0;
}
-CURL *HttpClient::init_curl_async(const std::string& url, deferred_req_res_t* req_res) {
+CURL *HttpClient::init_curl_async(const std::string& url, deferred_req_res_t* req_res, curl_slist *chunk) {
CURL *curl = curl_easy_init();
if(curl == nullptr) {
@@ -251,7 +259,6 @@ CURL *HttpClient::init_curl_async(const std::string& url, deferred_req_res_t* re
req_res->req->data = curl;
- struct curl_slist *chunk = nullptr;
std::string api_key_header = std::string("x-typesense-api-key: ") + HttpClient::api_key;
chunk = curl_slist_append(chunk, api_key_header.c_str());
@@ -287,11 +294,6 @@ CURL *HttpClient::init_curl_async(const std::string& url, deferred_req_res_t* re
curl_easy_setopt(curl, CURLOPT_CLOSESOCKETFUNCTION, HttpClient::curl_write_async_done);
curl_easy_setopt(curl, CURLOPT_CLOSESOCKETDATA, req_res);
- // This is okay as per [docs](http://curl.haxx.se/libcurl/c/curl_easy_setopt.html)
- // Strings passed to libcurl as 'char *' arguments, are copied by the library; thus the string storage
- // associated to the pointer argument may be overwritten after curl_easy_setopt() returns.
- curl_slist_free_all(chunk);
-
return curl;
}
diff --git a/src/http_server.cpp b/src/http_server.cpp
index 505e9a7d..e9f71d8a 100644
--- a/src/http_server.cpp
+++ b/src/http_server.cpp
@@ -672,7 +672,7 @@ void HttpServer::stream_response(http_req& request, http_res& response) {
//LOG(INFO) << "stream_response called";
if(request._req == nullptr) {
// raft log replay or when underlying request is aborted
- //LOG(INFO) << "request._req == nullptr";
+ //LOG(INFO) << "stream_response, request._req == nullptr";
destroy_request_response(&request, &response);
return;
}
@@ -712,6 +712,8 @@ void HttpServer::destroy_request_response(http_req* request, http_res* response)
/*LOG(INFO) << "destroy_request_response, response->proxied_stream=" << response->proxied_stream
<< ", request->_req=" << request->_req << ", response->await=" << &response->await;*/
+ //LOG(INFO) << "destroy_request_response, response: " << response << ", response->auto_dispose: " << response->auto_dispose;
+
if(response->auto_dispose) {
//LOG(INFO) << "destroy_request_response: deleting req/res";
delete request;