Skip to content

{180274222}: Don't let a temp btree become the thread's table btree - #6413

Merged
chands10 merged 1 commit into
bloomberg:mainfrom
chands10:chunk_stale_btree
Oct 27, 2026
Merged

chands10 merged 1 commit into
bloomberg:mainfrom
chands10:chunk_stale_btree

Conversation

@chands10

Copy link
Copy Markdown
Contributor

sqlite3BtreeCursor() set thd->bt to whatever btree the thread opened its first cursor on, temporary ones included. When that temp btree was closed, sqlite3BtreeClose() only cleared thd->bttmp (if/else-if), so thd->bt was left pointing at freed memory for the life of the SQL thread.

Anything walking thd->bt->cursors then missed the real table cursors. In a chunked transaction, chunk_transaction() failed to clear cur->shadtbl before the chunk commit freed the shadow tables, and the next insert dereferenced the freed shadow table in osql_save_insrec(). It reproduces with a query whose first cursor is on a temp table (e.g. "select 1 union select 2") on a fresh SQL thread, followed by a chunked INSERT ... SELECT spanning more than one chunk.

Only set thd->bt for non-temporary btrees, and clear thd->bt and thd->bttmp independently on close.

sqlite3BtreeCursor() set thd->bt to whatever btree the thread opened its
first cursor on, temporary ones included. When that temp btree was
closed, sqlite3BtreeClose() only cleared thd->bttmp (if/else-if), so
thd->bt was left pointing at freed memory for the life of the SQL thread.

Anything walking thd->bt->cursors then missed the real table cursors.
In a chunked transaction, chunk_transaction() failed to clear
cur->shadtbl before the chunk commit freed the shadow tables, and the
next insert dereferenced the freed shadow table in osql_save_insrec().
It reproduces with a query whose first cursor is on a temp table (e.g.
"select 1 union select 2") on a fresh SQL thread, followed by a chunked
INSERT ... SELECT spanning more than one chunk.

Only set thd->bt for non-temporary btrees, and clear thd->bt and
thd->bttmp independently on close.

Signed-off-by: Salil Chandra <schandra107@bloomberg.net>

@roborivers roborivers left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cbuild submission: Success ✓.
Regression testing: Success ✓.

@markhannum markhannum self-assigned this Oct 27, 2026
@chands10
chands10 merged commit c026cb7 into bloomberg:main Oct 27, 2026
6 checks passed
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.

3 participants