GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ThreadPool constructor might fail when pthread_create fails leaving threads unjoined · Issue #2444 · yhirose/cpp-httplib

ThreadPool constructor might fail when pthread_create fails leaving threads unjoined #2444

New issue
Copy link
New issue
Copy link
Closed
#2445
Closed
ThreadPool constructor might fail when pthread_create fails leaving threads unjoined#2444
#2445
Copy link

Description

@carlopi
carlopi
opened on May 7, 2026
Issue body actions

We are using httplib in duckdb, and we are super happy with it.

In an experimental branch, I try to use httplib's ThreadPool, but I found out that when (due to resource exhaustion in the system I was running the tests) pthread_create fails, the constructor for ThreadPool might leave threads in inconsistent state, and, in the context of my program, that lead to a std::terminate.

I am not completely sure this makes sense when outside duckdb / my specific branch, but I though of raising.

I am considering patching duckdb's httplib (would need to discuss with maintainers there) like:

@@ -904,9 +904,26 @@ class ThreadPool final : public TaskQueue {
 public:
   explicit ThreadPool(size_t n, size_t mqr = 0)
       : shutdown_(false), max_queued_requests_(mqr) {
-    while (n) {
-      threads_.emplace_back(worker(*this));
-      n--;
+    // Exception-safe construction: if std::thread creation fails partway
+    // (e.g. EAGAIN on pthread_create under thread-resource pressure), the
+    // partially-built threads_ vector would otherwise destruct with joinable
+    // std::thread objects, calling std::terminate(). Instead, signal shutdown
+    // to the workers we already spawned, join them, and rethrow.
+    try {
+      while (n) {
+        threads_.emplace_back(worker(*this));
+        n--;
+      }
+    } catch (...) {
+      {
+        duckdb::unique_lock<std::mutex> lock(mutex_);
+        shutdown_ = true;
+      }
+      cond_.notify_all();
+      for (auto &t : threads_) {
+        if (t.joinable()) { t.join(); }
+      }
+      throw;
     }
   }
 

see the diff at https://github.com/duckdb/duckdb/pull/22516/changes, now looking for guidance, it's this a proper httplib issue? Should I file a similar PR to httplib instead, would that make sense?

Thanks for the great project, and for any guidance you might offer.

Reacted by Matheus Gabriel Werny

Activity

  1. yhirose commented on May 8, 2026

    @yhirose
    yhirose
    on May 8, 2026
    Owner
    More actions

    @carlopi thanks for the report! I made a pull request which applies your change to the latest httplib.h.
    #2445

  2. yhirose
    closed this as completedin #2445on May 10, 2026
  3. SiarheiHerasiuta
    added a commit that references this issue on May 11, 2026

    Make ThreadPool ctor exception-safe on partial thread creation (yhiro…

    This issue will close once commit 7d5082c is merged into the 'master' branch.
    7d5082c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402792 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.