Commit 6409da4
authored
fix(locking): Replace object_id-based locks with thread-local storage (#226)
The previous implementation stored locks in a class variable indexed
by Thread.current.object_id, which had three critical defects:
1. Object ID reuse: Ruby can reuse object IDs after garbage collection,
allowing new threads to inadvertently access stale locks from dead
threads
2. Race conditions: The shared @@locks class variable was not
thread-safe, creating potential race conditions
3. Memory leaks: Dead thread entries were not automatically cleaned up
This fix replaces the class variable with Ruby's built-in thread-local
storage (Thread.current[:double_entry_locks]), which:
- Ties data directly to the thread object, not its object ID
- Provides isolated storage per thread
- Automatically cleans up when threads terminate1 parent f4bb6cf commit 6409da4
2 files changed
Lines changed: 10 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
10 | 16 | | |
11 | 17 | | |
12 | 18 | | |
13 | 19 | | |
| 20 | + | |
14 | 21 | | |
15 | 22 | | |
16 | 23 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
60 | | - | |
61 | | - | |
62 | 60 | | |
63 | 61 | | |
64 | 62 | | |
| |||
97 | 95 | | |
98 | 96 | | |
99 | 97 | | |
100 | | - | |
| 98 | + | |
101 | 99 | | |
102 | 100 | | |
103 | 101 | | |
104 | | - | |
| 102 | + | |
105 | 103 | | |
106 | 104 | | |
107 | 105 | | |
108 | | - | |
| 106 | + | |
109 | 107 | | |
110 | 108 | | |
111 | 109 | | |
| |||
0 commit comments