-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(lib/babe): fix timing for transition between epochs #1636
Changes from 1 commit
e92668d
8a67205
f893b82
a919188
c23fee5
d23f2be
52d0f73
b6c88b1
578ff9c
6177b10
13cf25c
0b7e8f5
e1ea906
18460c8
af33101
d2f38b2
27dc09f
7a5ce9e
749da75
1058a9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,6 @@ var logger log.Logger | |
type Service struct { | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
paused bool | ||
authority bool | ||
dev bool | ||
|
||
|
@@ -228,13 +227,7 @@ func (b *Service) EpochLength() uint64 { | |
func (b *Service) Pause() error { | ||
b.Lock() | ||
defer b.Unlock() | ||
|
||
if b.paused { | ||
return errors.New("service already paused") | ||
} | ||
|
||
b.paused = true | ||
b.pause <- struct{}{} | ||
close(b.pause) | ||
return nil | ||
} | ||
|
||
|
@@ -243,22 +236,33 @@ func (b *Service) Resume() error { | |
b.Lock() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Acquire lock after checking There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this function should be atomic in the case that |
||
defer b.Unlock() | ||
|
||
if !b.paused { | ||
if !b.IsPaused() { | ||
return nil | ||
} | ||
|
||
b.pause = make(chan struct{}) | ||
|
||
epoch, err := b.epochState.GetCurrentEpoch() | ||
if err != nil { | ||
logger.Error("failed to get current epoch", "error", err) | ||
return err | ||
} | ||
|
||
b.paused = false | ||
go b.initiate(epoch) | ||
logger.Info("service resumed", "epoch", epoch) | ||
return nil | ||
} | ||
|
||
// IsPaused returns if the service is paused or not (ie. producing blocks) | ||
func (b *Service) IsPaused() bool { | ||
select { | ||
case <-b.pause: | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
// Stop stops the service. If stop is called, it cannot be resumed. | ||
func (b *Service) Stop() error { | ||
b.Lock() | ||
|
@@ -301,13 +305,6 @@ func (b *Service) IsStopped() bool { | |
return b.ctx.Err() != nil | ||
} | ||
|
||
// IsPaused returns if the service is paused or not (ie. producing blocks) | ||
func (b *Service) IsPaused() bool { | ||
b.RLock() | ||
defer b.RUnlock() | ||
return b.paused | ||
} | ||
|
||
func (b *Service) safeSend(msg types.Block) error { | ||
b.Lock() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to take lock here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for your first comment, the lock is here so there isn't a send on a closed channel. maybe there is a better way to implement that? either way, I think I will be removing this section anyways and doing a refactor |
||
defer b.Unlock() | ||
|
@@ -413,9 +410,6 @@ func (b *Service) invokeBlockAuthoring(epoch uint64) error { | |
// resume it when ready | ||
if b.epochLength <= intoEpoch && !b.dev { | ||
logger.Debug("pausing BABE, need to sync", "slots into epoch", intoEpoch, "startSlot", startSlot, "epochStart", epochStart) | ||
go func() { | ||
<-b.pause | ||
}() | ||
return b.Pause() | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The lock is not required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there might be a chance that
Pause()
is called when theb.pause
channel is already close, thus causing a close of closed channel panic