jeroen / mongolite

Fast and Simple MongoDB Client for R
https://jeroen.github.io/mongolite/
286 stars 65 forks source link

Connection pooling broken #162

Closed MartinNowack closed 5 years ago

MartinNowack commented 5 years ago

I've been spending a couple of hours debugging mongolite as I have regularily crashes with mongolite. I think there are some issues with the weakptr-based implementation used for the pooling. This leads to the client being released prematurely although it is still used.

Here is a small example to reproduce the behaviour:

library(mongolite)
mongodb_url <- "mongodb+srv://readwrite:test@cluster0-84vdt.mongodb.net/test?ssl=true"
mongo_options = ssl_options(weak_cert_validation = T)
gctorture(TRUE)
get_pool_entry <-  function(name) {
  mongo(name, url = mongodb_url, options = mongo_options)
}

fooCount <- function() {
  con <-get_pool_entry("mtcars")
  con$count()
}

fooCount()
fooCount()

I get:

Error in mongo_get_default_database(client) : Client has been destroyed.
Calls: fooCount ... get_pool_entry -> mongo -> mongo_get_default_database -> .Call
Execution halted

I worked around this issue by always returning a new connection from the pool and not caching them. For now that works reliable.

jeroen commented 5 years ago

Thanks for this report. I'll try to look into this asap.

jeroen commented 5 years ago

Hmm I cannot reproduce this from your example. Does this still happen? Can you please give me your sessionInfo()?

MartinNowack commented 5 years ago

@jeroen Strange, here is my setup:

R version 3.2.3 (2015-12-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.5 LTS

locale:
 [1] LC_CTYPE=C                 LC_NUMERIC=C
 [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8
 [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base
jeroen commented 5 years ago

I have never been able to reproduce this issue. Note that you are using a very old version of R. The Garbage Collector has had numerous improvements since then.

Would it be possible for you to upgrade R and try again: the easiest way to upgrade R on Ubuntu is using the marutter ppa

sudo add-apt-repository ppa:marutter/rrutter
sudo apt-get update
sudo apt-get upgrade
MartinNowack commented 5 years ago

Ok, I updated to a newer version. I think the issue is gone.

Going to close it.

Thanks for your help!!