Skip to content
This repository has been archived by the owner on Dec 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #368 from peakji/master
Browse files Browse the repository at this point in the history
Close db with open iterator should not crash
  • Loading branch information
peakji authored Jun 8, 2017
2 parents 4a43d7f + 7b72938 commit 01be4e7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/iterator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,13 @@ bool Iterator::IteratorNext (std::vector<std::pair<std::string, std::string> >&

if (ok) {
result.push_back(std::make_pair(key, value));
size = size + key.size() + value.size();

if (!landed) {
landed = true;
return true;
}

size = size + key.size() + value.size();
if (size > highWaterMark)
return true;

Expand Down Expand Up @@ -329,6 +329,10 @@ NAN_METHOD(Iterator::Next) {

v8::Local<v8::Function> callback = info[0].As<v8::Function>();

if (iterator->ended) {
LD_RETURN_CALLBACK_OR_ERROR(callback, "iterator has ended");
}

NextWorker* worker = new NextWorker(
iterator
, new Nan::Callback(callback)
Expand Down
24 changes: 21 additions & 3 deletions test/iterator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ make('iterator optimized for seek', function (db, t, done) {
t.error(err, 'no error from next()')
t.equal(key.toString(), 'e', 'key matches')
t.ok(ite.cache.length > 0, "has cached items")
done();
ite.end(done)
})
})
})
Expand All @@ -139,8 +139,8 @@ make('iterator optimized for seek', function (db, t, done) {
make('iterator seek before next has completed', function (db, t, done) {
var ite = db.iterator()
ite.next(function (err, key, value) {
t.error(err, 'no error from end()')
done()
t.error(err, 'no error from next()')
ite.end(done)
})
var error
try {
Expand All @@ -152,6 +152,24 @@ make('iterator seek before next has completed', function (db, t, done) {
t.ok(error, 'had error from seek() before next() has completed')
})

make('close db with open iterator', function (db, t, done) {
var ite = db.iterator()
var cnt = 0
ite.next(function loop(err, key, value) {
if(cnt++ === 0)
t.error(err, 'no error from next()')
else
t.equal(err.message, 'iterator has ended')
if(key !== undefined)
ite.next(loop)
})

db.close(function (err){
t.error(err, 'no error from close()')
done(false)
})
})

make('iterator seek after end', function (db, t, done) {
var ite = db.iterator()
ite.next(function (err, key, value) {
Expand Down

0 comments on commit 01be4e7

Please sign in to comment.