Skip to content

Commit 79fb031

Browse files
authored
fix: memlock throws error when lock exists (#200)
Fix for memlock to throw an error when trying to acquire already existing lock. License: MIT Signed-off-by: Adam Uhlir <[email protected]>
1 parent a8e5860 commit 79fb031

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

src/errors/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
'use strict'
22

3+
/**
4+
* Error raised when there is lock already in place when repo is being opened.
5+
*/
6+
class LockExistsError extends Error {
7+
constructor (message) {
8+
super(message)
9+
this.name = 'LockExistsError'
10+
this.code = 'ERR_LOCK_EXISTS'
11+
this.message = message
12+
}
13+
}
14+
15+
LockExistsError.code = 'ERR_LOCK_EXISTS'
16+
exports.LockExistsError = LockExistsError
17+
318
/**
419
* Error raised when requested item is not found.
520
*/

src/lock-memory.js

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const errors = require('./errors')
34
const debug = require('debug')
45

56
const log = debug('repo:lock')
@@ -17,6 +18,11 @@ const LOCKS = {}
1718
exports.lock = async (dir) => { // eslint-disable-line require-await
1819
const file = dir + '/' + lockFile
1920
log('locking %s', file)
21+
22+
if (LOCKS[file] === true) {
23+
throw new errors.LockExistsError(`Lock already being held for file: ${file}`)
24+
}
25+
2026
LOCKS[file] = true
2127
const closer = {
2228
async close () { // eslint-disable-line require-await

test/browser.js

+1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ describe('IPFS Repo Tests on the Browser', () => {
2323
require('./keystore-test')(repo)
2424
require('./config-test')(repo)
2525
require('./api-addr-test')(repo)
26+
require('./lock-test')(repo)
2627
})

test/lock-test.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,10 @@ module.exports = (repo) => {
4141
})
4242

4343
describe('lock-memory', () => {
44-
it('should lock a dir', async () => {
44+
it('should lock and unlock dir', async () => {
4545
const dir = '/foo/bar'
4646
expect(await lockMemory.locked(dir)).to.be.false()
4747

48-
await lockMemory.lock(dir)
49-
expect(await lockMemory.locked(dir)).to.be.true()
50-
})
51-
52-
it('should unlock a dir', async () => {
53-
const dir = '/foo/bar'
5448
const closer = await lockMemory.lock(dir)
5549
expect(await lockMemory.locked(dir)).to.be.true()
5650

0 commit comments

Comments
 (0)