Merge pull request #1864 from influxdb/fix-1180

fix #1180: race in startStateLoop
pull/1849/merge v0.9.0-rc9
dgnorton 2015-03-06 13:21:05 -05:00
commit 3454523bae
2 changed files with 10 additions and 9 deletions

View File

@ -3,6 +3,7 @@
### Bugfixes ### Bugfixes
- [#1867](https://github.com/influxdb/influxdb/pull/1867): Fix race accessing topic replicas map - [#1867](https://github.com/influxdb/influxdb/pull/1867): Fix race accessing topic replicas map
- [#1864](https://github.com/influxdb/influxdb/pull/1864): fix race in startStateLoop
## v0.9.0-rc8 [2015-03-05] ## v0.9.0-rc8 [2015-03-05]

View File

@ -14,7 +14,6 @@ import (
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"runtime/debug" "runtime/debug"
"sort" "sort"
"strconv" "strconv"
@ -629,20 +628,21 @@ func (l *Log) Leave() error {
// Returns once the state has transitioned to the initial state passed in. // Returns once the state has transitioned to the initial state passed in.
func (l *Log) startStateLoop(closing <-chan struct{}, state State) { func (l *Log) startStateLoop(closing <-chan struct{}, state State) {
l.wg.Add(1) l.wg.Add(1)
go l.stateLoop(closing, state) stateChanged := make(chan struct{})
go l.stateLoop(closing, state, stateChanged)
// Wait until state change. // Wait until state change.
for { <-stateChanged
if l.state == state {
break
}
runtime.Gosched()
}
} }
// stateLoop runs in a separate goroutine and runs the appropriate state loop. // stateLoop runs in a separate goroutine and runs the appropriate state loop.
func (l *Log) stateLoop(closing <-chan struct{}, state State) { func (l *Log) stateLoop(closing <-chan struct{}, state State, stateChanged chan struct{}) {
defer l.wg.Done() defer l.wg.Done()
l.Logger.Printf("log state change: %s => %s", l.state, state)
l.state = state
close(stateChanged)
for { for {
// Transition to new state. // Transition to new state.
l.Logger.Printf("log state change: %s => %s", l.state, state) l.Logger.Printf("log state change: %s => %s", l.state, state)