Skip to content

Commit 0ba9a6b

Browse files
committed
Merge remote-tracking branch 'origin/hm/chain-stream-yield-every-100-polls' into hm/refactor-cli-snapshot-cmd
2 parents 2e1bffd + 29bbd0c commit 0ba9a6b

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

src/ipld/util.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ pin_project! {
229229
events: bool,
230230
tipset_keys:bool,
231231
track_progress: bool,
232+
n_polled: usize,
232233
}
233234
}
234235

@@ -300,6 +301,7 @@ pub fn stream_chain<
300301
events: false,
301302
tipset_keys: false,
302303
track_progress: false,
304+
n_polled: 0,
303305
}
304306
}
305307

@@ -324,14 +326,23 @@ impl<DB: Blockstore, T: Borrow<Tipset>, ITER: Iterator<Item = T> + Unpin, S: Cid
324326
{
325327
type Item = anyhow::Result<CarBlock>;
326328

327-
fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> {
329+
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
328330
use Task::*;
329331

330332
let export_tipset_keys = self.tipset_keys;
331333
let fail_on_dead_links = self.fail_on_dead_links;
332334
let stateroot_limit_exclusive = self.stateroot_limit_exclusive;
333335
let this = self.project();
334336

337+
// Yield to the runtime every 128 polls to allow cancellation
338+
{
339+
*this.n_polled += 1;
340+
if this.n_polled.is_multiple_of(128) {
341+
cx.waker().wake_by_ref();
342+
return Poll::Pending;
343+
}
344+
}
345+
335346
loop {
336347
while let Some(task) = this.dfs.front_mut() {
337348
match task {

0 commit comments

Comments
 (0)