gerald-lindsly / rmongodb

R driver for MongoDB
http://cnub.org/rmongodb.ashx
Apache License 2.0
83 stars 44 forks source link

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

Closed ssimeonov closed 11 years ago

ssimeonov commented 11 years ago

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
gerald-lindsly commented 11 years ago

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

ssimeonov commented 11 years ago

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

gerald-lindsly commented 11 years ago

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.

ssimeonov commented 11 years ago

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

ssimeonov commented 11 years ago

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
gerald-lindsly commented 11 years ago

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.

gerald-lindsly commented 11 years ago

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.

ssimeonov commented 11 years ago

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’
ssimeonov commented 11 years ago

Turned to SO for help http://stackoverflow.com/questions/13929128/r-package-build-failure-on-mac-os-x

gerald-lindsly commented 11 years ago

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).

ssimeonov commented 11 years ago

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

gerald-lindsly commented 11 years ago

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.

ssimeonov commented 11 years ago

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

gerald-lindsly commented 11 years ago

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.

gerald-lindsly commented 11 years ago

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

lefyer commented 11 years ago

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:

image

lefyer commented 11 years ago

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

gerald-lindsly commented 11 years ago

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 11 years ago

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

gerald-lindsly commented 11 years ago

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.

gerald-lindsly commented 11 years ago

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

gerald-lindsly commented 11 years ago

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

monkey101 commented 11 years ago

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 11 years ago

contents of 00install.out:

lefyer commented 11 years ago

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

gerald-lindsly commented 11 years ago

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 11 years ago

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

Merry Christmas~~

gerald-lindsly commented 11 years ago

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

Merry Christmas to you too.