Skip to content

Simplify ThreadPool into WorkerThread - #471

Merged
ospfranco merged 5 commits into
mainfrom
simplify-thread-pool
Oct 26, 2026
Merged

ospfranco merged 5 commits into
mainfrom
simplify-thread-pool

Conversation

@ospfranco

Copy link
Copy Markdown
Contributor

Builds on top of #470

Rather than keep on piling up on patches on the thread pool, simplifies the abstraction to a single worker thread impl. With some hard throws (thread joining itself).

Glenn444 and others added 5 commits October 16, 2026 23:52
close(), delete() and invalidate() drained the ThreadPool but left its
worker running; it was only joined in ~ThreadPool, when the last
shared_ptr (JS object, prepared statements, pending promise tasks) was
released. Apps that open/close repeatedly in a long-lived process kept
one idle thread per closed database until GC (an iOS background kill had
1,006 idle do_work threads).

- ThreadPool::shutdown(): sets done under the mutex, wakes and joins the
  worker; idempotent and safe to call concurrently. close/delete/
  invalidate call it after wait_finished(); ~ThreadPool calls it too.
- queue_work on a shut pool throws (callers queue from a Promise
  executor, so the promise rejects instead of never settling).
- A task that drops the last reference to its own pool no longer
  self-joins (std::system_error -> terminate): the worker detaches and
  do_work returns without touching the freed pool.

Fixes #469

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…join

Addresses the review of #470 (points 1-4 and 6-10).

shutdown() is now the single way to stop a pool. It refuses new work,
lets the worker run every task that is already queued (so no promise is
left unsettled and a later wait_finished() cannot block), then joins.
close(), delete() and invalidate() call just that instead of
wait_finished() followed by shutdown().

- Concurrent callers: the join runs under join_mutex, so a second
  shutdown() (or the destructor after it) returns only once the first
  caller's join has completed, never while the worker still uses the
  mutex, condition variable or queue.
- Called on the worker itself (e.g. a database invalidated from a task),
  shutdown() does not join: it runs the remaining queued tasks inline and
  returns. do_work() then releases `busy` as usual and exits, and a later
  shutdown() or the destructor joins it. The thread_local self-release
  flag and the detach branch are gone; the header documents the rule
  that makes them unnecessary: a task must never own its pool (none
  does: tasks capture the database or statement by raw pointer).
- `shut_down` is dropped; `done` is the only flag and is now read under
  the mutex only.
- queue_work() on a stopped pool throws ThreadPoolStoppedError
  ("[op-sqlite] thread pool is stopped"). promisify and
  flushPendingReactiveQueries map it to the same
  "[op-sqlite][<fn>] database is closed" error as throw_if_closed, thrown
  from the Promise executor so the promise rejects. promisify takes the
  function name for that.
- delete() checks the remote-only case before invalidating, interrupting
  or stopping the pool, so an unsupported delete() leaves the database
  open and usable.
- The constructor joins any thread it already started if a later one
  fails to start: the destructor does not run when a constructor throws.

scripts/thread_pool_check.cpp (run by scripts/test-thread-pool.sh, under
TSan and ASan) covers 300 closed-but-referenced pools, queue_work after
shutdown, draining queued tasks on shutdown() and in the destructor, two
concurrent shutdowns followed by the destructor, shutdown() from a task
on the worker, and a constructor whose thread cannot start.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Addresses point 5 of the review of #470 (and the test asked for in 10).

A statement keeps the raw connection handle it was prepared on. After
close() the async path rejected only because the pool was stopped, while
executeSync() and bindSync() still ran against the closed handle, which
for libsql is already freed.

PreparedStatementHostObject now holds a weak_ptr to its OPDatabase and
checks it at the top of bind, bindSync, execute and executeSync, before
anything touches the handle. A closed (or already destroyed) database
throws the same "[op-sqlite][<fn>] database is closed" error as
OPDatabase::throw_if_closed, with no `code`, whatever the pool's state.

The JS wrapper for stmt.execute is now async, as bind already is, so the
native throw reaches the caller as a rejection, the same way db.execute
reports a closed database. executeSync/bindSync throw synchronously.

example/src/tests/preparedStatements.ts covers both paths.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
The pool only ever runs one thread per connection, and after #470
nothing calls wait_finished() any more: shutdown() drains the queue and
joins. That makes most of the pool's machinery unnecessary.

- One std::thread, declared last, so a failed start needs no cleanup.
  This replaces the thread vector and the constructor try/catch.
- shutdown() runs under std::call_once: idempotent, and a concurrent
  caller blocks until the first caller's join has finished. This
  replaces join_mutex.
- shutdown() from the worker's own thread throws std::system_error
  before entering call_once, instead of running the rest of the queue
  inline. No task does this; the header documents the rule. This
  replaces worker_ids and the inline-drain path.
- busy and wait_finished() are removed.

Renamed ThreadPool -> WorkerThread (OPWorkerThread.*),
ThreadPoolStoppedError -> WorkerThreadStoppedError, the thread_pool
members -> worker_thread, and the harness/script to
scripts/worker_thread_check.cpp / scripts/test-worker-thread.sh.
Check (e) now expects shutdown() from the worker to throw while
another thread is already shutting down.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ospfranco
ospfranco added this pull request to stack #473 October 26, 2026 17:49
@ospfranco
ospfranco merged commit 03fd4b5 into main Oct 26, 2026
10 checks passed
@ospfranco
ospfranco deleted the simplify-thread-pool branch October 26, 2026 18:04
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

Successfully merging this pull request may close these issues.

2 participants