Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix connection leak when requested object file does not exist on disk #3

Merged
merged 1 commit into from
Sep 26, 2014

Conversation

jefferis
Copy link
Contributor

If an RDS format filehash is created on disk and an object that does not exist
is requested then there is a connection leak. If this process is repeated >124
times then R runs out of connections (which is of course bad news).

One safe approach appears to be:

  • first create connection
  • then open it for reading (potentially resulting in error condition)
  • always close it (destroying connection) even when erroring out

The problem can be demonstrated as follows:

dbCreate("mydbRDS", "RDS")
db <- dbInit("mydbRDS", "RDS")
dbInsert(db, "a", 1:10)
showConnections(all=T)
dbFetch("a")
showConnections(all=T)
dbFetch(db, "nonexistentkey")
showConnections(all=T)
dbUnlink(db)

And the exact origin can be seen in these two snippets

# Leaks a connection
showConnections(all=TRUE)
con <- tryCatch({
    gzfile(tempfile(),'rb')
}, condition = function(cond) {
    cond
})
showConnections(all=TRUE)

# No connection leak
showConnections(all=TRUE)
val <- tryCatch({
    con<-gzfile(tempfile())
    open(con,'rb')
    readLines(con)
}, condition = function(cond) {
    cond
}, finally = {
    close(con)
})
showConnections(all=TRUE)

* first create connection
* then open it for reading (potentially resulting in error condition)
* always close it (destroying connection) before erring out
@jefferis
Copy link
Contributor Author

Ping?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants