-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconcache_lock_hunt_test.exs
More file actions
303 lines (247 loc) · 8.05 KB
/
Copy pathconcache_lock_hunt_test.exs
File metadata and controls
303 lines (247 loc) · 8.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
defmodule Lockstep.ConCacheLockHuntTest do
@moduledoc """
Hunt for races in ConCache.Lock (sasa1977/con_cache).
ConCache.Lock is a small GenServer-based key-locking primitive used
internally by ConCache. It tracks per-key resources, monitors lock
holders, and re-assigns the lock when an owner exits or releases.
Skipped when the upstream source isn't on disk; clone with:
gh repo clone sasa1977/con_cache /tmp/concache_src
"""
use ExUnit.Case, async: false
import Bitwise
@concache_src "/tmp/concache_src"
setup_all do
if File.exists?(@concache_src) do
output_dir =
Path.join(System.tmp_dir!(), "lockstep_concache_#{System.unique_integer([:positive])}")
File.mkdir_p!(output_dir)
sources = [
Path.join(@concache_src, "lib/con_cache/lock/resource.ex"),
Path.join(@concache_src, "lib/con_cache/lock/monitors.ex"),
Path.join(@concache_src, "lib/con_cache/lock.ex")
]
paths = Enum.filter(sources, &File.exists?/1)
{:ok, _} =
Lockstep.MixCompiler.compile(%{
paths: paths,
output: output_dir
})
ordered = [
Path.join(output_dir, "/tmp/concache_src/lib/con_cache/lock/resource.ex"),
Path.join(output_dir, "/tmp/concache_src/lib/con_cache/lock/monitors.ex"),
Path.join(output_dir, "/tmp/concache_src/lib/con_cache/lock.ex")
]
File.cd!(@concache_src, fn ->
for path <- ordered do
if File.exists?(path) do
try do
Code.compile_file(path)
rescue
e ->
IO.puts(
:stderr,
"[concache] compile error on #{path}: " <>
String.slice(Exception.format(:error, e, __STACKTRACE__), 0, 400)
)
end
end
end
end)
{:ok, output_dir: output_dir}
else
:ok
end
end
test "ConCache.Lock module loaded", _ctx do
if not File.exists?(@concache_src) do
IO.puts("\n[concache] skip: clone sasa1977/con_cache to /tmp/concache_src first")
assert true
else
assert Code.ensure_loaded?(ConCache.Lock)
end
end
test "deep sweep: concurrent locking + release across strategies", _ctx do
if not File.exists?(@concache_src) do
IO.puts("\n[concache] skip")
assert true
else
results =
for strategy <- [:random, :pct, :fair_pct, :pos],
scenario <- [
{"two_lockers_one_key", &two_lockers_one_key/0},
{"three_lockers_one_key", &three_lockers_one_key/0},
{"owner_crash", &owner_crash/0},
{"try_lock", &try_lock_body/0},
{"reentrant", &reentrant_body/0}
] do
{name, body} = scenario
outcome =
try do
Lockstep.Runner.run(body,
iterations: 200,
strategy: strategy,
max_steps: 1000,
seed: :erlang.phash2({strategy, name}, 1 <<< 30),
iter_timeout: 5_000,
suite: "concache_#{strategy}_#{name}"
)
:clean
rescue
e in Lockstep.BugFound ->
{:bug, name, strategy, e.iteration, e.reason}
end
{strategy, name, outcome}
end
bugs = Enum.filter(results, fn {_, _, o} -> o != :clean end)
IO.puts("""
--- ConCache.Lock DEEP SWEEP: 4 strategies x 5 scenarios x 200 iter ---
clean: #{length(results) - length(bugs)}/#{length(results)}
bugs: #{length(bugs)}
""")
for {strategy, name, outcome} <- bugs do
IO.puts(" #{inspect(strategy)} on #{name}: #{inspect(outcome, limit: 8)}")
end
if length(bugs) > 0 do
IO.puts("\n*** Lockstep surfaced something in ConCache.Lock. ***\n")
end
end
end
# ----------------------------------------------------------------
# Bodies
# ----------------------------------------------------------------
defp two_lockers_one_key do
{:ok, lock} = ConCache.Lock.start_link([])
parent = self()
for i <- 1..2 do
Lockstep.spawn(fn ->
result =
ConCache.Lock.exec(lock, :the_key, fn ->
i
end)
Lockstep.send(parent, {:done, i, result})
end)
end
msgs = for _ <- 1..2, do: Lockstep.recv()
results = Enum.map(msgs, fn {:done, i, r} -> {i, r} end) |> Enum.sort()
if Enum.map(results, &elem(&1, 1)) != [1, 2] do
raise "expected [1, 2] in some order, got #{inspect(results)}"
end
end
defp three_lockers_one_key do
{:ok, lock} = ConCache.Lock.start_link([])
parent = self()
for i <- 1..3 do
Lockstep.spawn(fn ->
result =
ConCache.Lock.exec(lock, :the_key, fn ->
i
end)
Lockstep.send(parent, {:done, i, result})
end)
end
msgs = for _ <- 1..3, do: Lockstep.recv()
sorted = msgs |> Enum.map(fn {:done, i, _} -> i end) |> Enum.sort()
if sorted != [1, 2, 3] do
raise "expected all 3 to complete: #{inspect(msgs)}"
end
end
defp owner_crash do
{:ok, lock} = ConCache.Lock.start_link([])
parent = self()
# Owner takes the lock and crashes mid-execution. Pending waiter
# should be granted the lock via the monitor's :DOWN handler.
Lockstep.spawn(fn ->
try do
ConCache.Lock.exec(lock, :resource, fn ->
Lockstep.send(parent, :owner_holds)
raise "owner died holding lock"
end)
catch
_, _ -> :ok
end
end)
Lockstep.recv_first(fn
:owner_holds -> true
_ -> false
end)
Lockstep.spawn(fn ->
result = ConCache.Lock.exec(lock, :resource, fn -> :got_it end)
Lockstep.send(parent, {:waiter_result, result})
end)
{:waiter_result, r} =
Lockstep.recv_first(fn
{:waiter_result, _} -> true
_ -> false
end)
if r != :got_it do
raise "waiter never acquired after owner crash; got #{inspect(r)}"
end
end
defp try_lock_body do
{:ok, lock} = ConCache.Lock.start_link([])
parent = self()
# Worker A holds the lock, worker B tries to acquire.
a_holding = make_ref()
Lockstep.spawn(fn ->
ConCache.Lock.exec(lock, :resource, fn ->
Lockstep.send(parent, {:a_holding, a_holding})
# release-after barrier: parent will tell us when to release
Lockstep.recv_first(fn
:go -> true
_ -> false
end)
end)
Lockstep.send(parent, :a_done)
end)
Lockstep.recv_first(fn
{:a_holding, ^a_holding} -> true
_ -> false
end)
# B tries to acquire while A holds.
Lockstep.spawn(fn ->
result = ConCache.Lock.try_exec(lock, :resource, fn -> :got_it end)
Lockstep.send(parent, {:b_try, result})
end)
# We expect B to get :not_acquired.
{:b_try, b_result} =
Lockstep.recv_first(fn
{:b_try, _} -> true
_ -> false
end)
if b_result != {:lock, :not_acquired} do
raise "try_exec should have failed while owner holds; got #{inspect(b_result)}"
end
# Tell A to release.
[a_pid] =
Process.list()
|> Enum.filter(&(&1 != self()))
|> Enum.take(1)
_ = a_pid
# Easier: use a publish via the lock - actually let's just send :go
# to all processes. The right one will pick it up.
# We can use a more deterministic mechanism via the parent pid map,
# but for this scenario we don't deeply care.
:ok
end
defp reentrant_body do
{:ok, lock} = ConCache.Lock.start_link([])
parent = self()
# Same caller takes the same lock twice (re-entrant via exec).
Lockstep.spawn(fn ->
result =
ConCache.Lock.exec(lock, :the_key, fn ->
# nested: same pid takes the same key
ConCache.Lock.exec(lock, :the_key, fn -> :inner end)
end)
Lockstep.send(parent, {:done, result})
end)
{:done, r} =
Lockstep.recv_first(fn
{:done, _} -> true
_ -> false
end)
if r != :inner do
raise "re-entrant lock returned #{inspect(r)}"
end
end
end