go-logging icon indicating copy to clipboard operation
go-logging copied to clipboard

Thread safety

Open yacovm opened this issue 9 years ago • 0 comments

Hi.

Logger.log() calls moduleLeveled.Log() which calls moduleLeveled.GetLevel(), which does a map read. Now, var defaultBackend LeveledBackend is a global field. If someone calls concurrently logging.SetLevel(), a map write takes place in moduleLeveled.SetLevel() which results in a panic of "fatal error: concurrent map read and map write".

Is it possible to protect the backend via a read-write lock?

Attached a stack trace that caused a panic:

15:12:00 runtime.throw(0xd52020, 0x21)

15:12:00 /opt/go/go1.6.linux.amd64/src/runtime/panic.go:530 +0x90 fp=0xc8204cbba8 sp=0xc8204cbb90

15:12:00 runtime.mapaccess2_faststr(0xa7da00, 0xc8201b8690, 0xca16c0, 0x3, 0x0, 0xe2c800000036)

15:12:00 /opt/go/go1.6.linux.amd64/src/runtime/hashmap_fast.go:307 +0x5b fp=0xc8204cbc08 sp=0xc8204cbba8

15:12:00 github.com/hyperledger/fabric/vendor/github.com/op/go-logging.(*moduleLeveled).Log(0xc82015eb80, 0x4, 0x2, 0xc82035ce00, 0x0, 0x0)

15:12:00 /w/workspace/fabric-verify-x86_64/gopath/src/github.com/hyperledger/fabric/vendor/github.com/op/go-logging/level.go:113 +0xa7 fp=0xc8204cbca0 sp=0xc8204cbc08

15:12:00 github.com/hyperledger/fabric/vendor/github.com/op/go-logging.(*Logger).log(0xc8201b87e0, 0x4, 0x0, 0xc82031e980, 0x1, 0x1)

15:12:00 /w/workspace/fabric-verify-x86_64/gopath/src/github.com/hyperledger/fabric/vendor/github.com/op/go-logging/logger.go:170 +0x1f3 fp=0xc8204cbd10 sp=0xc8204cbca0

15:12:00 github.com/hyperledger/fabric/vendor/github.com/op/go-logging.(*Logger).Info(0xc8201b87e0, 0xc82031e980, 0x1, 0x1)

This problem manifests when running multiple go tests concurrently, when launching multiple instances of objects that use your package at the same time.

Update: I opened a PR https://github.com/op/go-logging/pull/105

yacovm avatar Sep 28 '16 12:09 yacovm