From 11602d630e3d1c8e5f3c9d34b25d270dca6d925f Mon Sep 17 00:00:00 2001 From: Simeon Simeonov Date: Sat, 29 Sep 2012 19:30:13 -0400 Subject: [PATCH] closes cursor even in the presence of exceptions --- lib/mongo/collection.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index 0b299a8578..492eb93df1 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -260,8 +260,11 @@ def find(selector={}, opts={}) }) if block_given? - yield cursor - cursor.close + begin + yield cursor + ensure + cursor.close + end nil else cursor @@ -594,7 +597,7 @@ def find_and_modify(opts={}) @db.command(cmd)['value'] end - + # Perform an aggregation using the aggregation framework on the current collection. # @note Aggregate requires server version >= 2.1.1 # @note Field References: Within an expression, field names must be quoted and prefixed by a dollar sign ($). @@ -636,10 +639,10 @@ def aggregate(pipeline=nil) unless Mongo::Support.ok?(result) raise Mongo::OperationFailure, "aggregate failed: #{result['errmsg']}" end - + return result["result"] end - + # Perform a map-reduce operation on the current collection. # # @param [String, BSON::Code] map a map function, written in JavaScript.