After WT-18533 (wiredtiger#15136), _sweep_close_dhandle_locked in src/conn/conn_sweep.c skips the whole btree->modified block for outdated disaggregated trees. That block held the only session_inuse != 0 check made under the dhandle write lock. Sweep calls wt_conn_dhandle_close(session, false, true, false), so the max_upd_txn visibility check does not run either. The only remaining in-use check for outdated handles is the unlocked relaxed load of session_inuse in _sweep_expire, made before the write-lock trylock (TOCTOU).
Race
- Sweep passes the unlocked session_inuse check in __sweep_expire (value is 0), before taking the write lock.
- A session that found the handle before it was marked outdated, or that holds an open cursor on it, takes the read lock. WT_DHANDLE_CAN_REOPEN is false for outdated handles, so it uses the slow path in _wt_session_lock_dhandle, which checks only WT_DHANDLE_OPEN and WT_DHANDLE_DEAD. _wt_session_get_dhandle does not recheck outdated.
- The session writes. __txn_next_op bumps session_inuse, and the tree can be dirtied because outdated trees stay writable. It then closes its cursor and releases the read lock while the transaction is still open.
- Sweep takes the write lock, skips the in-use check, and calls __wt_conn_dhandle_close. That frees the pages and btree still referenced by the open transaction's WT_TXN_OP.
- The later commit or rollback dereferences freed memory.
Impact
Use-after-free in the shared process: most likely a server crash, possibly memory corruption. The window is narrow and needs a step-down racing with sweep.
Proposed fix
Keep the dirty-outdated exception, but under the write lock still return without closing if session_inuse != 0, using the same acquire load as the existing check. This relies on the existing rwlock.
Testing
Add a Python test that holds an open transaction with a write on the stable table across step-down and sweep, and checks that sweep does not close the handle until the transaction resolves. test_layered_stepdown_dirty_outdated.py only covers scan-dirties-then-discard.
Found while reviewing the MongoDB import 10gen/mongo#67445.
- is related to
-
WT-18533 Read-only cursor scan can dirty a live stable btree across step-down - "disaggregated btree dirtied while not leader" assertion
-
- Closed
-