Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

All the connections are in use: Execution halted

I'm using getYahooData() function in TTR package quite intensely.

I have this piece of code:

for(i in 1:nrow(symbol)){
    tryCatch(prices <- getYahooData(symbol$symbol[i], from, to, freq="daily", 
                                    type="price"), 
             warning=function(e) continue <- 0)
    if (continue==0) next
}

This loop is long I get this error:

Error in file(file, "rt") : all connections are in use Calls: tryCatch ... doTryCatch -> getYahooData -> getYahooData -> read.table -> file Execution halted

What can I do?

UPDATE:

If i use closeAllConnections() I get:

 I get: *** caught segfault *** address (nil), cause 'memory not mapped' Traceback: 1: getConnection(set[i]) 2: close(getConnection(set[i])) 3: closeAllConnections() aborting ... 
like image 480
Dail Avatar asked Aug 18 '11 06:08

Dail


2 Answers

First : do not ever in your life use that continue construct again. It's of no use. tryCatch() will continue if you defined a handler for an error or a warning. It will use that one instead of the "default" error=function(e) stop(e). This one will stop your function. If you define a handler (either warning= or error= ), your script will not be stopped, so that continue is not necessary.

This said : The correct use of tryCatch in this case would be :

for(i in 1:nrow(symbol)){

tryCatch(prices <- getYahooData(symbol$symbol[i], from, to, freq="daily",
                                    type="price"), error = function(e){})

}

or, if you use it in a script and want to go to the next loop when an error occurs, you can simply use :

for(i in 1:nrow(symbol)){

    prices <- try(getYahooData(symbol$symbol[i], from, to, freq="daily",
                                    type="price"), silent=TRUE)

    if(inherits(prices,"try-error")) { next } # only true if an error occurs
    ... # rest of calculations
}

If you would have used this way of tryCatch or try, you wouldn't have had the problems you report here.

Now I could reproduce your case, if I use nonexistent symbols. Your erroneous use of tryCatch() function is causing you trouble. read.table returns an error (Error in file(file, "rt") : cannot open the connection). This is an error, not a warning. You get an additional warning saying that a 404 File not found was returned.

When a warning is issued together with an error, the handler function for the warning is dealt with first. That's because a warning has to be thrown before the function can be stopped. So it won't handle the error you get, which means that the on.exit(close(file)) in read.table() will not to be called. Hence, the connection is not closed correctly and still considered open, although it can't be found anymore by R (showAllConnections() shows nothing). As the error is not dealt with, something goes wrong in the registration of the connections. As the connection couldn't be opened, the on.exit(close(...)) will have no effect. showConnections() doesn't show the connection, but somehow R still thinks it's there. Hence, all hell breaks loose and you crash your R.

thanks for the corrections to @Tommy

A simple code example to illustrate this :

myfun <- function(x){
   if(x>1) warning("aWarning")
   stop("aStop")
   x
}

tryCatch(myfun(0.5),
          warning=function(w)print("warning"),
          error=function(e) print("stop"))
[1] "stop"              

tryCatch(myfun(1.5),
          warning=function(w)print("warning"),
          error=function(e) print("stop"))
[1] "warning"

In summary :

  • check the symbols you use. They're probably wrong.
  • do not ever again use a warning handler if you expect errors.

And as an extra: your loop will only return the result of the last call, as you overwrite prices every time you go through the loop, in case you would have used correct symbols.

Edit : in case you want to continue the action

like image 170
Joris Meys Avatar answered Nov 19 '22 18:11

Joris Meys


Close some connections? Could be as easy as inserting closeAllConnections() at the end of that loop body.

like image 29
IRTFM Avatar answered Nov 19 '22 19:11

IRTFM