Skip to content

capnslog: fix small race on PackageLogger.level#48

Merged
mischief merged 1 commit intocoreos:masterfrom
mischief:capnslog-level-race
Feb 10, 2016
Merged

capnslog: fix small race on PackageLogger.level#48
mischief merged 1 commit intocoreos:masterfrom
mischief:capnslog-level-race

Conversation

@mischief
Copy link
Contributor

@mischief mischief commented Feb 5, 2016

WARNING: DATA RACE
Read by goroutine 46:
  github.com/coreos/pkg/capnslog.(*PackageLogger).internalLog()
      /home/mischief/code/go/src/github.com/coreos/pkg/capnslog/pkg_logger.go:30 +0x41
  github.com/coreos/pkg/capnslog.(*PackageLogger).Info()
      /home/mischief/code/go/src/github.com/coreos/pkg/capnslog/pkg_logger.go:131 +0x5a
...

Previous write by goroutine 44:
  github.com/coreos/pkg/capnslog.RepoLogger.setRepoLogLevelInternal()
      /home/mischief/code/go/src/github.com/coreos/pkg/capnslog/logmap.go:169 +0xba
  github.com/coreos/pkg/capnslog.SetGlobalLogLevel()
      /home/mischief/code/go/src/github.com/coreos/pkg/capnslog/logmap.go:136 +0x153
...

@vcaputo
Copy link
Contributor

vcaputo commented Feb 5, 2016

Reader-writer locks don't generally make sense where the critical section is trivially short like the single integer assignment being done here, since internally a rwmutex is still serializing the potentially concurrent read-lock acquisitions like a mutex would @ entry to the critical section internally either using atomics or a lock to manage the reader/writer counts. Basically the cost of maintaining the reader-writer mutex is comparable to the critical section itself, in this case. This situation is better served by an atomic, in lieu of going that route just use a simple mutex.

@mischief
Copy link
Contributor Author

mischief commented Feb 5, 2016

@vcaputo i think my vote currently would be plain mutex if that's preferred. LogLevel is a int8 type, and sync/atomic works on 32/64bit integers and i don't know if i can get it to cope with a int8. i'd have to change LogLevel's type, which seems like a poor idea, with consideration to all the users of this package.

@vcaputo
Copy link
Contributor

vcaputo commented Feb 6, 2016

sgtm!

@mischief mischief force-pushed the capnslog-level-race branch from 2d80889 to bc73737 Compare February 6, 2016 03:12
@mischief
Copy link
Contributor Author

mischief commented Feb 6, 2016

switched to sync.Mutex, ptal

if inLevel != CRITICAL && mylevel < inLevel {
return
}
logger.Lock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason not to just piggyback on logger.Lock() for serializing the accesses to PackageLogger.level?
I don't think we need to worry about lock contention here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line #46 below contains an unprotected access to the level member.

@mischief mischief force-pushed the capnslog-level-race branch from bc73737 to ba2a174 Compare February 9, 2016 23:01
@mischief
Copy link
Contributor Author

mischief commented Feb 9, 2016

ptal

cc @vcaputo @barakmich

@crawford
Copy link
Contributor

LGTM

mischief added a commit that referenced this pull request Feb 10, 2016
capnslog: fix small race on PackageLogger.level
@mischief mischief merged commit 549bd78 into coreos:master Feb 10, 2016
@mischief mischief deleted the capnslog-level-race branch February 10, 2016 00:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants