fix: drain thread pool before closing db handle to prevent use-after-free#385
Open
isaacrowntree wants to merge 1 commit intoOP-Engineering:mainfrom
Open
fix: drain thread pool before closing db handle to prevent use-after-free#385isaacrowntree wants to merge 1 commit intoOP-Engineering:mainfrom
isaacrowntree wants to merge 1 commit intoOP-Engineering:mainfrom
Conversation
…free The `close()` JS function and `invalidate()` destructor path both call `opsqlite_close(db)` without first waiting for in-flight async queries on the thread pool to complete. If an `execute()` is queued or running when the db handle is freed, the worker thread dereferences a dangling `sqlite3*` pointer, causing heap corruption (SIGABRT in `sqlite3VdbeMemSetStr` / `_szone_free`). This is reproducible on iOS during React Native Fast Refresh: the old `ReactInstance` is destroyed (freeing `DBHostObject` via the destructor) while the thread pool still has pending queries from the previous JS context. Changes: - `close()`: call `thread_pool->waitFinished()` before `opsqlite_close()` to drain any queued/running async queries. Also set `db = nullptr` after close for safety. - `invalidate()`: replace `restartPool()` with `waitFinished()`. `restartPool()` joins threads then needlessly recreates the pool. `waitFinished()` blocks until the queue is empty and no worker is busy, then the `ThreadPool` destructor (via `shared_ptr` release) handles thread cleanup. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
3 tasks
Contributor
|
nice, thanks for the PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
close()andinvalidate()(destructor) both callopsqlite_close(db)without waiting for in-flight async queries on the thread pool to finish. If anexecute()is queued or running when the db handle is freed, the worker thread dereferences a danglingsqlite3*pointer → heap corruption → SIGABRT.Crash stack (iOS)
Reproduction
This is reliably reproducible on iOS during React Native Fast Refresh. The old
ReactInstanceis destroyed on one thread (freeingDBHostObjectand itssqlite3*handle via the destructor) while the op-sqlite thread pool worker on another thread is still executing a queuedopsqlite_execute()against the now-freed handle.The symptom at the JS level is intermittent "out of memory" errors from SQLite queries after Fast Refresh, requiring a hard app restart to recover.
Fix
close()(JS function, line 237)Added
thread_pool->waitFinished()beforeopsqlite_close(db)to drain any queued/running async queries. Also setdb = nullptrafter close for safety.invalidate()(destructor path, line 668)Replaced
restartPool()withwaitFinished().restartPool()joins all threads (waiting for in-flight work) but then needlessly recreates the pool — wasteful during teardown.waitFinished()blocks until the queue is empty and no worker is busy, then theThreadPooldestructor (viashared_ptrrelease) handles thread cleanup.Changes
// close() JS function function_map["close"] = HFN(this) { invalidated = true; + thread_pool->waitFinished(); opsqlite_close(db); + db = nullptr; return {}; }); // invalidate() called by destructor void DBHostObject::invalidate() { if (invalidated) return; invalidated = true; - thread_pool->restartPool(); + thread_pool->waitFinished(); if (db != nullptr) { opsqlite_close(db); db = nullptr; } }Why
waitFinished()is safe herewaitFinished()(already exists inOPThreadPool.h) blocks on the condition variable untilworkQueue.empty() && busy == 0. Sinceinvalidatedis set totruebefore the wait, no new JS-initiated queries can be queued (the JSI functions checkinvalidatedbefore callingpromisify). So the wait is bounded by the current in-flight query's execution time.