Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add segfault tests #514

Merged
merged 2 commits into from
Oct 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions leveldown.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ LevelDOWN.prototype.getProperty = function (property) {
}

LevelDOWN.prototype._iterator = function (options) {
if (this.status !== 'open') {
// Prevent segfault
throw new Error('cannot call iterator() before open()')
}

return new Iterator(this, options)
}

Expand Down
43 changes: 43 additions & 0 deletions test/segfault-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const test = require('tape')
const testCommon = require('./common')

// Open issue: https://github.com/Level/leveldown/issues/157
test.skip('close() does not segfault if there is a pending write', function (t) {
t.plan(3)

const db = testCommon.factory()

db.open(function (err) {
t.ifError(err, 'no open error')

// The "sync" option seems to be a reliable way to trigger a segfault,
// but is not necessarily the cause of that segfault. More likely, it
// exposes a race condition that's already there.
db.put('foo', 'bar', { sync: true }, function (err) {
// We never get here, due to segfault.
t.ifError(err, 'no put error')
})

db.close(function (err) {
// We never get here, due to segfault.
t.ifError(err, 'no close error')
})
})
})

// See https://github.com/Level/leveldown/issues/134
test('iterator() does not segfault if db is not open', function (t) {
t.plan(2)

const db = testCommon.factory()

try {
db.iterator()
} catch (err) {
t.is(err.message, 'cannot call iterator() before open()')
}

db.close(function (err) {
t.ifError(err, 'no close error')
})
})