frontrun 0.4.0__tar.gz → 0.5.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {frontrun-0.4.0 → frontrun-0.5.0}/Cargo.lock +2 -2
- {frontrun-0.4.0 → frontrun-0.5.0}/Cargo.toml +1 -1
- {frontrun-0.4.0 → frontrun-0.5.0}/PKG-INFO +121 -24
- {frontrun-0.4.0 → frontrun-0.5.0}/README.md +120 -23
- {frontrun-0.4.0 → frontrun-0.5.0}/crates/dpor/src/engine.rs +26 -74
- {frontrun-0.4.0 → frontrun-0.5.0}/crates/dpor/src/lib.rs +12 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/crates/dpor/src/path.rs +211 -8
- {frontrun-0.4.0 → frontrun-0.5.0}/crates/dpor/src/thread.rs +0 -3
- frontrun-0.5.0/frontrun/__init__.py +152 -0
- frontrun-0.5.0/frontrun/_async_autopause.py +129 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/_cooperative.py +289 -160
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/_deadlock.py +3 -1
- frontrun-0.5.0/frontrun/_dpor_core/__init__.py +47 -0
- frontrun-0.5.0/frontrun/_dpor_core/concurrency.py +167 -0
- frontrun-0.5.0/frontrun/_dpor_core/engine.py +31 -0
- frontrun-0.5.0/frontrun/_dpor_core/failures.py +42 -0
- frontrun-0.5.0/frontrun/_dpor_core/invariants.py +64 -0
- frontrun-0.5.0/frontrun/_dpor_core/row_locks.py +116 -0
- frontrun-0.5.0/frontrun/_dpor_core/utils.py +129 -0
- frontrun-0.5.0/frontrun/_dpor_runtime/__init__.py +19 -0
- frontrun-0.5.0/frontrun/_dpor_runtime/_shared.py +103 -0
- frontrun-0.5.0/frontrun/_dpor_runtime/explore.py +541 -0
- frontrun-0.5.0/frontrun/_dpor_runtime/preload_bridge.py +119 -0
- frontrun-0.5.0/frontrun/_dpor_runtime/replay.py +143 -0
- frontrun-0.5.0/frontrun/_dpor_runtime/runner.py +468 -0
- frontrun-0.5.0/frontrun/_dpor_runtime/scheduler.py +1071 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/_io_detection.py +104 -2
- frontrun-0.5.0/frontrun/_marker_coordination.py +239 -0
- frontrun-0.5.0/frontrun/_opcode_observer.py +1953 -0
- frontrun-0.5.0/frontrun/_patching.py +52 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/_preload_io.py +39 -2
- frontrun-0.5.0/frontrun/_random_schedules.py +109 -0
- frontrun-0.5.0/frontrun/_real_threading.py +84 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/_redis_client.py +133 -138
- frontrun-0.5.0/frontrun/_redis_client_async.py +183 -0
- frontrun-0.5.0/frontrun/_redis_command_data.py +323 -0
- frontrun-0.5.0/frontrun/_redis_parsing.py +315 -0
- frontrun-0.5.0/frontrun/_redis_patch_registry.py +28 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/_report.py +8 -4
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/_sql_anomaly.py +103 -68
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/_sql_cursor.py +374 -576
- frontrun-0.5.0/frontrun/_sql_cursor_async.py +327 -0
- frontrun-0.5.0/frontrun/_sql_db_scope.py +163 -0
- frontrun-0.5.0/frontrun/_sql_endpoint_suppression.py +224 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/_sql_params.py +72 -5
- frontrun-0.5.0/frontrun/_sql_parsing.py +522 -0
- frontrun-0.5.0/frontrun/_sql_patch_registry.py +33 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/_sql_predicates.py +9 -2
- frontrun-0.5.0/frontrun/_sql_row_locks.py +35 -0
- frontrun-0.5.0/frontrun/_sql_transactions.py +233 -0
- frontrun-0.5.0/frontrun/_strategy.py +275 -0
- frontrun-0.5.0/frontrun/_threaded_runner.py +111 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/_trace_format.py +63 -62
- frontrun-0.5.0/frontrun/_trace_marker_runtime.py +133 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/async_dpor.py +463 -507
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/async_scheduler.py +9 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/async_shuffler.py +195 -172
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/async_trace_markers.py +26 -102
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/bytecode.py +254 -237
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/cli.py +11 -11
- frontrun-0.5.0/frontrun/common.py +307 -0
- frontrun-0.5.0/frontrun/contrib/_shared.py +27 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/contrib/django/__init__.py +6 -17
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/contrib/django/_async.py +13 -36
- frontrun-0.5.0/frontrun/contrib/django/_shared.py +105 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/contrib/django/_sync.py +13 -40
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/contrib/sqlalchemy/__init__.py +12 -17
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/contrib/sqlalchemy/_async.py +16 -26
- frontrun-0.5.0/frontrun/contrib/sqlalchemy/_shared.py +161 -0
- frontrun-0.5.0/frontrun/contrib/sqlalchemy/_sync.py +80 -0
- frontrun-0.5.0/frontrun/dpor.py +48 -0
- frontrun-0.5.0/frontrun/explore.py +185 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/pytest_plugin.py +8 -1
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/trace_markers.py +184 -296
- {frontrun-0.4.0 → frontrun-0.5.0}/pyproject.toml +1 -1
- frontrun-0.4.0/frontrun/__init__.py +0 -69
- frontrun-0.4.0/frontrun/_real_threading.py +0 -74
- frontrun-0.4.0/frontrun/_redis_client_async.py +0 -258
- frontrun-0.4.0/frontrun/_redis_parsing.py +0 -471
- frontrun-0.4.0/frontrun/_sql_cursor_async.py +0 -367
- frontrun-0.4.0/frontrun/_sql_parsing.py +0 -527
- frontrun-0.4.0/frontrun/common.py +0 -114
- frontrun-0.4.0/frontrun/contrib/sqlalchemy/_sync.py +0 -136
- frontrun-0.4.0/frontrun/dpor.py +0 -3844
- {frontrun-0.4.0 → frontrun-0.5.0}/LICENSE +0 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/crates/dpor/Cargo.toml +0 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/crates/dpor/src/access.rs +0 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/crates/dpor/src/object.rs +0 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/crates/dpor/src/vv.rs +0 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/crates/dpor/src/wakeup_tree.rs +0 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/Makefile +0 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/_report_template.html +0 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/_schema.py +0 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/_sql_insert_tracker.py +0 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/_tracing.py +0 -0
- {frontrun-0.4.0 → frontrun-0.5.0}/frontrun/contrib/__init__.py +0 -0
|
@@ -10,14 +10,14 @@ checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
|
|
|
10
10
|
|
|
11
11
|
[[package]]
|
|
12
12
|
name = "frontrun-dpor"
|
|
13
|
-
version = "0.
|
|
13
|
+
version = "0.5.0"
|
|
14
14
|
dependencies = [
|
|
15
15
|
"pyo3",
|
|
16
16
|
]
|
|
17
17
|
|
|
18
18
|
[[package]]
|
|
19
19
|
name = "frontrun-io"
|
|
20
|
-
version = "0.
|
|
20
|
+
version = "0.5.0"
|
|
21
21
|
dependencies = [
|
|
22
22
|
"libc",
|
|
23
23
|
"once_cell",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: frontrun
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5.0
|
|
4
4
|
Classifier: Development Status :: 3 - Alpha
|
|
5
5
|
Classifier: Intended Audience :: Developers
|
|
6
6
|
Classifier: Programming Language :: Python :: 3
|
|
@@ -103,9 +103,10 @@ def test_transfer_lost_update():
|
|
|
103
103
|
])
|
|
104
104
|
|
|
105
105
|
executor = TraceExecutor(schedule)
|
|
106
|
-
executor.run(
|
|
107
|
-
|
|
108
|
-
|
|
106
|
+
executor.run({
|
|
107
|
+
"thread1": lambda: account.transfer(50),
|
|
108
|
+
"thread2": lambda: account.transfer(50),
|
|
109
|
+
}, timeout=5.0)
|
|
109
110
|
|
|
110
111
|
# One update was lost: balance is 150, not 200
|
|
111
112
|
assert account.balance == 150
|
|
@@ -147,9 +148,10 @@ def test_counter_lost_update():
|
|
|
147
148
|
])
|
|
148
149
|
|
|
149
150
|
executor = TraceExecutor(schedule)
|
|
150
|
-
executor.run(
|
|
151
|
-
|
|
152
|
-
|
|
151
|
+
executor.run({
|
|
152
|
+
"thread1": counter.increment,
|
|
153
|
+
"thread2": counter.increment,
|
|
154
|
+
}, timeout=5.0)
|
|
153
155
|
|
|
154
156
|
assert counter.value == 1 # One increment lost
|
|
155
157
|
```
|
|
@@ -160,8 +162,12 @@ DPOR (Dynamic Partial Order Reduction) *systematically* explores every meaningfu
|
|
|
160
162
|
|
|
161
163
|
When a race is found, the error trace shows the exact sequence of conflicting accesses and which threads were involved:
|
|
162
164
|
|
|
165
|
+
> **Prefer `frontrun.explore()`** — the new unified entry point (0.5+). The old
|
|
166
|
+
> per-strategy functions (`explore_dpor`, `explore_interleavings`, etc.) are
|
|
167
|
+
> deprecated and scheduled for removal in 0.6.
|
|
168
|
+
|
|
163
169
|
```python
|
|
164
|
-
from frontrun
|
|
170
|
+
from frontrun import explore
|
|
165
171
|
|
|
166
172
|
class Counter:
|
|
167
173
|
def __init__(self):
|
|
@@ -171,15 +177,31 @@ class Counter:
|
|
|
171
177
|
temp = self.value
|
|
172
178
|
self.value = temp + 1
|
|
173
179
|
|
|
180
|
+
def test_counter_is_atomic():
|
|
181
|
+
result = explore(
|
|
182
|
+
setup=Counter,
|
|
183
|
+
workers=Counter.increment,
|
|
184
|
+
count=2,
|
|
185
|
+
invariant=lambda c: c.value == 2,
|
|
186
|
+
)
|
|
187
|
+
result.assert_holds()
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
<details>
|
|
191
|
+
<summary>Old API (deprecated, will be removed in 0.6)</summary>
|
|
192
|
+
|
|
193
|
+
```python
|
|
194
|
+
from frontrun.dpor import explore_dpor
|
|
195
|
+
|
|
174
196
|
def test_counter_is_atomic():
|
|
175
197
|
result = explore_dpor(
|
|
176
198
|
setup=Counter,
|
|
177
199
|
threads=[lambda c: c.increment(), lambda c: c.increment()],
|
|
178
200
|
invariant=lambda c: c.value == 2,
|
|
179
201
|
)
|
|
180
|
-
|
|
181
202
|
assert result.property_holds, result.explanation
|
|
182
203
|
```
|
|
204
|
+
</details>
|
|
183
205
|
|
|
184
206
|
This test fails because `Counter.increment` is not atomic. The `result.explanation` shows the conflict:
|
|
185
207
|
|
|
@@ -204,16 +226,36 @@ DPOR explored exactly 2 interleavings out of the 6 possible (the other 4 are equ
|
|
|
204
226
|
|
|
205
227
|
**Search strategies:** The default DFS strategy is optimal for **exhaustive exploration** (`stop_on_first=False`) — it produces the minimum number of executions. When the trace space is very large and you have a limited execution budget (`stop_on_first=True` or a low `max_executions`), use a non-DFS strategy like `search="bit-reversal"` to spread exploration across diverse conflict points early, finding bugs faster on average. See [search strategy documentation](docs/search.rst) for details.
|
|
206
228
|
|
|
207
|
-
**Scope and limitations:** DPOR tracks Python bytecode-level conflicts (attribute and subscript reads/writes, lock operations) plus I/O. Redis key-level conflicts are detected by intercepting redis-py's `execute_command()
|
|
229
|
+
**Scope and limitations:** DPOR tracks Python bytecode-level conflicts (attribute and subscript reads/writes, lock operations) plus I/O. Redis key-level conflicts are detected by intercepting redis-py's `execute_command()`; activate with `detect_io=True` (works in both sync and async from 0.5). SQL conflicts are detected by intercepting DBAPI `cursor.execute()`. These key/table-level detectors are important: raw socket detection uses `host:port` as the resource ID, so every send and recv to the same server appears to conflict — without key-level or SQL-level refinement this causes a combinatorial explosion of spurious interleavings. C-extension shared state (NumPy arrays, etc.) is not tracked at all. The `frontrun` CLI adds C-level socket interception via `LD_PRELOAD` for opaque drivers, also at the coarse `host:port` level.
|
|
208
230
|
|
|
209
|
-
### 3. Bytecode Exploration
|
|
231
|
+
### 3. Bytecode Exploration (Random Strategy)
|
|
210
232
|
|
|
211
233
|
Bytecode exploration generates random opcode-level schedules and checks an invariant under each one, in the style of [Hypothesis](https://hypothesis.readthedocs.io/). Each thread fires a [`sys.settrace`](https://docs.python.org/3/library/sys.html#sys.settrace) callback at every bytecode instruction, pausing to wait for its scheduler turn. No markers or annotations needed.
|
|
212
234
|
|
|
213
|
-
|
|
235
|
+
The random strategy often finds races very quickly — sometimes on the first attempt. It can also find races that are invisible to DPOR, because it doesn't need to understand *why* a schedule is bad; it just checks whether the invariant holds after the threads finish. If a C extension mutates shared state in a way that breaks your invariant, random exploration will stumble into it. DPOR won't, because it can't see the C-level mutation.
|
|
214
236
|
|
|
215
237
|
The trade-off: error traces are less interpretable. You get the specific opcode schedule that broke the invariant and a best-effort interleaved source trace, but not the causal conflict analysis that DPOR provides.
|
|
216
238
|
|
|
239
|
+
> **Prefer `frontrun.explore(strategy='random')`** — the new unified entry point
|
|
240
|
+
> (0.5+). The old `explore_interleavings` is deprecated and will be removed in 0.6.
|
|
241
|
+
|
|
242
|
+
```python
|
|
243
|
+
from frontrun import explore
|
|
244
|
+
|
|
245
|
+
def test_counter_is_atomic():
|
|
246
|
+
result = explore(
|
|
247
|
+
setup=lambda: Counter(value=0),
|
|
248
|
+
workers=Counter.increment,
|
|
249
|
+
count=2,
|
|
250
|
+
invariant=lambda c: c.value == 2,
|
|
251
|
+
strategy="random",
|
|
252
|
+
)
|
|
253
|
+
result.assert_holds()
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
<details>
|
|
257
|
+
<summary>Old API (deprecated, will be removed in 0.6)</summary>
|
|
258
|
+
|
|
217
259
|
```python
|
|
218
260
|
from frontrun.bytecode import explore_interleavings
|
|
219
261
|
|
|
@@ -240,6 +282,7 @@ def test_counter_is_atomic():
|
|
|
240
282
|
|
|
241
283
|
assert result.property_holds, result.explanation
|
|
242
284
|
```
|
|
285
|
+
</details>
|
|
243
286
|
|
|
244
287
|
This fails with output like:
|
|
245
288
|
|
|
@@ -306,10 +349,10 @@ def test_redis_counter_race(redis_port):
|
|
|
306
349
|
assert not result.property_holds # DPOR finds the lost-update race
|
|
307
350
|
```
|
|
308
351
|
|
|
309
|
-
**Async DPOR** —
|
|
352
|
+
**Async DPOR** — `detect_io=True` covers Redis in async too (from 0.5):
|
|
310
353
|
|
|
311
354
|
```python
|
|
312
|
-
from frontrun
|
|
355
|
+
from frontrun import explore
|
|
313
356
|
import redis.asyncio as aioredis
|
|
314
357
|
|
|
315
358
|
async def test_async_redis_race(redis_port):
|
|
@@ -319,14 +362,19 @@ async def test_async_redis_race(redis_port):
|
|
|
319
362
|
await r.set("counter", str(val + 1))
|
|
320
363
|
await r.aclose()
|
|
321
364
|
|
|
322
|
-
result = await
|
|
365
|
+
result = await explore(
|
|
323
366
|
setup=lambda: None,
|
|
324
|
-
|
|
367
|
+
workers=increment,
|
|
368
|
+
count=2,
|
|
325
369
|
invariant=lambda s: True, # check Redis directly in a real test
|
|
326
|
-
|
|
370
|
+
detect_io=True,
|
|
327
371
|
)
|
|
328
372
|
```
|
|
329
373
|
|
|
374
|
+
> In 0.5 the async-only `detect_redis=True` kwarg was folded into `detect_io=True`
|
|
375
|
+
> so sync and async behave the same. `detect_redis=True` still works through
|
|
376
|
+
> 0.5 with a `DeprecationWarning`; it is removed in 0.6.
|
|
377
|
+
|
|
330
378
|
The same key-level precision applies to hashes (`HGET`/`HSET`), lists, sets, sorted sets, and all other Redis data structures — 160+ commands are classified. See the [Redis technical details](docs/redis.rst) for a full walkthrough.
|
|
331
379
|
|
|
332
380
|
### C-Level I/O Interception
|
|
@@ -363,11 +411,11 @@ with IOEventDispatcher() as dispatcher:
|
|
|
363
411
|
By default, frontrun only traces user code — files outside the stdlib, `site-packages`, and frontrun's own internals. When the code under test lives inside an installed package (Django apps, plugin architectures, etc.), pass `trace_packages` to widen the filter:
|
|
364
412
|
|
|
365
413
|
```python
|
|
366
|
-
from frontrun
|
|
414
|
+
from frontrun import explore
|
|
367
415
|
|
|
368
|
-
result =
|
|
416
|
+
result = explore(
|
|
369
417
|
setup=make_state,
|
|
370
|
-
|
|
418
|
+
workers=[thread_a, thread_b],
|
|
371
419
|
invariant=check_invariant,
|
|
372
420
|
trace_packages=["mylib.*", "django_filters.*"],
|
|
373
421
|
)
|
|
@@ -420,13 +468,17 @@ def test_async_counter_lost_update():
|
|
|
420
468
|
assert counter.value == 1 # One increment lost
|
|
421
469
|
```
|
|
422
470
|
|
|
423
|
-
### Async
|
|
471
|
+
### Async Exploration
|
|
424
472
|
|
|
425
|
-
Async
|
|
473
|
+
Async exploration works at natural ``await`` boundaries instead of opcodes, making schedules stable across Python versions. ``frontrun.explore()`` detects async workers automatically:
|
|
474
|
+
|
|
475
|
+
> **Prefer `frontrun.explore()`** — the new unified entry point (0.5+). The old
|
|
476
|
+
> `explore_interleavings` (async form) and `explore_async_dpor` are deprecated
|
|
477
|
+
> and will be removed in 0.6.
|
|
426
478
|
|
|
427
479
|
```python
|
|
428
480
|
import asyncio
|
|
429
|
-
from frontrun import
|
|
481
|
+
from frontrun import explore
|
|
430
482
|
|
|
431
483
|
class Counter:
|
|
432
484
|
def __init__(self):
|
|
@@ -437,6 +489,35 @@ class Counter:
|
|
|
437
489
|
await asyncio.sleep(0) # any natural await is a scheduling point
|
|
438
490
|
self.value = temp + 1
|
|
439
491
|
|
|
492
|
+
# DPOR (default) — systematic
|
|
493
|
+
async def test_async_counter_dpor():
|
|
494
|
+
result = await explore(
|
|
495
|
+
setup=Counter,
|
|
496
|
+
workers=Counter.increment,
|
|
497
|
+
count=2,
|
|
498
|
+
invariant=lambda c: c.value == 2,
|
|
499
|
+
)
|
|
500
|
+
result.assert_holds()
|
|
501
|
+
|
|
502
|
+
# Random strategy — fast, probabilistic
|
|
503
|
+
async def test_async_counter_random():
|
|
504
|
+
result = await explore(
|
|
505
|
+
setup=Counter,
|
|
506
|
+
workers=Counter.increment,
|
|
507
|
+
count=2,
|
|
508
|
+
invariant=lambda c: c.value == 2,
|
|
509
|
+
strategy="random",
|
|
510
|
+
max_attempts=200,
|
|
511
|
+
)
|
|
512
|
+
result.assert_holds()
|
|
513
|
+
```
|
|
514
|
+
|
|
515
|
+
<details>
|
|
516
|
+
<summary>Old async API (deprecated, will be removed in 0.6)</summary>
|
|
517
|
+
|
|
518
|
+
```python
|
|
519
|
+
from frontrun import explore_interleavings
|
|
520
|
+
|
|
440
521
|
async def test_async_counter_race():
|
|
441
522
|
result = await explore_interleavings(
|
|
442
523
|
setup=lambda: Counter(),
|
|
@@ -444,9 +525,9 @@ async def test_async_counter_race():
|
|
|
444
525
|
invariant=lambda c: c.value == 2,
|
|
445
526
|
max_attempts=200,
|
|
446
527
|
)
|
|
447
|
-
|
|
448
528
|
assert result.property_holds, result.explanation
|
|
449
529
|
```
|
|
530
|
+
</details>
|
|
450
531
|
|
|
451
532
|
## CLI
|
|
452
533
|
|
|
@@ -505,6 +586,22 @@ confusing failures when the environment isn't properly set up.
|
|
|
505
586
|
|
|
506
587
|
## Development
|
|
507
588
|
|
|
589
|
+
### Prefer `assert_holds()` over manual asserts
|
|
590
|
+
|
|
591
|
+
`InterleavingResult` exposes a convenience helper that raises `AssertionError`
|
|
592
|
+
with the race explanation on failure and returns `None` silently on success:
|
|
593
|
+
|
|
594
|
+
```python
|
|
595
|
+
result = explore_dpor(setup, [thread1, thread2], invariant)
|
|
596
|
+
result.assert_holds() # preferred over: assert result.property_holds, result.explanation
|
|
597
|
+
```
|
|
598
|
+
|
|
599
|
+
An optional `msg_prefix` is prepended to the explanation:
|
|
600
|
+
|
|
601
|
+
```python
|
|
602
|
+
result.assert_holds(msg_prefix="transfer race: ")
|
|
603
|
+
```
|
|
604
|
+
|
|
508
605
|
### Running Tests
|
|
509
606
|
|
|
510
607
|
```bash
|
|
@@ -61,9 +61,10 @@ def test_transfer_lost_update():
|
|
|
61
61
|
])
|
|
62
62
|
|
|
63
63
|
executor = TraceExecutor(schedule)
|
|
64
|
-
executor.run(
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
executor.run({
|
|
65
|
+
"thread1": lambda: account.transfer(50),
|
|
66
|
+
"thread2": lambda: account.transfer(50),
|
|
67
|
+
}, timeout=5.0)
|
|
67
68
|
|
|
68
69
|
# One update was lost: balance is 150, not 200
|
|
69
70
|
assert account.balance == 150
|
|
@@ -105,9 +106,10 @@ def test_counter_lost_update():
|
|
|
105
106
|
])
|
|
106
107
|
|
|
107
108
|
executor = TraceExecutor(schedule)
|
|
108
|
-
executor.run(
|
|
109
|
-
|
|
110
|
-
|
|
109
|
+
executor.run({
|
|
110
|
+
"thread1": counter.increment,
|
|
111
|
+
"thread2": counter.increment,
|
|
112
|
+
}, timeout=5.0)
|
|
111
113
|
|
|
112
114
|
assert counter.value == 1 # One increment lost
|
|
113
115
|
```
|
|
@@ -118,8 +120,12 @@ DPOR (Dynamic Partial Order Reduction) *systematically* explores every meaningfu
|
|
|
118
120
|
|
|
119
121
|
When a race is found, the error trace shows the exact sequence of conflicting accesses and which threads were involved:
|
|
120
122
|
|
|
123
|
+
> **Prefer `frontrun.explore()`** — the new unified entry point (0.5+). The old
|
|
124
|
+
> per-strategy functions (`explore_dpor`, `explore_interleavings`, etc.) are
|
|
125
|
+
> deprecated and scheduled for removal in 0.6.
|
|
126
|
+
|
|
121
127
|
```python
|
|
122
|
-
from frontrun
|
|
128
|
+
from frontrun import explore
|
|
123
129
|
|
|
124
130
|
class Counter:
|
|
125
131
|
def __init__(self):
|
|
@@ -129,15 +135,31 @@ class Counter:
|
|
|
129
135
|
temp = self.value
|
|
130
136
|
self.value = temp + 1
|
|
131
137
|
|
|
138
|
+
def test_counter_is_atomic():
|
|
139
|
+
result = explore(
|
|
140
|
+
setup=Counter,
|
|
141
|
+
workers=Counter.increment,
|
|
142
|
+
count=2,
|
|
143
|
+
invariant=lambda c: c.value == 2,
|
|
144
|
+
)
|
|
145
|
+
result.assert_holds()
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
<details>
|
|
149
|
+
<summary>Old API (deprecated, will be removed in 0.6)</summary>
|
|
150
|
+
|
|
151
|
+
```python
|
|
152
|
+
from frontrun.dpor import explore_dpor
|
|
153
|
+
|
|
132
154
|
def test_counter_is_atomic():
|
|
133
155
|
result = explore_dpor(
|
|
134
156
|
setup=Counter,
|
|
135
157
|
threads=[lambda c: c.increment(), lambda c: c.increment()],
|
|
136
158
|
invariant=lambda c: c.value == 2,
|
|
137
159
|
)
|
|
138
|
-
|
|
139
160
|
assert result.property_holds, result.explanation
|
|
140
161
|
```
|
|
162
|
+
</details>
|
|
141
163
|
|
|
142
164
|
This test fails because `Counter.increment` is not atomic. The `result.explanation` shows the conflict:
|
|
143
165
|
|
|
@@ -162,16 +184,36 @@ DPOR explored exactly 2 interleavings out of the 6 possible (the other 4 are equ
|
|
|
162
184
|
|
|
163
185
|
**Search strategies:** The default DFS strategy is optimal for **exhaustive exploration** (`stop_on_first=False`) — it produces the minimum number of executions. When the trace space is very large and you have a limited execution budget (`stop_on_first=True` or a low `max_executions`), use a non-DFS strategy like `search="bit-reversal"` to spread exploration across diverse conflict points early, finding bugs faster on average. See [search strategy documentation](docs/search.rst) for details.
|
|
164
186
|
|
|
165
|
-
**Scope and limitations:** DPOR tracks Python bytecode-level conflicts (attribute and subscript reads/writes, lock operations) plus I/O. Redis key-level conflicts are detected by intercepting redis-py's `execute_command()
|
|
187
|
+
**Scope and limitations:** DPOR tracks Python bytecode-level conflicts (attribute and subscript reads/writes, lock operations) plus I/O. Redis key-level conflicts are detected by intercepting redis-py's `execute_command()`; activate with `detect_io=True` (works in both sync and async from 0.5). SQL conflicts are detected by intercepting DBAPI `cursor.execute()`. These key/table-level detectors are important: raw socket detection uses `host:port` as the resource ID, so every send and recv to the same server appears to conflict — without key-level or SQL-level refinement this causes a combinatorial explosion of spurious interleavings. C-extension shared state (NumPy arrays, etc.) is not tracked at all. The `frontrun` CLI adds C-level socket interception via `LD_PRELOAD` for opaque drivers, also at the coarse `host:port` level.
|
|
166
188
|
|
|
167
|
-
### 3. Bytecode Exploration
|
|
189
|
+
### 3. Bytecode Exploration (Random Strategy)
|
|
168
190
|
|
|
169
191
|
Bytecode exploration generates random opcode-level schedules and checks an invariant under each one, in the style of [Hypothesis](https://hypothesis.readthedocs.io/). Each thread fires a [`sys.settrace`](https://docs.python.org/3/library/sys.html#sys.settrace) callback at every bytecode instruction, pausing to wait for its scheduler turn. No markers or annotations needed.
|
|
170
192
|
|
|
171
|
-
|
|
193
|
+
The random strategy often finds races very quickly — sometimes on the first attempt. It can also find races that are invisible to DPOR, because it doesn't need to understand *why* a schedule is bad; it just checks whether the invariant holds after the threads finish. If a C extension mutates shared state in a way that breaks your invariant, random exploration will stumble into it. DPOR won't, because it can't see the C-level mutation.
|
|
172
194
|
|
|
173
195
|
The trade-off: error traces are less interpretable. You get the specific opcode schedule that broke the invariant and a best-effort interleaved source trace, but not the causal conflict analysis that DPOR provides.
|
|
174
196
|
|
|
197
|
+
> **Prefer `frontrun.explore(strategy='random')`** — the new unified entry point
|
|
198
|
+
> (0.5+). The old `explore_interleavings` is deprecated and will be removed in 0.6.
|
|
199
|
+
|
|
200
|
+
```python
|
|
201
|
+
from frontrun import explore
|
|
202
|
+
|
|
203
|
+
def test_counter_is_atomic():
|
|
204
|
+
result = explore(
|
|
205
|
+
setup=lambda: Counter(value=0),
|
|
206
|
+
workers=Counter.increment,
|
|
207
|
+
count=2,
|
|
208
|
+
invariant=lambda c: c.value == 2,
|
|
209
|
+
strategy="random",
|
|
210
|
+
)
|
|
211
|
+
result.assert_holds()
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
<details>
|
|
215
|
+
<summary>Old API (deprecated, will be removed in 0.6)</summary>
|
|
216
|
+
|
|
175
217
|
```python
|
|
176
218
|
from frontrun.bytecode import explore_interleavings
|
|
177
219
|
|
|
@@ -198,6 +240,7 @@ def test_counter_is_atomic():
|
|
|
198
240
|
|
|
199
241
|
assert result.property_holds, result.explanation
|
|
200
242
|
```
|
|
243
|
+
</details>
|
|
201
244
|
|
|
202
245
|
This fails with output like:
|
|
203
246
|
|
|
@@ -264,10 +307,10 @@ def test_redis_counter_race(redis_port):
|
|
|
264
307
|
assert not result.property_holds # DPOR finds the lost-update race
|
|
265
308
|
```
|
|
266
309
|
|
|
267
|
-
**Async DPOR** —
|
|
310
|
+
**Async DPOR** — `detect_io=True` covers Redis in async too (from 0.5):
|
|
268
311
|
|
|
269
312
|
```python
|
|
270
|
-
from frontrun
|
|
313
|
+
from frontrun import explore
|
|
271
314
|
import redis.asyncio as aioredis
|
|
272
315
|
|
|
273
316
|
async def test_async_redis_race(redis_port):
|
|
@@ -277,14 +320,19 @@ async def test_async_redis_race(redis_port):
|
|
|
277
320
|
await r.set("counter", str(val + 1))
|
|
278
321
|
await r.aclose()
|
|
279
322
|
|
|
280
|
-
result = await
|
|
323
|
+
result = await explore(
|
|
281
324
|
setup=lambda: None,
|
|
282
|
-
|
|
325
|
+
workers=increment,
|
|
326
|
+
count=2,
|
|
283
327
|
invariant=lambda s: True, # check Redis directly in a real test
|
|
284
|
-
|
|
328
|
+
detect_io=True,
|
|
285
329
|
)
|
|
286
330
|
```
|
|
287
331
|
|
|
332
|
+
> In 0.5 the async-only `detect_redis=True` kwarg was folded into `detect_io=True`
|
|
333
|
+
> so sync and async behave the same. `detect_redis=True` still works through
|
|
334
|
+
> 0.5 with a `DeprecationWarning`; it is removed in 0.6.
|
|
335
|
+
|
|
288
336
|
The same key-level precision applies to hashes (`HGET`/`HSET`), lists, sets, sorted sets, and all other Redis data structures — 160+ commands are classified. See the [Redis technical details](docs/redis.rst) for a full walkthrough.
|
|
289
337
|
|
|
290
338
|
### C-Level I/O Interception
|
|
@@ -321,11 +369,11 @@ with IOEventDispatcher() as dispatcher:
|
|
|
321
369
|
By default, frontrun only traces user code — files outside the stdlib, `site-packages`, and frontrun's own internals. When the code under test lives inside an installed package (Django apps, plugin architectures, etc.), pass `trace_packages` to widen the filter:
|
|
322
370
|
|
|
323
371
|
```python
|
|
324
|
-
from frontrun
|
|
372
|
+
from frontrun import explore
|
|
325
373
|
|
|
326
|
-
result =
|
|
374
|
+
result = explore(
|
|
327
375
|
setup=make_state,
|
|
328
|
-
|
|
376
|
+
workers=[thread_a, thread_b],
|
|
329
377
|
invariant=check_invariant,
|
|
330
378
|
trace_packages=["mylib.*", "django_filters.*"],
|
|
331
379
|
)
|
|
@@ -378,13 +426,17 @@ def test_async_counter_lost_update():
|
|
|
378
426
|
assert counter.value == 1 # One increment lost
|
|
379
427
|
```
|
|
380
428
|
|
|
381
|
-
### Async
|
|
429
|
+
### Async Exploration
|
|
382
430
|
|
|
383
|
-
Async
|
|
431
|
+
Async exploration works at natural ``await`` boundaries instead of opcodes, making schedules stable across Python versions. ``frontrun.explore()`` detects async workers automatically:
|
|
432
|
+
|
|
433
|
+
> **Prefer `frontrun.explore()`** — the new unified entry point (0.5+). The old
|
|
434
|
+
> `explore_interleavings` (async form) and `explore_async_dpor` are deprecated
|
|
435
|
+
> and will be removed in 0.6.
|
|
384
436
|
|
|
385
437
|
```python
|
|
386
438
|
import asyncio
|
|
387
|
-
from frontrun import
|
|
439
|
+
from frontrun import explore
|
|
388
440
|
|
|
389
441
|
class Counter:
|
|
390
442
|
def __init__(self):
|
|
@@ -395,6 +447,35 @@ class Counter:
|
|
|
395
447
|
await asyncio.sleep(0) # any natural await is a scheduling point
|
|
396
448
|
self.value = temp + 1
|
|
397
449
|
|
|
450
|
+
# DPOR (default) — systematic
|
|
451
|
+
async def test_async_counter_dpor():
|
|
452
|
+
result = await explore(
|
|
453
|
+
setup=Counter,
|
|
454
|
+
workers=Counter.increment,
|
|
455
|
+
count=2,
|
|
456
|
+
invariant=lambda c: c.value == 2,
|
|
457
|
+
)
|
|
458
|
+
result.assert_holds()
|
|
459
|
+
|
|
460
|
+
# Random strategy — fast, probabilistic
|
|
461
|
+
async def test_async_counter_random():
|
|
462
|
+
result = await explore(
|
|
463
|
+
setup=Counter,
|
|
464
|
+
workers=Counter.increment,
|
|
465
|
+
count=2,
|
|
466
|
+
invariant=lambda c: c.value == 2,
|
|
467
|
+
strategy="random",
|
|
468
|
+
max_attempts=200,
|
|
469
|
+
)
|
|
470
|
+
result.assert_holds()
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
<details>
|
|
474
|
+
<summary>Old async API (deprecated, will be removed in 0.6)</summary>
|
|
475
|
+
|
|
476
|
+
```python
|
|
477
|
+
from frontrun import explore_interleavings
|
|
478
|
+
|
|
398
479
|
async def test_async_counter_race():
|
|
399
480
|
result = await explore_interleavings(
|
|
400
481
|
setup=lambda: Counter(),
|
|
@@ -402,9 +483,9 @@ async def test_async_counter_race():
|
|
|
402
483
|
invariant=lambda c: c.value == 2,
|
|
403
484
|
max_attempts=200,
|
|
404
485
|
)
|
|
405
|
-
|
|
406
486
|
assert result.property_holds, result.explanation
|
|
407
487
|
```
|
|
488
|
+
</details>
|
|
408
489
|
|
|
409
490
|
## CLI
|
|
410
491
|
|
|
@@ -463,6 +544,22 @@ confusing failures when the environment isn't properly set up.
|
|
|
463
544
|
|
|
464
545
|
## Development
|
|
465
546
|
|
|
547
|
+
### Prefer `assert_holds()` over manual asserts
|
|
548
|
+
|
|
549
|
+
`InterleavingResult` exposes a convenience helper that raises `AssertionError`
|
|
550
|
+
with the race explanation on failure and returns `None` silently on success:
|
|
551
|
+
|
|
552
|
+
```python
|
|
553
|
+
result = explore_dpor(setup, [thread1, thread2], invariant)
|
|
554
|
+
result.assert_holds() # preferred over: assert result.property_holds, result.explanation
|
|
555
|
+
```
|
|
556
|
+
|
|
557
|
+
An optional `msg_prefix` is prepended to the explanation:
|
|
558
|
+
|
|
559
|
+
```python
|
|
560
|
+
result.assert_holds(msg_prefix="transfer race: ")
|
|
561
|
+
```
|
|
562
|
+
|
|
466
563
|
### Running Tests
|
|
467
564
|
|
|
468
565
|
```bash
|