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

Improve object cleanup #145

Merged
merged 13 commits into from Jan 28, 2023
4 changes: 4 additions & 0 deletions NEWS.md
@@ -1,5 +1,9 @@
# pool (development version)

* If you `poolCheckout()` an object and forget to return it before it's deleted,
you now get a more informative message and the object is automatically
cleaned up (#95).

# pool 0.1.6

* `left_join()` and friends once again work with pool objects (#111).
Expand Down
4 changes: 3 additions & 1 deletion R/pool.R
Expand Up @@ -179,7 +179,9 @@ Pool <- R6::R6Class("Pool",
## detect leaked connections and destroy them
reg.finalizer(pool_metadata, function(e) {
if (pool_metadata$valid) {
warning("You have a leaked pooled object.")
cat("<pool> Checked-out object deleted before being returned.\n")
hadley marked this conversation as resolved.
Show resolved Hide resolved
cat("<pool> Make sure to `poolReturn()` all objects retrieved with `poolCheckout().`\n")
self$release(object)
}
}, onexit = TRUE)

Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/_snaps/release.md
@@ -0,0 +1,13 @@
# release: warns if object can't be returned

Code
pool <- poolCreate(function() 1)
obj <- poolCheckout(pool)
rm(obj)
. <- gc()
Output
<pool> Checked-out object deleted before being returned.
<pool> Make sure to `poolReturn()` all objects retrieved with `poolCheckout().`
Code
poolClose(pool)

11 changes: 11 additions & 0 deletions tests/testthat/test-release.R
Expand Up @@ -64,6 +64,17 @@ describe("release", {
expect_error(poolClose(pool),
"The pool was already closed.")
})

it("warns if object can't be returned", {
expect_snapshot({
pool <- poolCreate(function() 1)
obj <- poolCheckout(pool)
rm(obj)
. <- gc()
poolClose(pool)
})
})

})


Expand Down