Skip to content

Commit

Permalink
Merge pull request #792 from ahrtr/freelist_rollback_20240709
Browse files Browse the repository at this point in the history
Panicking when a write txn tries to free a page which was allocated by itself
  • Loading branch information
ahrtr authored Jul 30, 2024
2 parents 49c7697 + da1c83c commit df83d9d
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions internal/freelist/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func (t *shared) Free(txid common.Txid, p *common.Page) {
t.pending[txid] = txp
}
allocTxid, ok := t.allocs[p.Id()]
common.Verify(func() {
if allocTxid == txid {
panic(fmt.Sprintf("free: freed page (%d) was allocated by the same transaction (%d)", p.Id(), txid))
}
})
if ok {
delete(t.allocs, p.Id())
}
Expand All @@ -87,7 +92,6 @@ func (t *shared) Rollback(txid common.Txid) {
if txp == nil {
return
}
var m common.Pgids
for i, pgid := range txp.ids {
delete(t.cache, pgid)
tx := txp.alloctx[i]
Expand All @@ -98,13 +102,12 @@ func (t *shared) Rollback(txid common.Txid) {
// Pending free aborted; restore page back to alloc list.
t.allocs[pgid] = tx
} else {
// Freed page was allocated by this txn; OK to throw away.
m = append(m, pgid)
// A writing TXN should never free a page which was allocated by itself.
panic(fmt.Sprintf("rollback: freed page (%d) was allocated by the same transaction (%d)", pgid, txid))
}
}
// Remove pages from pending list and mark as free if allocated by txid.
delete(t.pending, txid)
t.mergeSpans(m)
}

func (t *shared) AddReadonlyTXID(tid common.Txid) {
Expand Down

0 comments on commit df83d9d

Please sign in to comment.