Commit 36ca5db
committed
Fix SMCk common-ancestor walk crash when random_mass is zero
In msp_smc_k_common_ancestor_event, gsl_ran_flat occasionally draws
random_mass == 0 (probability ~1/2^32 per CA event). With random_mass
of zero, fenwick_find returns the first non-zero hull and
remaining_mass = cum_sum - 0 lands at exactly x_hull->count. The walk
loop with predicate `remaining_mass >= 0` then over-iterates by one
match, runs off the head of the AVL tree, and trips the
tsk_bug_assert at line 7694.
Clamp remaining_mass strictly below x_hull->count so the loop
terminates after exactly `count` matches. The fix is local; it does
not change the RNG draw count or pattern, so seeded simulations that
did not previously trip the bug produce identical trajectories.
Add a regression test (test_smc_k_bug_repro) that replays the
user-reported failure (50 diploid samples, seq_length 2e7, recomb
1e-8, demography change at t=30, seed 3940783591). Pre-fix the test
aborts after ~74821 events; post-fix it completes in about a second.1 parent 1b14a8a commit 36ca5db
2 files changed
Lines changed: 69 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7688 | 7688 | | |
7689 | 7689 | | |
7690 | 7690 | | |
| 7691 | + | |
| 7692 | + | |
| 7693 | + | |
| 7694 | + | |
| 7695 | + | |
| 7696 | + | |
| 7697 | + | |
| 7698 | + | |
| 7699 | + | |
| 7700 | + | |
| 7701 | + | |
| 7702 | + | |
7691 | 7703 | | |
7692 | 7704 | | |
7693 | 7705 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4656 | 4656 | | |
4657 | 4657 | | |
4658 | 4658 | | |
| 4659 | + | |
| 4660 | + | |
| 4661 | + | |
| 4662 | + | |
| 4663 | + | |
| 4664 | + | |
| 4665 | + | |
| 4666 | + | |
| 4667 | + | |
| 4668 | + | |
| 4669 | + | |
| 4670 | + | |
| 4671 | + | |
| 4672 | + | |
| 4673 | + | |
| 4674 | + | |
| 4675 | + | |
| 4676 | + | |
| 4677 | + | |
| 4678 | + | |
| 4679 | + | |
| 4680 | + | |
| 4681 | + | |
| 4682 | + | |
| 4683 | + | |
| 4684 | + | |
| 4685 | + | |
| 4686 | + | |
| 4687 | + | |
| 4688 | + | |
| 4689 | + | |
| 4690 | + | |
| 4691 | + | |
| 4692 | + | |
| 4693 | + | |
| 4694 | + | |
| 4695 | + | |
| 4696 | + | |
| 4697 | + | |
| 4698 | + | |
| 4699 | + | |
| 4700 | + | |
| 4701 | + | |
| 4702 | + | |
| 4703 | + | |
| 4704 | + | |
| 4705 | + | |
| 4706 | + | |
| 4707 | + | |
| 4708 | + | |
| 4709 | + | |
| 4710 | + | |
| 4711 | + | |
| 4712 | + | |
| 4713 | + | |
| 4714 | + | |
4659 | 4715 | | |
4660 | 4716 | | |
4661 | 4717 | | |
| |||
4771 | 4827 | | |
4772 | 4828 | | |
4773 | 4829 | | |
| 4830 | + | |
4774 | 4831 | | |
4775 | 4832 | | |
4776 | 4833 | | |
| |||
0 commit comments