Skip to content

Commit f7bb021

Browse files
committed
Fix cycle error
1 parent 0756af5 commit f7bb021

File tree

3 files changed

+102
-46
lines changed

3 files changed

+102
-46
lines changed

src/function/fetch.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,34 @@ where
180180
};
181181

182182
// Now that we've claimed the item, check again to see if there's a "hot" value.
183-
let opt_old_memo = self.get_memo_from_table_for(zalsa, id, memo_ingredient_index);
183+
let mut opt_old_memo = self.get_memo_from_table_for(zalsa, id, memo_ingredient_index);
184+
185+
// If this is a provisional memo from the same revision. Await all cycle heads because they could be
186+
// running on a different thread.
187+
if let Some(mut old_memo) = opt_old_memo {
188+
if old_memo.value.is_some()
189+
&& old_memo.may_be_provisional()
190+
&& old_memo.verified_at.load() == zalsa.current_revision()
191+
{
192+
opt_old_memo = loop {
193+
old_memo.await_heads(zalsa, self.database_key_index(id));
194+
195+
let new_old = self.get_memo_from_table_for(zalsa, id, memo_ingredient_index);
196+
match new_old {
197+
None => unreachable!("Expected memo to be present"),
198+
// If the new memo is the same as the old, then this means that this is still the "latest" memo for this
199+
Some(new_old) if std::ptr::eq(new_old, old_memo) => {
200+
break Some(new_old);
201+
}
202+
Some(new_old) => {
203+
tracing::debug!("Provisional memo has been updated by another thread while waiting for its cycle heads");
204+
old_memo = new_old;
205+
}
206+
}
207+
};
208+
}
209+
}
210+
184211
if let Some(old_memo) = opt_old_memo {
185212
if old_memo.value.is_some() {
186213
let mut cycle_heads = CycleHeads::default();

src/function/memo.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,35 @@ impl<V> Memo<V> {
200200
}
201201
}
202202

203+
#[inline(always)]
204+
pub(super) fn await_heads(&self, zalsa: &Zalsa, database_key_index: DatabaseKeyIndex) {
205+
for head in &self.revisions.cycle_heads {
206+
let head_index = head.database_key_index;
207+
208+
if database_key_index == head_index {
209+
continue;
210+
}
211+
212+
let ingredient = zalsa.lookup_ingredient(head_index.ingredient_index());
213+
let cycle_head_kind = ingredient.cycle_head_kind(zalsa, head_index.key_index());
214+
215+
if matches!(
216+
cycle_head_kind,
217+
CycleHeadKind::NotProvisional | CycleHeadKind::FallbackImmediate
218+
) {
219+
// This cycle is already finalized, so we don't need to wait on it;
220+
// keep looping through cycle heads.
221+
tracing::trace!("Dependent cycle head {head_index:?} has been finalized.");
222+
} else if ingredient.wait_for(zalsa, head_index.key_index()) {
223+
tracing::trace!("Dependent cycle head {head_index:?} has been released");
224+
} else {
225+
// We hit a cycle blocking on the cycle head; this means it's in
226+
// our own active query stack and we are responsible to resolve the
227+
// cycle
228+
}
229+
}
230+
}
231+
203232
/// Cycle heads that should be propagated to dependent queries.
204233
#[inline(always)]
205234
pub(super) fn cycle_heads(&self) -> &CycleHeads {

tests/parallel/cycle_nested_deep.rs

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -110,50 +110,50 @@ fn the_test() {
110110

111111
tracing::info!("Complete");
112112
}, // ,
113-
// "
114-
// 9102ac21f5a392c8f88cc0a27300000000004092244992244992244992244992244992244992
115-
// 2449922449922449922449922449922449922449922449922449922449922449922449922449
116-
// 9224499224499224491248c23664dbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66d
117-
// dbb66ddbb66d9324499224499224499224499224499224499224499224499224494a92244992
118-
// 2449922449922449922449922449922449922449922449922449922449922449922449922449
119-
// 9224499224499224499224499224499224499224499224499224499224499224499224499224
120-
// 499224499224499224499224499224499224499224c91629dbb66ddbb66d922449922449d9b6
121-
// 6ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddb364992942449922449924892
122-
// 244992244992244992244992244992244992b429dab66ddbb66d4b922449922449b46ddbb66d
123-
// dbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb6
124-
// 6ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddb
125-
// b66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddb2449922449922449922449922449922449
126-
// 922449922449922449d2b66ddbb66ddbb66ddbb66ddbb66ddbb66ddb962c4992244992244992
127-
// 2449922449922449b66ddbb64d49922449922449922449922449922449922449922449922449
128-
// 922449922449924892244992244992244992246ddbb624499224499224499224499224499224
129-
// 4992244992244992244992244992244992244992244992244992244992244992244992244992
130-
// 2449922449922449922449922449922449922449922449922449922449922449922449922449
131-
// 9224499224499224499224499224499224499224499224499224499224499224499224499224
132-
// 4992244992244992244992244992244992244992244992244992244992244992244992244992
133-
// 2449922449922449922449922449922449922449922449922449922449922449922449922449
134-
// 9224499224499224499224499224499224499224499224499224499224499224499224499224
135-
// 4992244992244992244992244992244992244992244992244992244992244992244992244992
136-
// 2449922449922449922449922449922449922449922449922449922449922449922449922449
137-
// 9224499224499224499224499224499224499224499224499224499224499224499224499224
138-
// 4992244992244992244992244992244992244992244992244992244992244992244992244992
139-
// 244992244992244992244992244992244992489124499224498a244992244992244992244992
140-
// 2449922449922449922449922449922449922449922449922449529224499224492249922449
141-
// 9224499224499224499224499224499224499224499224499224499224499224499224499224
142-
// 4992244992244992244992244992244992244992244992244992244992244992244992244992
143-
// 2449922449922449922449922449922449922449922449922449922449922449922449922449
144-
// 9224499224499224499224499224499224499224499224499224499224499224499224499224
145-
// 4992244992244992244992244992244992244992244992244992244992244992244992244992
146-
// 2449922449922449922449922449922449922449922449922449922449922449922449922449
147-
// 9224499224499224499224499224499224499224499224499224499224499224499224499224
148-
// 4992244992244992244992244992244992244992244992244992244992244992244992244992
149-
// 2449922449922449922449922449922449922449922449922449922449922449922449922449
150-
// 9224499224499224499224499224499224499224499224499224499224499224499224499224
151-
// 4992244992244992244992244992244992244992244992244992244992244992244992244992
152-
// 2449922449922449922449922449922449922449922449922449922449922449922449922449
153-
// 9224499224499224499224499224499224499224499224499224499224499224499224499224
154-
// 4992244992244992244992244992244992244992244992244992244992244992244992244992
155-
// 2449922449922449922449922449922449922449922449922449922449922449922449922449
156-
// 922449922449922449922449922449922409
157-
// "
113+
// "
114+
// 9102ac21f5a392c8f88cc0a27300000000004092244992244992244992244992244992244992
115+
// 2449922449922449922449922449922449922449922449922449922449922449922449922449
116+
// 9224499224499224491248c23664dbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66d
117+
// dbb66ddbb66d9324499224499224499224499224499224499224499224499224494a92244992
118+
// 2449922449922449922449922449922449922449922449922449922449922449922449922449
119+
// 9224499224499224499224499224499224499224499224499224499224499224499224499224
120+
// 499224499224499224499224499224499224499224c91629dbb66ddbb66d922449922449d9b6
121+
// 6ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddb364992942449922449924892
122+
// 244992244992244992244992244992244992b429dab66ddbb66d4b922449922449b46ddbb66d
123+
// dbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb6
124+
// 6ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddb
125+
// b66ddbb66ddbb66ddbb66ddbb66ddbb66ddbb66ddb2449922449922449922449922449922449
126+
// 922449922449922449d2b66ddbb66ddbb66ddbb66ddbb66ddbb66ddb962c4992244992244992
127+
// 2449922449922449b66ddbb64d49922449922449922449922449922449922449922449922449
128+
// 922449922449924892244992244992244992246ddbb624499224499224499224499224499224
129+
// 4992244992244992244992244992244992244992244992244992244992244992244992244992
130+
// 2449922449922449922449922449922449922449922449922449922449922449922449922449
131+
// 9224499224499224499224499224499224499224499224499224499224499224499224499224
132+
// 4992244992244992244992244992244992244992244992244992244992244992244992244992
133+
// 2449922449922449922449922449922449922449922449922449922449922449922449922449
134+
// 9224499224499224499224499224499224499224499224499224499224499224499224499224
135+
// 4992244992244992244992244992244992244992244992244992244992244992244992244992
136+
// 2449922449922449922449922449922449922449922449922449922449922449922449922449
137+
// 9224499224499224499224499224499224499224499224499224499224499224499224499224
138+
// 4992244992244992244992244992244992244992244992244992244992244992244992244992
139+
// 244992244992244992244992244992244992489124499224498a244992244992244992244992
140+
// 2449922449922449922449922449922449922449922449922449529224499224492249922449
141+
// 9224499224499224499224499224499224499224499224499224499224499224499224499224
142+
// 4992244992244992244992244992244992244992244992244992244992244992244992244992
143+
// 2449922449922449922449922449922449922449922449922449922449922449922449922449
144+
// 9224499224499224499224499224499224499224499224499224499224499224499224499224
145+
// 4992244992244992244992244992244992244992244992244992244992244992244992244992
146+
// 2449922449922449922449922449922449922449922449922449922449922449922449922449
147+
// 9224499224499224499224499224499224499224499224499224499224499224499224499224
148+
// 4992244992244992244992244992244992244992244992244992244992244992244992244992
149+
// 2449922449922449922449922449922449922449922449922449922449922449922449922449
150+
// 9224499224499224499224499224499224499224499224499224499224499224499224499224
151+
// 4992244992244992244992244992244992244992244992244992244992244992244992244992
152+
// 2449922449922449922449922449922449922449922449922449922449922449922449922449
153+
// 9224499224499224499224499224499224499224499224499224499224499224499224499224
154+
// 4992244992244992244992244992244992244992244992244992244992244992244992244992
155+
// 2449922449922449922449922449922449922449922449922449922449922449922449922449
156+
// 922449922449922449922449922449922409
157+
// "
158158
);
159159
}

0 commit comments

Comments
 (0)