mongo.find_one and mongo.command crash R session #11

Closed
ssimeonov opened this Issue Dec 18, 2012 · 28 comments

Comments

Projects
None yet
4 participants

Using the latest (v1.0.2) [rmongodb](https://github.com/gerald-lindsly/rmongodb) driver for MongoDB, the following code crashes the R session. I am using the 64-bit R version 2.15.2 on Mac OS X.

    library(rmongodb)
    mongo <- mongo.create('127.0.0.1', db='test')
    print(mongo.is.connected(mongo))
    mongo.find.one(mongo, 'unknown')

The output is:

    > library(rmongodb)
    rmongodb package (mongo-r-driver) loaded
    Use 'help("mongo")' to get started.

    > mongo <- mongo.create('127.0.0.1', db='test')
    > print(mongo.is.connected(mongo))
    [1] TRUE
    > mongo.find.one(mongo, 'unknown')
    R(60519) malloc: *** error for object 0x7fff5fbfce10: pointer being freed was not allocated
    *** set a breakpoint in malloc_error_break to debug
    Abort trap: 6

In this case the problem is the invalid collection name. However, passing a valid collection name does not solve the problem:

> library(rmongodb)
rmongodb package (mongo-r-driver) loaded
Use 'help("mongo")' to get started.

> mongo <- mongo.create('127.0.0.1', db='test')
> print(mongo.is.connected(mongo))
[1] TRUE
> mongo.find.one(mongo, 'test.my_collection')
R(64073) malloc: *** error for object 0x7fff5fbfce20: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6

Passing non-empty criteria also does not solve the problem:

> library(rmongodb)
rmongodb package (mongo-r-driver) loaded
Use 'help("mongo")' to get started.

> mongo <- mongo.create('127.0.0.1', db='test')
> print(mongo.is.connected(mongo))
[1] TRUE
> criteria <- mongo.bson.from.list(list(x=5))
> mongo.find.one(mongo, 'test.my_collection', criteria)
R(65109) malloc: *** error for object 0x7fff5fbfce20: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6

Switching to find makes the issue go away at the expense of extra code.

> library(rmongodb)
> mongo <- mongo.create('127.0.0.1', db='test')
> print(mongo.is.connected(mongo))
[1] TRUE
> # criteria <- mongo.bson.from.list(list(x=5))
> cursor <- mongo.find(mongo, 'test.my_collection')
> result <- list()
> while (mongo.cursor.next(cursor)) {
+  value <- mongo.cursor.value(cursor)
+  result[[length(result) + 1]] <- mongo.bson.to.list(value)
+ }
> mongo.cursor.destroy(cursor) 
[1] FALSE
> result
list()
> library(rmongodb)
> mongo <- mongo.create('127.0.0.1', db='test')
> print(mongo.is.connected(mongo))
[1] TRUE
> # criteria <- mongo.bson.from.list(list(x=5))
> cursor <- mongo.find(mongo, 'blah')
> result <- list()
> while (mongo.cursor.next(cursor)) {
+  value <- mongo.cursor.value(cursor)
+  result[[length(result) + 1]] <- mongo.bson.to.list(value)
+ }
> mongo.cursor.destroy(cursor) 
[1] FALSE
> result
list()

The same problem happens if a fake command is passed. This used to work under R 2.15.1 (it was part of my test suite).

The same type of crash happens if you pass a non-existent command to MongoDB. Again, this used to work under R 2.15.1.

    > library(rmongodb)
    > mongo <- mongo.create('127.0.0.1', db='test')
    > mongo.command(mongo, 'test', list(isMaster=1))
        ismaster : 8     true
        maxBsonObjectSize : 16   16777216
        ok : 1   1.000000
    > mongo.command(mongo, 'test', list(fake=1))
    R(66743) malloc: *** error for object 0x7fff5fbfde20: pointer being freed was not allocated
    *** set a breakpoint in malloc_error_break to debug
    Abort trap: 6
Owner

gerald-lindsly commented Dec 18, 2012

This had a bug fix about a month ago here on Github. CRAN is still not updated to the version found here.

What's the best way to load the package from GitHub?

Owner

gerald-lindsly commented Dec 18, 2012

There is a 'ZIP' button on the project page. This will download the
complete archive to you which must then be built with Visual Studio

On Tue, Dec 18, 2012 at 3:03 AM, Simeon Simeonov
notifications@github.comwrote:

What's the best way to load the package from GitHub?


Reply to this email directly or view it on GitHubhttps://github.com/gerald-lindsly/rmongodb/issues/11#issuecomment-11477117.

I'm on Mac OS X--no Visual Studio. :( Perhaps installing from source using devtools?

Alas, devtools can't do it.

> install_github(repo='rmongodb', username='gerald-lindsly')
Installing github repo(s) rmongodb/master from gerald-lindsly
Installing rmongodb.zip from https://api.github.com/repos/gerald-lindsly/rmongodb/zipball/master
Error: Does not appear to be an R package
Owner

gerald-lindsly commented Dec 18, 2012

Well, whatever you would use to build. I know nothing about Macs. There
is probably a build error under OS X. Was planning to address this in the
next major release (which will be a while still). I don't know what
they're doing to get the library on CRAN for Mac OS X.

On Tue, Dec 18, 2012 at 3:08 AM, Simeon Simeonov
notifications@github.comwrote:

I'm on Mac OS X--no Visual Studio. :( Perhaps installing from source using
devtools?


Reply to this email directly or view it on GitHubhttps://github.com/gerald-lindsly/rmongodb/issues/11#issuecomment-11477219.

Owner

gerald-lindsly commented Dec 18, 2012

oops, use R's package install as described in the ReadMe.md (but I think
the build problem will get in your way)

On Tue, Dec 18, 2012 at 3:11 AM, Gerald Lindsly gerald.lindsly@gmail.comwrote:

Well, whatever you would use to build. I know nothing about Macs. There
is probably a build error under OS X. Was planning to address this in the
next major release (which will be a while still). I don't know what
they're doing to get the library on CRAN for Mac OS X.

On Tue, Dec 18, 2012 at 3:08 AM, Simeon Simeonov <notifications@github.com

wrote:

I'm on Mac OS X--no Visual Studio. :( Perhaps installing from source
using devtools?


Reply to this email directly or view it on GitHubhttps://github.com/gerald-lindsly/rmongodb/issues/11#issuecomment-11477219.

Yes, you are right: I got a build error.

2087[SPX/r(ss_mongo_rep *)]$ r CMD INSTALL ~/Downloads/rmongodb-master/rmongodb.tar.gz 
* installing to library ‘/Library/Frameworks/R.framework/Versions/2.15/Resources/library’
* installing *source* package ‘rmongodb’ ...
** libs
*** arch - i386
make: *** No rule to make target `._api.o', needed by `rmongodb.so'.  Stop.
ERROR: compilation failed for package ‘rmongodb’
* removing ‘/Library/Frameworks/R.framework/Versions/2.15/Resources/library/rmongodb’
* restoring previous ‘/Library/Frameworks/R.framework/Versions/2.15/Resources/library/rmongodb’
Owner

gerald-lindsly commented Dec 18, 2012

Where did you get rmongodb.tar.gz? If you downloaded from GitHub, this
would not be in the right format to install directly. You'll need to
unpack it and the install should be done from under the rmongodb/ directory
(as unpacked from the archive).

rmongodb.tar.gz contains the master branch of this repo.

Owner

gerald-lindsly commented Dec 19, 2012

This GitHub archive is not itself an R package. The R package is contained
within the subfolder 'rmongodb'. The instructions in the ReadMe.md should
be used, but I am pretty sure there is still going to be build errors under
OS X.

On Tue, Dec 18, 2012 at 9:00 PM, Simeon Simeonov
notifications@github.comwrote:

rmongodb.tar.gz came from the master branch of this repo.

Right, my previous comment was incorrect: the .tar.gz file contains the rmongodb subfolder with DESCRIPTION etc.

Owner

gerald-lindsly commented Dec 19, 2012

The build command you used was on the GitHub archive, it looked like to me.
Unpack the archive and switch current directories to its root before
issuing 'R CMD INSTALL rmongodb'

On Wed, Dec 19, 2012 at 2:56 AM, Simeon Simeonov
notifications@github.comwrote:

Right, my previous comment was incorrect: the .tar.gz file contains the
rmongodb subfolder with DESCRIPTION etc.

Owner

gerald-lindsly commented Dec 19, 2012

Also, you'll need to unpack mongo-c-driver.zip to rmongodb/rmongodb/src as
described in the ReadMe.md also.

lefyer commented Dec 20, 2012

Tried what recommended on https://github.com/gerald-lindsly/rmongodb/blob/master/README.md. unpack mongo-c-driver-src.zip into rmongodb/rmongodb/src, then recheck and reintall the rmongodb, still got this error:

  • installing source package 'rmongodb' ...
    ** libs
    ERROR: compilation failed for package 'rmongodb'
  • removing 'C:/rmongodb-master/rmongodb.Rcheck/rmongodb'

