← Back to Pull requests

refactor: split Context.Run, Subcontext.Run and NewSubcontext (maat #1) #245

Closed
unknown closed this pull request without merging on Nov 18, 2026 16:34 UTC
View on GitHub
unknown unknown commented on Nov 12, 2026 17:43 UTC
Author

Split Context.Run, Subcontext.Run and Context.NewSubcontext into small helpers. This is item #1 of the maat assessment (DD-29), tracked as DD-31.

These three functions had the most maat hits in the root package: 2 critical MAAT-FUNC-006 hits (12 and 13 return points), a cyclomatic complexity up to 17, and a maintainability index below 44. Each Run now keeps only the guards, the run timer and its two defers. The rest moves to helpers, so each defer stays in the function that owns its resource. The order in which defers run does not change.

There is no change of public API and no change of behavior. Only context.go, subcontext.go and the new run_paths_test.go change.

Commits

Read them in order. go test ./... passes after each commit.

1. test: cover Run guard paths and pinner hand-off. Adds run_paths_test.go. All tests pass on main before the refactor.
2. refactor: split Context.Run into helpers.
3. refactor: split Subcontext.Run into helpers. Same names and shape as commit 2, to compare side by side.
4. refactor: split Context.NewSubcontext into helpers.
5. refactor: move NewSubcontext closed guard into initSubcontextLocked. Added after review round 1 (see the last section). It only moves one guard and adds doc comments.

New helpers (same names on Context and Subcontext): emptyRunError, checkRunnable, encodeAndEval, encodeRunData, startEvalLocked, evalLocked, keepOrUnpin. For NewSubcontext: initSubcontextLocked, newSubcontextTimer, addSubcontextLocked.

Review guide: lock and defer order

Run calls encodeAndEval, which calls evalLocked. A callee returns before its caller, and Go runs defers in LIFO order in one function. So the order is the same as on main.

| Runs | Context.Run defer (final line) | In | Subcontext.Run defer (final line) | In |
|---|---|---|---|---|
| 1st | ObjectDestroy (context.go:329) | evalLocked | ObjectDestroy (subcontext.go:188) | evalLocked |
| 2nd | resultPinner.Unpin (context.go:326) | evalLocked | resultPinner.Unpin (subcontext.go:185) | evalLocked |
| 3rd | evalsInFlight.Done (context.go:280) | encodeAndEval | parent.evalsInFlight.Done (subcontext.go:132) | encodeAndEval |
| 4th | mu.Unlock (context.go:275) | encodeAndEval | s.mu.Unlock (subcontext.go:127) | encodeAndEval |
| 5th | pinner closure (context.go:263) calls keepOrUnpin (context.go:342) | encodeAndEval | pinner closure (subcontext.go:115) calls keepOrUnpin (subcontext.go:200) | encodeAndEval |
| 6th | runTimer.Stop (context.go:219) | Run | runTimer.Stop (subcontext.go:70) | Run |
| 7th | TimerStats (context.go:216) | Run | TimerStats (subcontext.go:67) | Run |

In NewSubcontext, the handle-release defer (context.go:115) and mu.Unlock (context.go:122) stay in NewSubcontext. The new helpers have no defer. success = true is still set before the Subcontext is allocated and registered.

These four points stay true:
1. evalsInFlight.Add is under the lock: Context.startEvalLocked is only called between context.mu.Lock() and its deferred unlock. Subcontext.startEvalLocked calls Add between s.parent.mu.Lock() and s.parent.mu.Unlock().
2. The lock order is s.mu, then parent.mu. Only Subcontext.startEvalLocked takes parent.mu, while the caller holds s.mu.
3. ObjectDestroy and the result unpin run in evalLocked, so before the eval lock is released.
4. wafOwnsData = true is the last statement before ContextEval / SubcontextEval. The pinner closure reads the value when encodeAndEval returns, after the unlock, as on main.

The pinner defer must stay a closure. defer x.keepOrUnpin(pinner, wafOwnsData) would read wafOwnsData too early and unpin data that the WAF still reads. The existing tests do not catch this. TestContextRunKeepsPinnerUntilClose, TestSubcontextRunKeepsPinnerUntilClose and mutations M1/M2 do.

The pinner is still allocated (new(runtime.Pinner) in encodeAndEval) after newRunTimer succeeded, as on main. No recover is added. wafEncodeTimer.Stop() is still a plain call before the error check.

keepOrUnpin body diff

The two keepOrUnpin copies (comments removed, context. and s. replaced by R.) are identical. This command prints nothing:

body() { awk -v sig="$2" 'index($0, sig) == 1 {p=1} p {print} p && /^}/ {exit}' "$1" | grep -v '^[[:space:]]*//' | perl -pe 's/\b(context|s)\./R./g; s/^func \([^)]*\)/func (R)/'; }
diff <(body context.go 'func (context *Context) keepOrUnpin(') <(body subcontext.go 'func (s *Subcontext) keepOrUnpin(')

Result: empty output, exit code 0.

New tests (run_paths_test.go)

Fixture: the Arachni test rule (ua0-600-12x, match_regex ^Arachni) on the address my.input. The input {"my.input": "Arachni"} matches it, so the first evaluation on a fresh Context creates one event. The tests that check !HasEvents therefore fail if the WAF evaluates the input when it should not. (The WAF reports a rule only once per context, so a second run of the same input has no event.)

| Test | What it checks |
|---|---|
| TestContextRunKeepsPinnerUntilClose, TestSubcontextRunKeepsPinnerUntilClose | Each successful run hands its input pinner to the runner. Close releases them. |
| TestSubcontextRunParentClosedBeforeEval | A run rejected because the parent is closed leaves no outstanding evaluation (Close returns within 5 s). The closedHint restore runs once only, before Close. |
| TestRunChecksUnderEvalLock | The closed and ctx.Err checks made under the eval lock, for Context and Subcontext. A context wrapper (errGateCtx) proves that Run passed the earlier checks before the test changes the state. No sleep. |
| TestRunGuardOrder | Order of the guards: nil, empty, closed, exhausted timer, ctx.Err. |
| TestRunUnknownTimerKeyKeepsNoPinner | A run with an unknown TimerKey fails and keeps no input pinner. |

Evidence (not run by CI)

maat

maat analyze ./... --output json --detail full, on main (2727c22) and on this PR head. Exit code 1 in both cases, because the score is below the threshold of 95.

| Score | Before | After |
|---|---|---|
| Overall | 90 | 91 |
| Root module | 79 | 82 |
| context.go | 55 | 100 |
| subcontext.go | 62 | 100 |
| run_paths_test.go | - | 100 |

Hits removed: 28 (2 critical, 11 error, 15 warning). Hits added: 0. Total hits: 198 before, 170 after.

| File | Function | Hits | Rules (severity, value) |
|---|---|---|---|
| context.go | NewSubcontext | 8 | CC-001 (warning, 13), CC-002 (warning, 13), FUNC-002 (warning, 13), FUNC-003 (warning, 36), FUNC-006 (warning, 7), HALSTEAD-001 (warning, 1099.1), MI-001 (error, 43.0), SLOC-001 (warning, 36) |
| context.go | Run | 10 | CC-001 (warning, 15), CC-002 (warning, 13), FUNC-002 (warning, 15), FUNC-003 (error, 51), FUNC-006 (critical, 12), HALSTEAD-001 (warning, 1533.7), HALSTEAD-002 (warning, 21690.6), IMPL-002 (error, 11.2), MI-001 (error, 38.4), SLOC-001 (error, 51) |
| subcontext.go | Run | 10 | CC-001 (error, 17), CC-002 (warning, 15), FUNC-002 (error, 17), FUNC-003 (error, 55), FUNC-006 (critical, 13), HALSTEAD-001 (warning, 1718.2), HALSTEAD-002 (warning, 30008.5), IMPL-002 (error, 11.2), MI-001 (error, 37.1), SLOC-001 (error, 55) |

Hits that stay in these files, not on the three functions: MAAT-ARCH-005 on Context and Subcontext, MAAT-MI-001 on Context.Close and Subcontext.close.

Compiler diagnostics

go version: go1.27.4 darwin/arm64

E1, go build -gcflags=-m . 2>&1 | grep -E '^\./(context|subcontext)\.go' | grep -E 'wafOwnsData|moved to heap': exactly 4 lines, as expected.

./context.go:324:100: wafOwnsData does not escape
./context.go:327:6: moved to heap: result
./subcontext.go:183:97: wafOwnsData does not escape
./subcontext.go:186:6: moved to heap: result

E2, go build -gcflags='-d=defer' . 2>&1 | grep -E '^\./(context|subcontext)\.go': 21 lines, all 21 end with open-coded defer.

./context.go:115:2: open-coded defer
./context.go:122:2: open-coded defer
./context.go:216:2: open-coded defer
./context.go:219:2: open-coded defer
./context.go:263:2: open-coded defer
./context.go:275:2: open-coded defer
./context.go:280:2: open-coded defer
./context.go:326:2: open-coded defer
./context.go:329:2: open-coded defer
./context.go:348:2: open-coded defer
./context.go:412:2: open-coded defer
./context.go:433:2: open-coded defer
./subcontext.go:67:2: open-coded defer
./subcontext.go:70:2: open-coded defer
./subcontext.go:115:2: open-coded defer
./subcontext.go:127:2: open-coded defer
./subcontext.go:132:2: open-coded defer
./subcontext.go:185:2: open-coded defer
./subcontext.go:188:2: open-coded defer
./subcontext.go:206:2: open-coded defer
./subcontext.go:335:2: open-coded defer

Mutation checks (M1-M7)

Each mutation was applied alone to a throwaway copy outside the repo. In the M3-M6 rows, the no-kept-pinner assertion fails too. M3-M6 also fail on commit 1 (the tests on main code, with the same mutation on the original lines).

| # | Mutation | Run on | Test that fails | Result |
|---|---|---|---|---|
| M1 | defer func() { context.keepOrUnpin(...) }() becomes defer context.keepOrUnpin(pinner, wafOwnsData) | PR head | TestContextRunKeepsPinnerUntilClose | fails |
| M2 | the same in Subcontext.encodeAndEval | PR head | TestSubcontextRunKeepsPinnerUntilClose | fails |
| M3 | delete the locked closedHint check of Context | commit 1 and PR head | TestRunChecksUnderEvalLock/Context/closed | fails (both) |
| M4 | delete the locked ctx.Err check of Context | commit 1 and PR head | TestRunChecksUnderEvalLock/Context/cancel | fails (both). The error assertion still passes, !HasEvents and no-kept-pinner fail. |
| M5 | delete the locked s.closedHint check | commit 1 and PR head | TestRunChecksUnderEvalLock/Subcontext/closed | fails (both) |
| M6 | delete the locked ctx.Err check of Subcontext | commit 1 and PR head | TestRunChecksUnderEvalLock/Subcontext/cancel | fails (both). Same as M4. |
| M7 | swap the timer guard and the ctx.Err guard in checkRunnable (Context, then Subcontext) | PR head | TestRunGuardOrder/.../timer_comes_before_ctx.Err | fails (both) |

Only the target case fails in M3-M7. The other cases of the same test pass.

Race and vet

| Command | Result |
|---|---|
| go test -count=1 ./... | pass |
| go vet ./... | pass |
| go test -race -count=3 ./... | pass |
| go test -race -count=20 -run 'Race\|Concurrent\|Close\|SubContext\|Subcontext\|Canceled\|Sibling\|GoroutineLeaks\|Pinner\|UnderEvalLock\|GuardOrder' . (42 tests, 81 s) | pass |

Benchmarks

Machine: Apple M5 Pro, AC power. go test -c for before (main at 2727c22) and after (this PR head). 10 rounds, the order of before/after alternates between rounds, -test.benchmem. Set: BenchmarkWAF|BenchmarkRunOnly|BenchmarkSubcontextRunOnly|BenchmarkSibling|BenchmarkContextRun|BenchmarkSubcontextRun. Control: WAF/ContextLifecycle.

Verdict per row: every row passes, the control is stable.

  • allocs/op: equal on every row, in the main run and in both reruns.

  • First run: one row had a significant sec/op increase, WAF/DataComplexity/Heavy (+1.57 %, p=0.035). Three rows had a median delta of +2 % or more with p >= 0.05 (inconclusive by the rule): WAF/DataComplexity/Minimal (+2.7 %), WAF/DataComplexity/Realistic (+2.7 %) and ContextRun (+2.9 %). The control WAF/ContextLifecycle moved +2.0 % with p=0.315, so it is stable.

  • Rerun 1 (20 rounds, new files -rerun1.txt): Heavy +1.4 % (p=0.989), Minimal +0.9 %, Realistic +0.1 %. None significant, all below +2 %. Control ContextLifecycle p=0.445, stable.

  • Rerun 2 (20 rounds, new files -rerun2.txt): ContextRun -1.5 % (p=0.239). Control p=0.862, stable.
  • <details><summary>benchstat, main run (10 rounds): full output</summary>

    goos: darwin
    goarch: arm64
    pkg: github.com/DataDog/go-libddwaf/v5
    cpu: Apple M5 Pro
                                           │  before.txt  │              after.txt              │
                                           │    sec/op    │    sec/op     vs base               │
    WAF/RequestResponse-18                   46.29µ ± 10%   46.88µ ±  5%       ~ (p=0.631 n=10)
    WAF/Run/SmallRuleset/NoAttack-18         4.687µ ±  5%   4.672µ ± 16%       ~ (p=0.796 n=10)
    WAF/Run/SmallRuleset/Attack-18           6.372µ ±  5%   6.370µ ±  6%       ~ (p=0.781 n=10)
    WAF/Run/RecommendedRuleset/NoAttack-18   39.59µ ±  6%   39.15µ ±  5%       ~ (p=0.684 n=10)
    WAF/Run/RecommendedRuleset/Attack-18     38.37µ ±  5%   38.81µ ±  6%       ~ (p=0.912 n=10)
    WAF/ContextLifecycle-18                  708.2n ±  5%   722.3n ±  7%       ~ (p=0.315 n=10)
    WAF/DataComplexity/Minimal-18            9.277µ ±  8%   9.527µ ±  8%       ~ (p=0.895 n=10)
    WAF/DataComplexity/Realistic-18          38.80µ ±  6%   39.85µ ±  9%       ~ (p=0.393 n=10)
    WAF/DataComplexity/Heavy-18              827.4µ ±  1%   840.4µ ±  3%  +1.57% (p=0.035 n=10)
    WAF/Parallel-18                          8.708µ ±  4%   8.691µ ±  5%       ~ (p=0.579 n=10)
    WAF/Subcontext-18                        17.70µ ±  4%   17.87µ ±  9%       ~ (p=0.684 n=10)
    RunOnly-18                               32.28µ ±  6%   32.51µ ±  5%       ~ (p=0.631 n=10)
    SubcontextRunOnly-18                     13.86µ ±  2%   13.82µ ±  5%       ~ (p=0.739 n=10)
    SiblingSubcontextParallelism-18          2.364µ ± 14%   2.384µ ± 11%       ~ (p=0.971 n=10)
    SiblingSubcontextSerialized-18           2.001µ ±  4%   2.037µ ± 12%       ~ (p=0.165 n=10)
    ContextRun-18                            2.002µ ±  6%   2.059µ ± 11%       ~ (p=0.315 n=10)
    SubcontextRun-18                         1.990µ ±  8%   2.006µ ±  7%       ~ (p=0.928 n=10)
    geomean                                  11.08µ         11.20µ        +1.01%
    
                                           │  before.txt  │               after.txt               │
                                           │     B/op     │     B/op      vs base                 │
    WAF/RequestResponse-18                   6.326Ki ± 0%   6.326Ki ± 0%       ~ (p=1.000 n=10) ¹
    WAF/Run/SmallRuleset/NoAttack-18         3.808Ki ± 0%   3.808Ki ± 0%       ~ (p=1.000 n=10) ¹
    WAF/Run/SmallRuleset/Attack-18           6.576Ki ± 0%   6.576Ki ± 0%       ~ (p=1.000 n=10) ¹
    WAF/Run/RecommendedRuleset/NoAttack-18   4.559Ki ± 0%   4.559Ki ± 0%       ~ (p=1.000 n=10) ¹
    WAF/Run/RecommendedRuleset/Attack-18     10.72Ki ± 0%   10.72Ki ± 0%       ~ (p=1.000 n=10)
    WAF/ContextLifecycle-18                    520.0 ± 0%     520.0 ± 0%       ~ (p=1.000 n=10) ¹
    WAF/DataComplexity/Minimal-18            2.408Ki ± 0%   2.408Ki ± 0%       ~ (p=1.000 n=10) ¹
    WAF/DataComplexity/Realistic-18          4.559Ki ± 0%   4.559Ki ± 0%       ~ (p=1.000 n=10) ¹
    WAF/DataComplexity/Heavy-18              20.18Ki ± 0%   20.17Ki ± 0%       ~ (p=0.370 n=10)
    WAF/Parallel-18                          4.577Ki ± 0%   4.577Ki ± 0%       ~ (p=0.734 n=10)
    WAF/Subcontext-18                        2.713Ki ± 0%   2.713Ki ± 0%       ~ (p=1.000 n=10) ¹
    RunOnly-18                               3.082Ki ± 0%   3.082Ki ± 0%       ~ (p=0.783 n=10)
    SubcontextRunOnly-18                     1.688Ki ± 0%   1.688Ki ± 0%       ~ (p=0.699 n=10)
    SiblingSubcontextParallelism-18          1.750Ki ± 0%   1.749Ki ± 0%       ~ (p=0.696 n=10)
    SiblingSubcontextSerialized-18           1.754Ki ± 0%   1.752Ki ± 0%       ~ (p=0.164 n=10)
    ContextRun-18                            1.756Ki ± 0%   1.755Ki ± 0%       ~ (p=0.319 n=10)
    SubcontextRun-18                         1.756Ki ± 0%   1.755Ki ± 0%       ~ (p=0.151 n=10)
    geomean                                  3.261Ki        3.260Ki       -0.02%
    ¹ all samples are equal
    
                                           │ before.txt │              after.txt              │
                                           │ allocs/op  │ allocs/op   vs base                 │
    WAF/RequestResponse-18                   141.0 ± 0%   141.0 ± 0%       ~ (p=1.000 n=10) ¹
    WAF/Run/SmallRuleset/NoAttack-18         84.00 ± 0%   84.00 ± 0%       ~ (p=1.000 n=10) ¹
    WAF/Run/SmallRuleset/Attack-18           156.0 ± 0%   156.0 ± 0%       ~ (p=1.000 n=10) ¹
    WAF/Run/RecommendedRuleset/NoAttack-18   102.0 ± 0%   102.0 ± 0%       ~ (p=1.000 n=10) ¹
    WAF/Run/RecommendedRuleset/Attack-18     282.0 ± 0%   282.0 ± 0%       ~ (p=1.000 n=10) ¹
    WAF/ContextLifecycle-18                  6.000 ± 0%   6.000 ± 0%       ~ (p=1.000 n=10) ¹
    WAF/DataComplexity/Minimal-18            40.00 ± 0%   40.00 ± 0%       ~ (p=1.000 n=10) ¹
    WAF/DataComplexity/Realistic-18          102.0 ± 0%   102.0 ± 0%       ~ (p=1.000 n=10) ¹
    WAF/DataComplexity/Heavy-18              496.0 ± 0%   496.0 ± 0%       ~ (p=1.000 n=10) ¹
    WAF/Parallel-18                          102.0 ± 0%   102.0 ± 0%       ~ (p=1.000 n=10) ¹
    WAF/Subcontext-18                        42.00 ± 0%   42.00 ± 0%       ~ (p=1.000 n=10) ¹
    RunOnly-18                               73.00 ± 0%   73.00 ± 0%       ~ (p=1.000 n=10) ¹
    SubcontextRunOnly-18                     29.00 ± 0%   29.00 ± 0%       ~ (p=1.000 n=10) ¹
    SiblingSubcontextParallelism-18          32.00 ± 0%   32.00 ± 0%       ~ (p=1.000 n=10) ¹
    SiblingSubcontextSerialized-18           32.00 ± 0%   32.00 ± 0%       ~ (p=1.000 n=10) ¹
    ContextRun-18                            32.00 ± 0%   32.00 ± 0%       ~ (p=1.000 n=10) ¹
    SubcontextRun-18                         32.00 ± 0%   32.00 ± 0%       ~ (p=1.000 n=10) ¹
    geomean                                  64.72        64.72       +0.00%
    ¹ all samples are equal
    

    </details>

    <details><summary>benchstat, rerun 1 (20 rounds): <code>WAF/DataComplexity</code> + control</summary>

    goos: darwin
    goarch: arm64
    pkg: github.com/DataDog/go-libddwaf/v5
    cpu: Apple M5 Pro
                                    │ before-rerun1.txt │          after-rerun1.txt          │
                                    │      sec/op       │   sec/op     vs base               │
    WAF/ContextLifecycle-18                 730.6n ± 3%   752.0n ± 4%       ~ (p=0.445 n=20)
    WAF/DataComplexity/Minimal-18           9.470µ ± 6%   9.555µ ± 5%       ~ (p=0.920 n=20)
    WAF/DataComplexity/Realistic-18         40.47µ ± 4%   40.51µ ± 3%       ~ (p=0.516 n=20)
    WAF/DataComplexity/Heavy-18             843.6µ ± 5%   855.4µ ± 4%       ~ (p=0.989 n=20)
    geomean                                 22.05µ        22.34µ       +1.32%
    
                                    │ before-rerun1.txt │           after-rerun1.txt            │
                                    │       B/op        │     B/op      vs base                 │
    WAF/ContextLifecycle-18                  520.0 ± 0%     520.0 ± 0%       ~ (p=1.000 n=20) ¹
    WAF/DataComplexity/Minimal-18          2.408Ki ± 0%   2.408Ki ± 0%       ~ (p=1.000 n=20) ¹
    WAF/DataComplexity/Realistic-18        4.559Ki ± 0%   4.559Ki ± 0%       ~ (p=1.000 n=20) ¹
    WAF/DataComplexity/Heavy-18            20.18Ki ± 0%   20.18Ki ± 0%       ~ (p=0.248 n=20)
    geomean                                3.257Ki        3.257Ki       +0.00%
    ¹ all samples are equal
    
                                    │ before-rerun1.txt │          after-rerun1.txt           │
                                    │     allocs/op     │ allocs/op   vs base                 │
    WAF/ContextLifecycle-18                  6.000 ± 0%   6.000 ± 0%       ~ (p=1.000 n=20) ¹
    WAF/DataComplexity/Minimal-18            40.00 ± 0%   40.00 ± 0%       ~ (p=1.000 n=20) ¹
    WAF/DataComplexity/Realistic-18          102.0 ± 0%   102.0 ± 0%       ~ (p=1.000 n=20) ¹
    WAF/DataComplexity/Heavy-18              496.0 ± 0%   496.0 ± 0%       ~ (p=1.000 n=20) ¹
    geomean                                  59.03        59.03       +0.00%
    ¹ all samples are equal
    

    </details>

    <details><summary>benchstat, rerun 2 (20 rounds): <code>ContextRun</code> + control</summary>

    goos: darwin
    goarch: arm64
    pkg: github.com/DataDog/go-libddwaf/v5
    cpu: Apple M5 Pro
                            │ before-rerun2.txt │          after-rerun2.txt           │
                            │      sec/op       │    sec/op     vs base               │
    WAF/ContextLifecycle-18         746.6n ± 5%   737.5n ± 10%       ~ (p=0.862 n=20)
    ContextRun-18                   1.814µ ± 3%   1.786µ ±  3%       ~ (p=0.239 n=20)
    geomean                         1.164µ        1.148µ        -1.39%
    
                            │ before-rerun2.txt │           after-rerun2.txt            │
                            │       B/op        │     B/op      vs base                 │
    WAF/ContextLifecycle-18          520.0 ± 0%     520.0 ± 0%       ~ (p=1.000 n=20) ¹
    ContextRun-18                  1.752Ki ± 0%   1.750Ki ± 0%       ~ (p=0.305 n=20)
    geomean                          965.9          965.5       -0.04%
    ¹ all samples are equal
    
                            │ before-rerun2.txt │          after-rerun2.txt           │
                            │     allocs/op     │ allocs/op   vs base                 │
    WAF/ContextLifecycle-18          6.000 ± 0%   6.000 ± 0%       ~ (p=1.000 n=20) ¹
    ContextRun-18                    32.00 ± 0%   32.00 ± 0%       ~ (p=1.000 n=20) ¹
    geomean                          13.86        13.86       +0.00%
    ¹ all samples are equal
    

    </details>

    Review round 1: changes and evidence (commit 5)

    Review round 1 found that NewSubcontext had a cyclomatic complexity of 8 and 6 returns, and asked for three doc comments. Commit 5 (bbb6fe2) does this and nothing else:

  • The closed / null ddwaf_context guard moves from NewSubcontext to the start of initSubcontextLocked. It still runs under context.mu, before ddwaf_subcontext_init. The lock and both defers stay in NewSubcontext. No new helper.

  • newSubcontextTimer states that the caller MUST hold context.mu. Both keepOrUnpin state that they take pinnersMu themselves, so the caller MUST NOT hold it. The line numbers of the defer table above are updated (keepOrUnpin moves by 1 line).
  • gocyclo context.go subcontext.go: the highest value is 6 (NewSubcontext, and the unchanged Subcontext.close). initSubcontextLocked is 5. NewSubcontext has 5 returns. No function that the PR adds or changes is above 6.

    | Check on commit 5 | Result |
    |---|---|
    | go test -count=1 ./..., go vet ./... | pass |
    | go test -race -count=3 ./... | pass |
    | targeted go test -race -count=20 -run '...' . (plan v2 §6, 76 s) | pass |
    | E1 | same 4 lines as above (wafOwnsData does not escape ×2, moved to heap: result ×2) |
    | E2 | 21 lines, all open-coded defer (raw output in the Compiler diagnostics section) |
    | keepOrUnpin body diff | empty |
    | go doc -all . against main | identical |
    | maat | overall 90 → 91, root 79 → 82, context.go 100, subcontext.go 100, run_paths_test.go 100. 28 hits removed (2 critical, 11 error, 15 warning), 0 added. Same as before commit 5. |

    Mutations M1–M7 are not run again. Commit 5 does not touch Run, startEvalLocked or keepOrUnpin code.

    Benchmarks for commit 5

    Rows that call NewSubcontext or Subcontext.Run (WAF/Subcontext, SubcontextRun, SubcontextRunOnly, SiblingSubcontext) plus the control WAF/ContextLifecycle. before is 381405e and after is 769f868 (the commit IDs before the commits were signed; the signed IDs are b1c5366 and bbb6fe2, with the same trees). Same protocol as above (alternating order, -test.benchmem). The machine was loaded by other jobs (load average 11–16), so all absolute times are higher than in the first measurement. allocs/op is equal on every row in the three attempts. B/op shows no significant increase on any row. One row has a significant B/op decrease: SiblingSubcontextSerialized in attempt 2 (−0.08 %, p=0.006).

    1. Attempt 1 (10 rounds): rejected for measurement quality. This is a deviation, not a trigger of control rule 5. The control WAF/ContextLifecycle had p=0.052, which is not below 0.05, so rule 5 did not apply. I rejected the attempt because the spread is too large to read (WAF/ContextLifecycle ±27 % before, ±337 % after). I did not use it for any verdict. Output kept below for transparency. The final verdict does not depend on it.
    2. Attempt 2 (10 rounds, new files): rows to rerun. The control is stable (p=0.912). Two rows have a median delta ≥ +2 % with p ≥ 0.05 (inconclusive by rule 3): WAF/Subcontext (+2.3 %) and SiblingSubcontextSerialized (+4.6 %). Other rows are below +2 % or negative.
    3. Rerun (20 rounds, new files, with the control): all pass. WAF/Subcontext −3.2 % (p=0.512), SiblingSubcontextSerialized −1.2 % (p=0.433), control WAF/ContextLifecycle +0.8 % (p=0.868, stable).

    Verdict per row: every row passes, the control is stable.

    <details><summary>Attempt 1 (rejected, 10 rounds)</summary>

    goos: darwin
    goarch: arm64
    pkg: github.com/DataDog/go-libddwaf/v5
    cpu: Apple M5 Pro
                                    │  before.txt  │              after.txt               │
                                    │    sec/op    │    sec/op      vs base               │
    WAF/ContextLifecycle-18           737.7n ± 27%   877.6n ± 337%       ~ (p=0.052 n=10)
    WAF/Subcontext-18                 17.75µ ± 34%   18.70µ ± 306%       ~ (p=0.353 n=10)
    SubcontextRunOnly-18              13.69µ ± 18%   13.36µ ±   3%       ~ (p=0.190 n=10)
    SiblingSubcontextParallelism-18   2.628µ ± 40%   2.590µ ±  33%       ~ (p=0.631 n=10)
    SiblingSubcontextSerialized-18    2.020µ ± 30%   2.030µ ±   3%       ~ (p=0.781 n=10)
    SubcontextRun-18                  2.017µ ±  3%   2.050µ ±  23%       ~ (p=0.271 n=10)
    geomean                           3.525µ         3.650µ         +3.54%
    
                                    │  before.txt  │               after.txt               │
                                    │     B/op     │     B/op      vs base                 │
    WAF/ContextLifecycle-18             520.0 ± 0%     520.0 ± 0%       ~ (p=1.000 n=10) ¹
    WAF/Subcontext-18                 2.713Ki ± 0%   2.713Ki ± 0%       ~ (p=1.000 n=10) ¹
    SubcontextRunOnly-18              1.688Ki ± 0%   1.688Ki ± 0%       ~ (p=0.390 n=10)
    SiblingSubcontextParallelism-18   1.749Ki ± 0%   1.749Ki ± 0%       ~ (p=1.000 n=10)
    SiblingSubcontextSerialized-18    1.754Ki ± 0%   1.753Ki ± 0%       ~ (p=0.536 n=10)
    SubcontextRun-18                  1.756Ki ± 0%   1.756Ki ± 0%       ~ (p=0.226 n=10)
    geomean                           1.524Ki        1.524Ki       -0.01%
    ¹ all samples are equal
    
                                    │ before.txt │              after.txt              │
                                    │ allocs/op  │ allocs/op   vs base                 │
    WAF/ContextLifecycle-18           6.000 ± 0%   6.000 ± 0%       ~ (p=1.000 n=10) ¹
    WAF/Subcontext-18                 42.00 ± 0%   42.00 ± 0%       ~ (p=1.000 n=10) ¹
    SubcontextRunOnly-18              29.00 ± 0%   29.00 ± 0%       ~ (p=1.000 n=10) ¹
    SiblingSubcontextParallelism-18   32.00 ± 0%   32.00 ± 0%       ~ (p=1.000 n=10) ¹
    SiblingSubcontextSerialized-18    32.00 ± 0%   32.00 ± 0%       ~ (p=1.000 n=10) ¹
    SubcontextRun-18                  32.00 ± 0%   32.00 ± 0%       ~ (p=1.000 n=10) ¹
    geomean                           24.92        24.92       +0.00%
    ¹ all samples are equal
    

    </details>

    <details><summary>Attempt 2 (10 rounds)</summary>

    goos: darwin
    goarch: arm64
    pkg: github.com/DataDog/go-libddwaf/v5
    cpu: Apple M5 Pro
                                    │ before-run2.txt │           after-run2.txt            │
                                    │     sec/op      │    sec/op     vs base               │
    WAF/ContextLifecycle-18              851.4n ±  7%   843.2n ± 19%       ~ (p=0.912 n=10)
    WAF/Subcontext-18                    20.69µ ±  6%   21.17µ ± 10%       ~ (p=0.481 n=10)
    SubcontextRunOnly-18                 16.63µ ± 29%   14.84µ ± 95%       ~ (p=0.631 n=10)
    SiblingSubcontextParallelism-18      2.834µ ±  9%   2.800µ ± 13%       ~ (p=0.912 n=10)
    SiblingSubcontextSerialized-18       2.321µ ± 22%   2.428µ ± 87%       ~ (p=0.631 n=10)
    SubcontextRun-18                     2.294µ ± 43%   2.334µ ± 77%       ~ (p=0.579 n=10)
    geomean                              4.050µ         4.017µ        -0.81%
    
                                    │ before-run2.txt │            after-run2.txt             │
                                    │      B/op       │     B/op      vs base                 │
    WAF/ContextLifecycle-18                520.0 ± 0%     520.0 ± 0%       ~ (p=1.000 n=10) ¹
    WAF/Subcontext-18                    2.713Ki ± 0%   2.713Ki ± 0%       ~ (p=1.000 n=10) ¹
    SubcontextRunOnly-18                 1.690Ki ± 0%   1.688Ki ± 0%       ~ (p=0.698 n=10)
    SiblingSubcontextParallelism-18      1.750Ki ± 0%   1.750Ki ± 0%       ~ (p=0.416 n=10)
    SiblingSubcontextSerialized-18       1.750Ki ± 0%   1.748Ki ± 0%  -0.08% (p=0.006 n=10)
    SubcontextRun-18                     1.752Ki ± 0%   1.754Ki ± 0%       ~ (p=0.588 n=10)
    geomean                              1.523Ki        1.523Ki       -0.01%
    ¹ all samples are equal
    
                                    │ before-run2.txt │           after-run2.txt            │
                                    │    allocs/op    │ allocs/op   vs base                 │
    WAF/ContextLifecycle-18                6.000 ± 0%   6.000 ± 0%       ~ (p=1.000 n=10) ¹
    WAF/Subcontext-18                      42.00 ± 0%   42.00 ± 0%       ~ (p=1.000 n=10) ¹
    SubcontextRunOnly-18                   29.00 ± 0%   29.00 ± 0%       ~ (p=1.000 n=10) ¹
    SiblingSubcontextParallelism-18        32.00 ± 0%   32.00 ± 0%       ~ (p=1.000 n=10) ¹
    SiblingSubcontextSerialized-18         32.00 ± 0%   32.00 ± 0%       ~ (p=1.000 n=10) ¹
    SubcontextRun-18                       32.00 ± 0%   32.00 ± 0%       ~ (p=1.000 n=10) ¹
    geomean                                24.92        24.92       +0.00%
    ¹ all samples are equal
    

    </details>

    <details><summary>Rerun (20 rounds): <code>WAF/Subcontext</code>, <code>SiblingSubcontextSerialized</code> + control</summary>

    goos: darwin
    goarch: arm64
    pkg: github.com/DataDog/go-libddwaf/v5
    cpu: Apple M5 Pro
                                   │ before-rerun3.txt │          after-rerun3.txt          │
                                   │      sec/op       │   sec/op     vs base               │
    WAF/ContextLifecycle-18                797.9n ± 6%   804.0n ± 7%       ~ (p=0.868 n=20)
    WAF/Subcontext-18                      19.80µ ± 8%   19.17µ ± 5%       ~ (p=0.512 n=20)
    SiblingSubcontextSerialized-18         2.095µ ± 7%   2.070µ ± 4%       ~ (p=0.433 n=20)
    geomean                                3.211µ        3.171µ       -1.22%
    
                                   │ before-rerun3.txt │           after-rerun3.txt            │
                                   │       B/op        │     B/op      vs base                 │
    WAF/ContextLifecycle-18                 520.0 ± 0%     520.0 ± 0%       ~ (p=1.000 n=20) ¹
    WAF/Subcontext-18                     2.713Ki ± 0%   2.713Ki ± 0%       ~ (p=1.000 n=20) ¹
    SiblingSubcontextSerialized-18        1.750Ki ± 0%   1.750Ki ± 0%       ~ (p=0.910 n=20)
    geomean                               1.341Ki        1.341Ki       -0.01%
    ¹ all samples are equal
    
                                   │ before-rerun3.txt │          after-rerun3.txt           │
                                   │     allocs/op     │ allocs/op   vs base                 │
    WAF/ContextLifecycle-18                 6.000 ± 0%   6.000 ± 0%       ~ (p=1.000 n=20) ¹
    WAF/Subcontext-18                       42.00 ± 0%   42.00 ± 0%       ~ (p=1.000 n=20) ¹
    SiblingSubcontextSerialized-18          32.00 ± 0%   32.00 ± 0%       ~ (p=1.000 n=20) ¹
    geomean                                 20.05        20.05       +0.00%
    ¹ all samples are equal
    

    </details>

    Out of scope

    Context.Close, Subcontext.close, ARCH-005 (field types of the structs), a shared pinner type, and the other files of the DD-29 sub-issues.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: eliottness Review duration: 8570m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #245"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%