Skip to content

Instantly share code, notes, and snippets.

@ritxi
Last active September 25, 2019 06:19
Show Gist options
  • Save ritxi/42f8c2f2317f566ad1a99e8b8a20c569 to your computer and use it in GitHub Desktop.
Save ritxi/42f8c2f2317f566ad1a99e8b8a20c569 to your computer and use it in GitHub Desktop.
Cursors not being closed after iteration
require 'byebug'
require 'mongo'
client = Mongo::Client.new(['127.0.0.1:27017'])
11.times { client['test'].insert_one(a: rand) }
puts "noTimeout cursors before iteration: #{client.command('serverStatus': 1).first['metrics']['cursor']['open']['noTimeout']}"
query = client['test'].find.batch_size(10).no_cursor_timeout
query.each { next }
puts "noTimeout cursors after iteration: #{client.command('serverStatus': 1).first['metrics']['cursor']['open']['noTimeout']}"
puts 'gc'
GC.start
sleep 10
puts 'done'
puts "noTimeout cursors after waiting: #{client.command('serverStatus': 1).first['metrics']['cursor']['open']['noTimeout']}"
D, [2019-09-25T08:11:47.351640 #99034] DEBUG -- : MONGODB | 127.0.0.1:27017 | admin.insert | STARTED | {"insert"=>"test", "documents"=>[{"a"=>0.8066219628621961, "_id"=>BSON::ObjectId('5d8b05239bc38582daab80a7')}], "ordered"=>true}
D, [2019-09-25T08:11:47.351912 #99034] DEBUG -- : MONGODB | 127.0.0.1:27017 | admin.insert | SUCCEEDED | 0.000s
D, [2019-09-25T08:11:47.352277 #99034] DEBUG -- : MONGODB | 127.0.0.1:27017 | admin.serverStatus | STARTED | {"serverStatus"=>1}
D, [2019-09-25T08:11:47.353111 #99034] DEBUG -- : MONGODB | 127.0.0.1:27017 | admin.serverStatus | SUCCEEDED | 0.001s
noTimeout cursors before iteration: 0
D, [2019-09-25T08:11:47.353758 #99034] DEBUG -- : MONGODB | 127.0.0.1:27017 | admin.find | STARTED | {"find"=>"test", "filter"=>{}, "batchSize"=>10, "noCursorTimeout"=>true}
D, [2019-09-25T08:11:47.354139 #99034] DEBUG -- : MONGODB | 127.0.0.1:27017 | admin.find | SUCCEEDED | 0.000s
D, [2019-09-25T08:11:47.354506 #99034] DEBUG -- : MONGODB | 127.0.0.1:27017 | admin.getMore | STARTED | {"getMore"=>830930141683, "batchSize"=>10, "collection"=>"test"}
D, [2019-09-25T08:11:47.354731 #99034] DEBUG -- : MONGODB | 127.0.0.1:27017 | admin.getMore | SUCCEEDED | 0.000s
D, [2019-09-25T08:11:47.355097 #99034] DEBUG -- : MONGODB | 127.0.0.1:27017 | admin.serverStatus | STARTED | {"serverStatus"=>1}
D, [2019-09-25T08:11:47.355848 #99034] DEBUG -- : MONGODB | 127.0.0.1:27017 | admin.serverStatus | SUCCEEDED | 0.000s
noTimeout cursors after iteration: 1
gc
done
D, [2019-09-25T08:11:57.374360 #99034] DEBUG -- : MONGODB | 127.0.0.1:27017 | admin.serverStatus | STARTED | {"serverStatus"=>1}
D, [2019-09-25T08:11:57.375770 #99034] DEBUG -- : MONGODB | 127.0.0.1:27017 | admin.serverStatus | SUCCEEDED | 0.001s
noTimeout cursors after waiting: 1
D, [2019-09-25T08:16:01.625577 #99394] DEBUG -- : MONGODB | 127.0.0.1:27017 | admin.serverStatus | STARTED | {"serverStatus"=>1}
D, [2019-09-25T08:16:01.626710 #99394] DEBUG -- : MONGODB | 127.0.0.1:27017 | admin.serverStatus | SUCCEEDED | 0.001s
noTimeout cursors before iteration: 0
D, [2019-09-25T08:16:01.627382 #99394] DEBUG -- : MONGODB | 127.0.0.1:27017 | admin.find | STARTED | {"find"=>"test", "filter"=>{}, "batchSize"=>10, "noCursorTimeout"=>true}
D, [2019-09-25T08:16:01.627678 #99394] DEBUG -- : MONGODB | 127.0.0.1:27017 | admin.find | SUCCEEDED | 0.000s
D, [2019-09-25T08:16:01.628195 #99394] DEBUG -- : MONGODB | 127.0.0.1:27017 | admin.serverStatus | STARTED | {"serverStatus"=>1}
D, [2019-09-25T08:16:01.629242 #99394] DEBUG -- : MONGODB | 127.0.0.1:27017 | admin.serverStatus | SUCCEEDED | 0.001s
noTimeout cursors after iteration: 0
gc
done
D, [2019-09-25T08:16:11.648819 #99394] DEBUG -- : MONGODB | 127.0.0.1:27017 | admin.serverStatus | STARTED | {"serverStatus"=>1}
D, [2019-09-25T08:16:11.650690 #99394] DEBUG -- : MONGODB | 127.0.0.1:27017 | admin.serverStatus | SUCCEEDED | 0.001s
noTimeout cursors after waiting: 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment