rdotnet icon indicating copy to clipboard operation
rdotnet copied to clipboard

After call to REngine.GetInstance, app's own Ctrl+C handler no longer works

Open kierenj opened this issue 5 years ago • 6 comments

I'm using .NET Core - as stated, after I've called GetInstance, my Ctrl+C handler (added via Console.CancelKeyPress += ... is no longer called when I hit Ctrl+C

kierenj avatar Jun 19 '19 14:06 kierenj

@kierenj - is there a minimal set of code (or your repository) you can provide to reproduce the problem? Just want to make sure we're able to reproduce the problem exactly.

lrasmus avatar Jun 20 '19 01:06 lrasmus

No problem:

https://github.com/kierenj/rdotnet-ctrl-c-repro

master branch ignores the CancelKeyPressHandler working branch works fine - because the call to REngine.GetInstance is commented out

Let me know if I can assist/help any further

kierenj avatar Jun 20 '19 07:06 kierenj

I am by no means an expert, but this may be "working as designed". It appears to be more of the way embedded R works than R.NET (at least from what I can see). When the REngine is initialized, and it's in turn initializing R it starts up R's REPL: https://github.com/rdotnet/rdotnet/blob/master/R.NET/REngine.cs#L493

Which eventually gets to the REPL code which I believe is first grabbing your keyboard input and no longer passing it up to your handler: https://github.com/wch/r-source/blob/32eff2cd469115952a6f19eccc9c0b08c8d62822/src/main/main.c#L200-L295

The reason I'm thinking this may be as designed is that when I'm in the R console natively and I press Ctrl+C, it takes the input although it does no action with it.

I tried a few things to get around it. First, REngine's Initialize takes a character device as a parameter. If one isn't specified (default behavior), it creates one. I tried using the NullCharacterDevice explicitly, creating my own character device just to see what callbacks I could catch, and skipping over that initialization code within REngine. Unfortunately, none of those made a difference. I think it's because that callbacks happen way after R has grabbed the input.

lrasmus avatar Jun 20 '19 12:06 lrasmus

ah - it looks like this one - https://github.com/wch/r-source/blob/32eff2cd469115952a6f19eccc9c0b08c8d62822/src/main/main.c#L671 called by https://github.com/wch/r-source/blob/32eff2cd469115952a6f19eccc9c0b08c8d62822/src/main/main.c#L911

That's checking R_SignalHandlers which is hard-set here: https://github.com/wch/r-source/blob/32eff2cd469115952a6f19eccc9c0b08c8d62822/src/main/main.c#L708

I'm very new to this too... does that mean I'd need a custom build of R?

kierenj avatar Jun 20 '19 13:06 kierenj

@kierenj - sorry for not closing the loop on this before. Yes, I think it would mean a custom build of R. I've been trying to actually do this to verify it'll solve the problem, but have just had some problems getting R to build in my environment. I'm not sure if you had progressed with this, but curious to know where you stand with this issue.

lrasmus avatar Aug 05 '19 19:08 lrasmus

Our project for the moment has drawn to a close, and it remains as just an inconvenience on Windows. When deployed (docker/Linux), it responds to SIGTERM just fine. It's a bit of a pain but not the end of the world.

kierenj avatar Oct 04 '19 06:10 kierenj