Commit 350b01d
committed
Fix SQLiteSession threading.Lock() bug and file descriptor leak
This PR addresses two critical bugs in SQLiteSession:
## Bug 1: threading.Lock() creating new instances
**Problem:**
In SQLiteSession (4 places) and AdvancedSQLiteSession (8 places), the code used:
```python
with self._lock if self._is_memory_db else threading.Lock():
```
For file-based databases, this creates a NEW Lock() instance on every operation,
providing NO thread safety whatsoever. Only in-memory databases used self._lock.
**Impact:**
- File-based SQLiteSession had zero thread protection
- Race conditions possible but masked by WAL mode's own concurrency handling
## Bug 2: File descriptor leak
**Problem:**
Thread-local connections in ThreadPoolExecutor are never cleaned up:
- asyncio.to_thread() uses ThreadPoolExecutor internally
- Each worker thread creates a connection on first use
- ThreadPoolExecutor reuses threads indefinitely
- Connections persist until program exit, accumulating file descriptors
**Evidence:**
Testing on main branch (60s, 40 concurrent workers):
- My system (FD limit 1,048,575): +789 FDs leaked, 0 errors (limit not reached)
- @ihower's system (likely limit 1,024): 646,802 errors in 20 seconds
Error: `sqlite3.OperationalError: unable to open database file`
## Solution: Unified shared connection approach
Instead of managing thread-local connections that can't be reliably cleaned up
in ThreadPoolExecutor, use a single shared connection for all database types.
**Changes:**
1. Removed thread-local connection logic (eliminates FD leak root cause)
2. All database types now use shared connection + self._lock
3. SQLite's WAL mode provides sufficient concurrency even with single connection
4. Fixed all 12 instances of threading.Lock() bug (4 in SQLiteSession, 8 in AdvancedSQLiteSession)
5. Kept _is_memory_db attribute for backward compatibility with AdvancedSQLiteSession
6. Added close() and __del__() methods for proper cleanup
**Results (60s stress test, 30 writers + 10 readers):**
```
Main branch:
- FD growth: +789 (leak)
- Throughput: 701 ops/s
- Errors: 0 on high-limit systems, 646k+ on normal systems
After fix:
- FD growth: +44 (stable)
- Throughput: 726 ops/s (+3.6% improvement)
- Errors: 0 on all systems
- All 29 SQLite tests pass
```
## Why shared connection performs better
SQLite's WAL (Write-Ahead Logging) mode already provides:
- Multiple concurrent readers
- One writer coexisting with readers
- Readers don't block writer
- Writer doesn't block readers (except during checkpoint)
The overhead of managing multiple connections outweighs any concurrency benefit.
## Backward compatibility
The _is_memory_db attribute is preserved for AdvancedSQLiteSession compatibility,
even though the implementation no longer differentiates connection strategies.
## Testing
Comprehensive stress test available at:
https://gist.github.com/gn00295120/0b6a65fe6c0ac6b7a1ce23654eed3ffe
Run with: `python sqlite_stress_test_final.py`1 parent 4bc33e3 commit 350b01d
File tree
2 files changed
+30
-47
lines changed- src/agents
- extensions/memory
- memory
2 files changed
+30
-47
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
| |||
146 | 145 | | |
147 | 146 | | |
148 | 147 | | |
149 | | - | |
| 148 | + | |
150 | 149 | | |
151 | 150 | | |
152 | 151 | | |
| |||
191 | 190 | | |
192 | 191 | | |
193 | 192 | | |
194 | | - | |
| 193 | + | |
195 | 194 | | |
196 | 195 | | |
197 | 196 | | |
| |||
333 | 332 | | |
334 | 333 | | |
335 | 334 | | |
336 | | - | |
| 335 | + | |
337 | 336 | | |
338 | 337 | | |
339 | 338 | | |
| |||
439 | 438 | | |
440 | 439 | | |
441 | 440 | | |
442 | | - | |
| 441 | + | |
443 | 442 | | |
444 | 443 | | |
445 | 444 | | |
| |||
694 | 693 | | |
695 | 694 | | |
696 | 695 | | |
697 | | - | |
| 696 | + | |
698 | 697 | | |
699 | 698 | | |
700 | 699 | | |
| |||
801 | 800 | | |
802 | 801 | | |
803 | 802 | | |
804 | | - | |
| 803 | + | |
805 | 804 | | |
806 | 805 | | |
807 | 806 | | |
| |||
1072 | 1071 | | |
1073 | 1072 | | |
1074 | 1073 | | |
1075 | | - | |
| 1074 | + | |
1076 | 1075 | | |
1077 | 1076 | | |
1078 | 1077 | | |
| |||
1236 | 1235 | | |
1237 | 1236 | | |
1238 | 1237 | | |
1239 | | - | |
| 1238 | + | |
1240 | 1239 | | |
1241 | 1240 | | |
1242 | 1241 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | | - | |
42 | 41 | | |
43 | 42 | | |
44 | | - | |
45 | | - | |
| 43 | + | |
46 | 44 | | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
57 | 52 | | |
58 | 53 | | |
59 | 54 | | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
| 55 | + | |
75 | 56 | | |
76 | 57 | | |
77 | 58 | | |
| |||
120 | 101 | | |
121 | 102 | | |
122 | 103 | | |
123 | | - | |
| 104 | + | |
124 | 105 | | |
125 | 106 | | |
126 | 107 | | |
| |||
174 | 155 | | |
175 | 156 | | |
176 | 157 | | |
177 | | - | |
| 158 | + | |
178 | 159 | | |
179 | 160 | | |
180 | 161 | | |
| |||
215 | 196 | | |
216 | 197 | | |
217 | 198 | | |
218 | | - | |
| 199 | + | |
219 | 200 | | |
220 | 201 | | |
221 | 202 | | |
| |||
252 | 233 | | |
253 | 234 | | |
254 | 235 | | |
255 | | - | |
| 236 | + | |
256 | 237 | | |
257 | 238 | | |
258 | 239 | | |
| |||
267 | 248 | | |
268 | 249 | | |
269 | 250 | | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | | - | |
274 | | - | |
275 | | - | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
0 commit comments