image

lefyer commented Dec 21, 2012

Hi,

sorry for the previous problem. Seems I unzip the wrong rmongodb package. My installation is for Windows i386.

Today I install R on my windows i386 machine, then unzip the rmongodb windows package from "http://cran.r-project.org/web/packages/rmongodb/index.html".

I check the rmongodb install successfully:

library("rmongodb")
rmongodb package (mongo-r-driver) loaded
Use 'help("mongo")' to get started.

But when I tried to run the command in teachers_aid.R, code:
indexed = list(indexed=TRUE)
b <- mongo.find.one(mongo, admin, indexed)
if (is.null(b)) {
mongo.index.create(mongo, classes, "name")
mongo.index.create(mongo, students, "name")
mongo.index.create(mongo, class_students, "class_id")
mongo.index.create(mongo, class_students, "student_id")
mongo.index.create(mongo, tests, "class_id")
mongo.index.create(mongo, tests, "name")
mongo.index.create(mongo, test_scores, "test_id")
mongo.index.create(mongo, test_scores, "student_id")
mongo.insert(mongo, admin, indexed)
}

The error promted:
image

At this time, there is already a database created in MongoDB:
image

Could you please help? Is this problem the teacher_aid.R coding problem or something wrong with my installation?

Great thanks!
Feifei

Owner

gerald-lindsly commented Dec 21, 2012

Feifei, you are seeing the same bug this issue is about and which was fixed about a month ago. CRAN is not updated yet. Get the source code from GitHub: (shortcut to it here): http://github.com/gerald-lindsly/rmongodb/archive/master.zip

lefyer commented Dec 21, 2012

Hi Gerald, thanks for your reply. Is this the Package for Windows i386? After unpacking mongo-c-driver.zip to rmongodb-master/rmongodb/src, I tried to run command: "R CMD build rmongodb". Got this error:
image

Install also failed:
image

Pls help. Actually, I tried this package yesterday, always those errors occured. So this morning, I tried the CRAN zip package for windows. That package doesn't need to be install. All I needed to do was just copying that folder to R's library directory.

Best Regards
Feifei

Owner

gerald-lindsly commented Dec 21, 2012

This looks like incompatibilities with a more recent version of R. It may help if you posted the relevant portion of rmongodb.Rcheck/00install.out. I am working on an update, but there are problems between the current mongo-c-driver and rmongodb. I am working with 10gen, Inc. to resolve these issues.

Owner

gerald-lindsly commented Dec 23, 2012

rmongodb is updated to mongo-c-driver 0.7. Hopefully, this will resolve these issues. OS X still needs to be tested.

Owner

gerald-lindsly commented Dec 23, 2012

P.S. It may take a day or two to get distributed on the CRAN network

Collaborator

monkey101 commented Dec 23, 2012

I just tested on OSX (10.7.5). It builds clean and unit tests pass.

-Dan

On Sun, Dec 23, 2012 at 1:54 PM, Gerald Lindsly notifications@github.comwrote:

rmongodb is updated to mongo-c-driver 0.7. Hopefully, this will resolve
these issues. OS X still needs to be tested.


Reply to this email directly or view it on GitHubhttps://github.com/gerald-lindsly/rmongodb/issues/11#issuecomment-11649262.

lefyer commented Dec 24, 2012

contents of 00install.out:

  • installing source package 'rmongodb' ...
    ** libs
    ERROR: compilation failed for package 'rmongodb'
  • removing 'C:/rmongodb-master/rmongodb.Rcheck/rmongodb'

lefyer commented Dec 24, 2012

Hi Gerald, I tried the updated package this morning, seems the same problem is still exist. 00install.out msg was the same.
image

Is there any step error when install rmongodb? Should I build the downloaded 'ZIP' file with Visual Studio(as you mentioned in your past comment for 'ssimeonov'), not the "R CMD build rmongodb" command?

Maybe I will wait for your next release of rmongodb in CRAN. The CRAN's rmongodb_1.0.3.zip works on my Windows but had error.

Thanks
Feifei

Owner

gerald-lindsly commented Dec 24, 2012

Feifei, I have a feeling you don't have Rtools installed. See http://cran.r-project.org/doc/manuals/R-admin.html#The-Windows-toolset

lefyer commented Dec 25, 2012

Thanks!!! Gerald, you are right. I uninstalled the Rtool last week. Now the rmongodb works well.

Merry Christmas~~

Owner

gerald-lindsly commented Dec 25, 2012

Ok, great. The issues reported by the topic of this issue have been resolved then.

Merry Christmas to you too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment