fix(analyzer): prevent debug panic from stale by-reference counters#1242
Conversation
|
How did you trigger this panic exactly? I put there on purpose, because I wanted to be able to replicate this somehow. I know it panics on wordpress with dev builds, but never managed to reproduce.
|
|
@npo-mmenke please add a PHP code test case in crates/analyzer/tests/cases if you actually managed to reproduce the panic, i have no idea what this change is doing now or what its meant to solve. |
I can reproduce the panic reliably on an unpatched debug build of Setup:
Exact reproduction steps: # in mago repo
cargo build -p mago
# run from mago repo
/usr/bin/time -f 'wall_s=%e max_rss_kb=%M' \
target/debug/mago \
--workspace ../php-toolchain-benchmarks/workspace/wordpress \
--config ../php-toolchain-benchmarks/project-configurations/wordpress/mago.toml \
analyze --reporting-format=count
Result on my side:
- exit code: 101
- panic site: crates/analyzer/src/context/block.rs:508:13
- sample panic messages:
- No references found for variable $info['flv']['framecount'], even though it has a reference count of 1
- No references found for variable $info['ogg']['comments_raw'], even though it has a reference count of 1
- No references found for variable $thisfile_riff_WAVE['rgad'][0]['data'], even though it has a reference count of 1 |
|
The problem is we don't have an isolated case where we can reproduce this, i am aware of the panic in wordpress, it was the reason i initially added it there when adding support for references, while the changes may remove the panic, i have no idea if this is the proper fix, and as long as we dont have an isolated test to reproduce it, we can't fix it. |
|
working on a reproducer |
Restore the intentional debug_assert in remove_possible_reference. Add an isolated PHP regression case for the stale by-reference counter panic (issue_1242_reference_count_sync). Register the new analyzer test case in tests/mod.rs.
|
Updated in the latest commit:
Validation:
|
Add a regression unit test for reference chains ( =& ; =& ). When removing a referenced variable, keep existing counts on the promoted reference and add reassigned references instead of dropping the count.
Add unit tests for promoted-reference count handling and debug stale-count panic behavior. Add two integration cases for reconciler reference graph paths and register them in tests/mod.rs.
|
Added targeted test coverage for the reference-count sync changes. What was added:
Local validation:
All passed. |
Summary
This fixes a debug-only analyzer panic triggered on large WordPress analysis runs:
No references found for variable ..., even though it has a reference count of 1Root cause:
references_in_scopeentries could be removed in reconciliation paths without keepingreferenced_countsin sync.Changes
reconciler::reconcile_keyed_types.references_in_scopeentriesBlockContext::remove_possible_referenceresilient to stale counter state.> 0but no references are present, clean up the stale counter instead of panickingremove_possible_reference_tolerates_stale_reference_countValidation
cargo test -p mago-analyzer --lockederror/warning/help/note)