nsmithuk / local-kms

A mock version of AWS' Key Management Service, for local development and testing.
MIT License
198 stars 34 forks source link

Concurrency issues (resource temporarily unavailable) #13

Closed smecsia closed 4 years ago

smecsia commented 4 years ago

Currently it is impossible to use local-kms concurrently from multiple processes, because the underlying goleveldb library assumes database would be consumed from a single thread only. As a result the following error occurs when I run multiple nodes accessing local-kms in my test environment:

resource temporarily unavailable
goroutine 56 [running]:
net/http.(*conn).serve.func1(0xc0000b2b40)
    /usr/local/go/src/net/http/server.go:1769 +0x139
panic(0x8d5100, 0xdde600)
    /usr/local/go/src/runtime/panic.go:522 +0x1b5
github.com/nsmithuk/local-kms/src/data.NewDatabase(...)
    /go/src/github.com/nsmithuk/local-kms/src/data/database.go:19
github.com/nsmithuk/local-kms/src.getDatabase(0xc00002c6c0)
    /go/src/github.com/nsmithuk/local-kms/src/init.go:23 +0xb8
github.com/nsmithuk/local-kms/src.handleRequest(0xae6340, 0xc0001b22a0, 0xc000606300)
    /go/src/github.com/nsmithuk/local-kms/src/server.go:31 +0x14b
net/http.HandlerFunc.ServeHTTP(0x95fcc0, 0xae6340, 0xc0001b22a0, 0xc000606300)
    /usr/local/go/src/net/http/server.go:1995 +0x44
net/http.(*ServeMux).ServeHTTP(0xe18640, 0xae6340, 0xc0001b22a0, 0xc000606300)
    /usr/local/go/src/net/http/server.go:2375 +0x1d6
net/http.serverHandler.ServeHTTP(0xc000108000, 0xae6340, 0xc0001b22a0, 0xc000606300)
    /usr/local/go/src/net/http/server.go:2774 +0xa8
net/http.(*conn).serve(0xc0000b2b40, 0xae7580, 0xc0000f6300)
    /usr/local/go/src/net/http/server.go:1878 +0x851
created by net/http.(*Server).Serve
    /usr/local/go/src/net/http/server.go:2884 +0x2f4

Ideally we should either reuse leveldb reference or open it in read-only mode, which might be tricky though.

nsmithuk commented 4 years ago

Hi @smecsia. Thank you for reporting the issue. I'll have a fix out for this in the next day or so.

nsmithuk commented 4 years ago

@smecsia A fix has been pushed to master.

smecsia commented 4 years ago

Hi @nsmithuk I've tested latest Docker image multiple times and it works nicely! Thank you for a blazing-fast fix!