optimistic lock gives wrong result with isolation level read-committed
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Bug Report
Please answer these questions before submitting your issue. Thanks!
### 1. Minimal reproduce step (Required)
```python
import mysql.connector
import threading
import time
DB_CONFIG = {
'user': 'root',
'password': '',
'host': '127.0.0.1',
'port': 4000, # adjust if needed
'database': 'test',
'autocommit': False
}
def setup_table():
conn = mysql.connector.connect(**DB_CONFIG)
cursor = conn.cursor()
cursor.execute("DROP TABLE IF EXISTS kv")
cursor.execute("""
CREATE TABLE kv (
id INT PRIMARY KEY,
v1 INT,
v2 INT,
a VARCHAR(10)
)
""")
cursor.execute("INSERT INTO kv VALUES (1, 1, 1, NULL)")
conn.commit()
cursor.close()
conn.close()
print("[Setup] Table 'kv' created with 1 row.")
def session1(txn_mode):
conn = mysql.connector.connect(**DB_CONFIG)
cursor = conn.cursor()
cursor.execute(f"SET SESSION tidb_txn_mode = '{txn_mode}'")
cursor.execute("SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED")
cursor.execute("BEGIN")
print(f"[Session1-{txn_mode}] Begin transaction with READ COMMITTED")
cursor.execute("SELECT * FROM kv")
rows = cursor.fetchall()
print(f"[Session1-{txn_mode}] Initial read:", rows)
print(f"[Session1-{txn_mode}] Sleeping to allow session2 to delete row...")
time.sleep(5)
cursor.execute("SELECT * FROM kv")
rows = cursor.fetchall()
print(f"[Session1-{txn_mode}] Read after session2 commit:", rows)
cursor.execute("COMMIT")
cursor.close()
conn.close()
def session2(txn_mode):
time.sleep(2) # Ensure session1 starts first
conn = mysql.connector.connect(**DB_CONFIG)
cursor = conn.cursor()
cursor.execute(f"SET SESSION tidb_txn_mode = '{txn_mode}'")
cursor.execute("SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED")
cursor.execute("BEGIN")
print(f"[Session2-{txn_mode}] Deleting rows in kv")
cursor.execute("DELETE FROM kv")
cursor.execute("COMMIT")
print(f"[Session2-{txn_mode}] Delete committed")
cursor.close()
conn.close()
def run_test(txn_mode):
print(f"\n===== Running test with {txn_mode.upper()} lock mode =====")
setup_table()
t1 = threading.Thread(target=session1, args=(txn_mode,))
t2 = threading.Thread(target=session2, args=(txn_mode,))
t1.start()
t2.start()
t1.join()
t2.join()
print(f"[Main] Test with {txn_mode.upper()} complete.")
def main():
run_test('optimistic')
run_test('pessimistic')
if __name__ == "__main__":
main()
```
### 2. What did you expect to see? (Required)
isolation level is `READ COMMITTED`, in session 1, the 2nd select should return no rows regardless tidb_txn_mode
### 3. What did you see instead (Required)
```bash
===== Running test with OPTIMISTIC lock mode =====
[Setup] Table 'kv' created with 1 row.
[Session1-optimistic] Begin transaction with READ COMMITTED
[Session1-optimistic] Initial read: [(1, 1, 1, None)]
[Session1-optimistic] Sleeping to allow session2 to delete row...
[Session2-optimistic] Deleting rows in kv
[Session2-optimistic] Delete committed
[Session1-optimistic] Read after session2 commit: [(1, 1, 1, None)] <=== wrong result
[Main] Test with OPTIMISTIC complete.
===== Running test with PESSIMISTIC lock mode =====
[Setup] Table 'kv' created with 1 row.
[Session1-pessimistic] Begin transaction with READ COMMITTED
[Session1-pessimistic] Initial read: [(1, 1, 1, None)]
[Session1-pessimistic] Sleeping to allow session2 to delete row...
[Session2-pessimistic] Deleting rows in kv
[Session2-pessimistic] Delete committed
[Session1-pessimistic] Read after session2 commit: []
[Main] Test with PESSIMISTIC complete.
```
### 4. What is your TiDB version? (Required)
```sql
mysql> select tidb_version()\G
*************************** 1. row ***************************
tidb_version(): Release Version: v9.0.0-beta.2.pre-141-g6bc897846c
Edition: Community
Git Commit Hash: 6bc897846c9a0aa4bdbcbc37657f739743cd8b98
Git Branch: HEAD
UTC Build Time: 2025-07-19 11:00:34
GoVersion: go1.23.11
Race Enabled: false
Check Table Before Drop: false
Store: tikv
Kernel Type: Classic
```
Contributor guide
Assessment
This issue has not been assessed yet.