Skip to content

Commit

Permalink
Fix #2068
Browse files Browse the repository at this point in the history
  • Loading branch information
yhirose committed Feb 21, 2025
1 parent 22d90c2 commit 2b95d5e
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -1534,7 +1534,10 @@ class ClientImpl {
std::recursive_mutex request_mutex_;

// These are all protected under socket_mutex
public:
size_t socket_requests_in_flight_ = 0;

protected:
std::thread::id socket_requests_are_from_thread_ = std::thread::id();
bool socket_should_be_closed_when_request_is_done_ = false;

Expand Down Expand Up @@ -7333,6 +7336,16 @@ inline ClientImpl::ClientImpl(const std::string &host, int port,
client_cert_path_(client_cert_path), client_key_path_(client_key_path) {}

inline ClientImpl::~ClientImpl() {
// Wait until all the requests in flight are handled.
size_t retry_count = 10;
while (retry_count-- > 0) {
{
std::lock_guard<std::mutex> guard(socket_mutex_);
if (socket_requests_in_flight_ == 0) { break; }
}
std::this_thread::sleep_for(std::chrono::microseconds{1});
}

std::lock_guard<std::mutex> guard(socket_mutex_);
shutdown_socket(socket_);
close_socket(socket_);
Expand Down

0 comments on commit 2b95d5e

Please sign in to comment.