Repository navigation
Open
Description
Summary
When a closure literal (Expr::FnLit) is declared inside an inner block (if, for, match, select, or { ... }) and captures a may_share binding m declared in an enclosing intermediate block (i.e., m lives in st.scopes[d] rather than the function-root st.env), moving m in the enclosing block after the inner block has exited (task worker(move m, done)) fails to update the snapshot stored in st.closure_scoped_envs. Consequently, closures::check_closure_boundaries sees m.moved == None inside the closure's captured snapshot and accepts concurrent mutation (Verdict::Accept with 0 diagnostics instead of rejecting with HYB-E3003).
Root Cause (model.rs, freeze.rs, closures.rs)
- When the inner block exits,
FnState::scope_pop()(compiler/crates/hy-check/src/model.rs:324-340) snapshots(snap_env, snap_aliases, snap_share)intoself.closure_scoped_envskeyed by theFnLit's(line, col)position. - When
m(declared in an enclosing blockst.scopes[d]) is subsequently moved viatask worker(move m, done),Checker::mark_move(compiler/crates/hy-check/src/freeze.rs:329-365) updatesb.moved = Some(MoveInfo { ... })onst.scopes[d]viast.env_lookup_mut(m), but never updates already-captured snapshots inst.closure_scoped_envs. - When the enclosing block exits (
model.rs:443:self.scopes.pop()), the popped scope's.movedand.non_owned_originstates are discarded without being propagated toself.closure_scoped_envs. - At function exit,
closures::check_closure_boundaries(compiler/crates/hy-check/src/closures.rs:44-47) only syncs.movedfromscoped_st.env.get(k)(the top-level function scope), which does not contain block-scoped bindingsm.
Minimal Reproducer (repro_closure_scoped_move.hy)
fn worker(m map[string]int, done chan int) {
m["worker"] = 1
done <- 1
}
fn main() {
done := make(chan int, 1)
if true {
m := map[string]int{"k": 1}
if true {
defer (fn() {
m["k"] = 2
})()
}
task worker(move m, done)
}
<-done
}- Expected:
Verdict::RejectwithHYB-E3003(the deferred closure mutatesmaftermwas moved toworker). - Actual:
Verdict::Accept(0 diagnostics), causing a concurrent map write race at runtime.
Fix
- In
Checker::mark_move(compiler/crates/hy-check/src/freeze.rs), propagateMoveInfoandnon_owned_originfor allmembersinto existing snapshots inst.closure_scoped_envs. - In
FnState::scope_pop(compiler/crates/hy-check/src/model.rs), propagate.movedand.non_owned_originfrompopped_scopeintoself.closure_scoped_envsbefore dropping the scope.
Payout Wallet (Base USDC)
- Base / EVM (USDC):
0x8132B258bC02F98EbcE321EFf18528938fB58940
Metadata
Metadata
Assignees
Labels
No labels
Activity
CrowAleksito commentedon Nov 7, 2026
Fix PR opened by original reporter (@CrowAleksito): #3949 — Official Base USDC wallet:
0x8132B258bC02F98EbcE321EFf18528938fB58940zongsm-cmyk commentedon Nov 7, 2026
Submitted clean fix with raw string test fixtures in PR #4056 (#4056).
st.closure_scoped_envsdual-write infreeze.rsandmodel.rsfor block-scoped bindings.shape.rs.r#"..."#socargo test --workspacecompiles cleanly without literal token collisions.Closes #3947
Closes #3948
/claim #3947
Base (USDC) Payout Address:
0xC10373008eFb38192C87C375ac63aB12E27C684Ctest(hy-check): use raw string literals and [t any] in limitlessproje…
CrowAleksito commentedon Nov 7, 2026
Updated original reporter PR #3949 (#3949) per @limitlessprojects review (using raw string literals
r#...#insoundness_closure_scoped_moves_and_compound_generic.rs).fix(hy-check): propagate block-scoped moves to closure_scoped_envs an…
aaskfuqw commentedon Nov 9, 2026
Confirmation + two additions: (a) the channel-send / select-send sites, and (b) the filed repro as written cannot race — here is a variant that does
I reproduced this root cause on
mainat30206b5741eec013dfc270224eaae874dc232324. Two points worth adding to the report:(a) The same stale snapshot defeats the freeze at channel sends, not only at spawns
ch <- move xs(statement send) andselect { ch <- move xs: ... }(send arm) are also accepted with 0 diagnostics, and both emit Go that races. The mechanism is exactly the one described in the "Root Cause" section — the closure's captured view is snapshotted at the inner block'sscope_pop, the latermark_moveupdates only the live scope, andcheck_closure_boundariessyncs.movedfrom the top-levelenvonly — the check-site type does not matter.(b) The repro in the issue cannot race as written; this variant does
The filed reproducer ends
mainwith<-donebefore returning. The deferred write runs after that receive, i.e. afterworkerhas already sent ondone, so nothing overlaps.go build --raceon it is silent (verified locally).Remove the wait and the race is real. This is the version I verified —
hyb check→OK (0 diagnostics), exit 0;go build --race+ run →WARNING: DATA RACEon the same word,main.sinkvs the deferred closure:Race-detector output (head):
The
map[string]inttwin ends infatal error: concurrent map writesin addition to the race report. Aselect { ch <- move m: }spelling also reproduces.Suggested regression fixtures
ch <- move xsafter an inner-blockdeferclosure, no<-done(above) →HYB-E3003.HYB-E3003.select { ch <- move xs: ... }spelling →HYB-E3003.task worker(move m, done)+<-doneshape — currently accepted; keep it accepted or reject it consistently, but it must not be the only fixture, because it cannot demonstrate the race.Environment: release build of
30206b5(HYB_PINNED_RUST=1.99.1),hyb build --race(Go 1.27.3 with the documentedHYB_ALLOW_TOOLCHAIN_DRIFT=1, winlibs gcc 16.2.0 for cgo). No repository sources were modified for this run.aaskfuqw commentedon Nov 9, 2026
Second, distinct gap in the same snapshot machinery: an alias created after the capture keeps the moved value alive (the move is on the alias, not on the captured name)
This is a different shape from the issue's reproducer, and I think it needs its own line in the fix — the snapshot is stale in two directions, not one.
Minimal repro (
alias_after_capture.hy)hyb check→OK (0 diagnostics), exit 0.hyb build --race+ run →WARNING: DATA RACE(write inmain.main.func1viaruntime.deferreturnvs write inmain.worker,mapassign_faststr, same address), 2/2 runs.Why this is a different failure from the issue's reproducer
In the filed reproducer, the captured name itself is moved (
task worker(move m, done)), and the snapshot'smovedflag formis stale. Here the captured namesharedis never moved: the move is applied toalias, a binding created after the snapshot exists. The snapshot has no record ofaliasat all, and nothing propagates the new alias back into it — so the closure's view ofsharednever learns that anything derived from it was moved/frozen.Control that isolates the trigger
Identical body without the intermediate block:
→ REJECT
HYB-E3003on this commit. So the hole is specifically the block-scoped capture snapshot, not the alias derivation itself.Channel-send spelling, same shape
struct holder { m map[string]int } fn consumer(ch <-chan holder, done chan<- bool) { h := <-ch for i in 0..2000000 { h.m["w"] = i } done <- true } fn main() { done := make(chan bool, 1) ch := make(chan holder, 1) task consumer(ch, done) if true { shared := map[string]int{} if true { defer (fn() { for i in 0..2000000 { shared["late"] = i } })() } var h holder h.m = shared ch <- move h } }hyb check→ exit 0;hyb build --race→WARNING: DATA RACE. Note this one also needs the field-store alias (h.m = shared) to be recorded into the snapshot.Suggested fixtures
alias_after_capture→HYB-E3003(this comment's first program).HYB-E3003.Fix shape
The existing plan (propagate
moved/non_owned_originfrom popped scopes and re-sync snapshots atcheck_closure_boundaries) closes the filed reproducer, but not this one: here the snapshot is missing a derivation (alias := shared,h.m = shared) that happened after it was built. The snapshot needs themay_share/alias edges created after capture to be merged in as well — i.e. snapshot invalidation on anyrecord_may_share/record_place_store_from_snapshotthat touches a captured root, not only onmark_move.Environment:
30206b57, release build,hyb build --racewith Go 1.27.3 (HYB_ALLOW_TOOLCHAIN_DRIFT=1) + winlibs gcc 16.2.0 for cgo. No repository sources modified.fix(hy-check): propagate block-scoped moves to closure_scoped_envs an…
fix(hy-check): propagate block-scoped moves to closure_scoped_envs an…
fix(hy-check): propagate block-scoped moves to closure_scoped_envs an…
fix(hy-check): propagate block-scoped moves to closure_scoped_envs an…
fix(hy-check): propagate block-scoped moves to closure_scoped_envs an…
fix(hy-check): propagate block-scoped moves to closure_scoped_envs an…
fix(hy-check): propagate block-scoped moves to closure_scoped_envs an…
fix(hy-check): propagate block-scoped moves to closure_scoped_envs an…
fix(hy-check): propagate block-scoped moves to closure_scoped_envs an…
fix(hy-check): propagate block-scoped moves to closure_scoped_envs an…
fix(hy-check): propagate block-scoped moves to closure_scoped_envs an…
fix(hy-check): propagate block-scoped moves to closure_scoped_envs an…
fix(hy-check): propagate outer-block moves to closure_scoped_envs (fixes