Commit 843f2be
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 843f2be
File tree
2 files changed
+97
-110
lines changed- src/agents
- extensions/memory
- memory
2 files changed
+97
-110
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 | | |
| |||
261 | 260 | | |
262 | 261 | | |
263 | 262 | | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | | - | |
274 | | - | |
275 | | - | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
276 | 276 | | |
277 | 277 | | |
278 | 278 | | |
| |||
284 | 284 | | |
285 | 285 | | |
286 | 286 | | |
287 | | - | |
288 | | - | |
289 | | - | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | | - | |
297 | | - | |
298 | | - | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
299 | 300 | | |
300 | 301 | | |
301 | 302 | | |
| |||
304 | 305 | | |
305 | 306 | | |
306 | 307 | | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | | - | |
311 | | - | |
312 | | - | |
313 | | - | |
314 | | - | |
315 | | - | |
316 | | - | |
317 | | - | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
318 | 320 | | |
319 | 321 | | |
320 | 322 | | |
| |||
333 | 335 | | |
334 | 336 | | |
335 | 337 | | |
336 | | - | |
| 338 | + | |
337 | 339 | | |
338 | 340 | | |
339 | 341 | | |
| |||
439 | 441 | | |
440 | 442 | | |
441 | 443 | | |
442 | | - | |
| 444 | + | |
443 | 445 | | |
444 | 446 | | |
445 | 447 | | |
| |||
694 | 696 | | |
695 | 697 | | |
696 | 698 | | |
697 | | - | |
| 699 | + | |
698 | 700 | | |
699 | 701 | | |
700 | 702 | | |
| |||
756 | 758 | | |
757 | 759 | | |
758 | 760 | | |
759 | | - | |
760 | | - | |
761 | | - | |
762 | | - | |
763 | | - | |
764 | | - | |
765 | | - | |
766 | | - | |
767 | | - | |
768 | | - | |
769 | | - | |
770 | | - | |
771 | | - | |
772 | | - | |
773 | | - | |
774 | | - | |
775 | | - | |
776 | | - | |
777 | | - | |
778 | | - | |
779 | | - | |
780 | | - | |
781 | | - | |
782 | | - | |
783 | | - | |
784 | | - | |
785 | | - | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
786 | 776 | | |
787 | 777 | | |
788 | | - | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
789 | 792 | | |
790 | 793 | | |
791 | 794 | | |
| |||
801 | 804 | | |
802 | 805 | | |
803 | 806 | | |
804 | | - | |
| 807 | + | |
805 | 808 | | |
806 | 809 | | |
807 | 810 | | |
| |||
1072 | 1075 | | |
1073 | 1076 | | |
1074 | 1077 | | |
1075 | | - | |
| 1078 | + | |
1076 | 1079 | | |
1077 | 1080 | | |
1078 | 1081 | | |
| |||
1236 | 1239 | | |
1237 | 1240 | | |
1238 | 1241 | | |
1239 | | - | |
| 1242 | + | |
1240 | 1243 | | |
1241 | 1244 | | |
1242 | 1245 | | |
| |||
| 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