Henrik Bengtsson
2004-Jun-10 23:03 UTC
[R] tryCatch() and preventing interrupts in 'finally'
With tryCatch() it is possible to catch interrupts with tryCatch(). Then you can use a 'finally' statement to clean up, release resources etc. However, how can I "protect" against additional interrupts? This is a concern when the hold down Ctrl+C and generates a sequence of interrupts. Example: tryCatch({ cat("Press Ctrl+C...\n"); Sys.sleep(5); }, interrupt = function(interrupt) { # ...and it will get caught cat("Caugh an interrupt:\n"); print(interrupt); }, finally = { cat("Trying to finalize. Press Ctrl+C...\n"); Sys.sleep(5); cat("...and this line will not be evaluated.\n"); }) Additional tryCatch() inside 'finally' will have the same problem and so. One idea I had was to wrap up the 'finally' statement in a withCallingHandlers(, interrupt=...), but it seems that withCallingHandlers() does not catch interrupts. For example, I tried to replace 'tryCatch' with 'withCallingHandlers' in the above code. So, what I basically need is a way to turn off interrupts during the evaluation of 'finally' or a way for it to restart at the last interrupted point. Is this possible? Thanks (again)! Henrik Bengtsson Dept. of Mathematical Statistics @ Centre for Mathematical Sciences Lund Institute of Technology/Lund University, Sweden (+2h UTC) +46 46 2229611 (off), +46 708 909208 (cell), +46 46 2224623 (fax) h b @ m a t h s . l t h . s e, http://www.maths.lth.se/~hb/
More complete support for interrupt management is still on the to do list, unfortunately. onexit actions have similar issues (and have for a long time). What I was hoping to do is provide a user level mechanism for turning off interrupts around a piece of code and then optionally enabling them within the code. It may make sense to adopt that behavior for tryCatch as the detault, and maybe also for functions with onexit actions. There is a risk of deadlock if the cleanup code goes into an infinite loop, but this may be better than the current behavior of allowing interrutps in cleanup code. There are some nasty little issues involved in getting this right, which is why it hasn't happened yet. Best, luke On Fri, 11 Jun 2004, Henrik Bengtsson wrote:> With tryCatch() it is possible to catch interrupts with tryCatch(). Then you > can use a 'finally' statement to clean up, release resources etc. However, > how can I "protect" against additional interrupts? This is a concern when > the hold down Ctrl+C and generates a sequence of interrupts. Example: > > tryCatch({ > cat("Press Ctrl+C...\n"); > Sys.sleep(5); > }, interrupt = function(interrupt) { > # ...and it will get caught > cat("Caugh an interrupt:\n"); > print(interrupt); > }, finally = { > cat("Trying to finalize. Press Ctrl+C...\n"); > Sys.sleep(5); > cat("...and this line will not be evaluated.\n"); > }) > > Additional tryCatch() inside 'finally' will have the same problem and so. > One idea I had was to wrap up the 'finally' statement in a > withCallingHandlers(, interrupt=...), but it seems that > withCallingHandlers() does not catch interrupts. For example, I tried to > replace 'tryCatch' with 'withCallingHandlers' in the above code. > > So, what I basically need is a way to turn off interrupts during the > evaluation of 'finally' or a way for it to restart at the last interrupted > point. Is this possible? > > Thanks (again)! > > Henrik Bengtsson > > Dept. of Mathematical Statistics @ Centre for Mathematical Sciences > Lund Institute of Technology/Lund University, Sweden (+2h UTC) > +46 46 2229611 (off), +46 708 909208 (cell), +46 46 2224623 (fax) > h b @ m a t h s . l t h . s e, http://www.maths.lth.se/~hb/ > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Luke Tierney University of Iowa Phone: 319-335-3386 Department of Statistics and Fax: 319-335-3017 Actuarial Science 241 Schaeffer Hall email: luke at stat.uiowa.edu Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu