yutannihilation / savvy

A simple R extension interface using Rust
https://yutannihilation.github.io/savvy/guide/
MIT License
73 stars 4 forks source link

Document how to handle cancellation #314

Open yutannihilation opened 3 weeks ago

yutannihilation commented 3 weeks ago

cf.

yutannihilation commented 3 weeks ago

WRE suggests R_CheckUserInterrupt(), but it seems duckdb-r doesn't use it for some reason, which I don't figure out yet. But it seems it's just use csignal.

https://cran.rstudio.com/doc/manuals/r-devel/R-exts.html#Allowing-interrupts

yutannihilation commented 3 weeks ago

ctrlc crate looks handy.

https://rust-cli.github.io/book/in-depth/signals.html https://github.com/Detegr/rust-ctrlc

eitsupi commented 3 weeks ago

This is very useful for me.

The current polars package implements the following, which I believe is not ideal. https://github.com/eitsupi/neo-r-polars/issues/23#issuecomment-2426537409 https://github.com/pola-rs/r-polars/blob/a7b064265f56ece53358955979f929b2cc055c63/src/rust/src/utils/extendr_concurrent.rs#L234-L250

yutannihilation commented 3 weeks ago

Oh, the current implementation looks very hacky...

I thought we can learn from Python Polars' implementation, but it seems they have their own difficulties...

https://github.com/pola-rs/polars/issues/2687 https://pyo3.rs/v0.22.5/faq#ctrl-c-doesnt-do-anything-while-my-rust-code-is-executing

yutannihilation commented 3 weeks ago

I think this is the naive translation to Rust, but this doesn't work except for the first Ctrl+C...

https://github.com/yutannihilation/cancel.test.savvy/blob/main/src/rust/src/lib.rs

yutannihilation commented 3 weeks ago

Some more findings:

yutannihilation commented 3 weeks ago

Calling eval_parse_text("Sys.sleep(0)")? on every loop is a workaround.

Other than Sys.sleep, message() and warning() works. However, most functions don't seem to work.