From b2dda08368793e5b01944eb134350cd928775c5e Mon Sep 17 00:00:00 2001 From: Whyrusleeping Date: Mon, 27 Apr 2020 15:54:41 -0700 Subject: [PATCH] respect shutdown signals when sleeping in miner code (#1617) --- miner/miner.go | 20 ++++++++++++++++---- node/impl/full/state.go | 2 +- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/miner/miner.go b/miner/miner.go index 594106b98..78abb2691 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -137,6 +137,15 @@ func (m *Miner) Unregister(ctx context.Context, addr address.Address) error { return nil } +func (m *Miner) niceSleep(d time.Duration) bool { + select { + case <-time.After(d): + return true + case <-m.stop: + return false + } +} + func (m *Miner) mine(ctx context.Context) { ctx, span := trace.StartSpan(ctx, "/mine") defer span.End() @@ -163,7 +172,7 @@ eventLoop: prebase, err := m.GetBestMiningCandidate(ctx) if err != nil { log.Errorf("failed to get best mining candidate: %s", err) - time.Sleep(time.Second * 5) + m.niceSleep(time.Second * 5) continue } @@ -181,7 +190,7 @@ eventLoop: } if base.TipSet.Equals(lastBase.TipSet) && lastBase.NullRounds == base.NullRounds { log.Warnf("BestMiningCandidate from the previous round: %s (nulls:%d)", lastBase.TipSet.Cids(), lastBase.NullRounds) - time.Sleep(build.BlockDelay * time.Second) + m.niceSleep(build.BlockDelay * time.Second) continue } lastBase = *base @@ -204,7 +213,10 @@ eventLoop: if len(blks) != 0 { btime := time.Unix(int64(blks[0].Header.Timestamp), 0) if time.Now().Before(btime) { - time.Sleep(time.Until(btime)) + if !m.niceSleep(time.Until(btime)) { + log.Warnf("received interrupt while waiting to broadcast block, will shutdown after block is sent out") + time.Sleep(time.Until(btime)) + } } else { log.Warnw("mined block in the past", "block-time", btime, "time", time.Now(), "duration", time.Since(btime)) @@ -214,7 +226,7 @@ eventLoop: for _, b := range blks { _, notOk := mWon[b.Header.Miner] if notOk { - log.Errorw("2 blocks for the same miner. Throwing hands in the air. Report this. It is important.", "bloks", blks) + log.Errorw("2 blocks for the same miner. Throwing hands in the air. Report this. It is important.", "blocks", blks) continue eventLoop } mWon[b.Header.Miner] = struct{}{} diff --git a/node/impl/full/state.go b/node/impl/full/state.go index 26ecb0523..1c8e6f29b 100644 --- a/node/impl/full/state.go +++ b/node/impl/full/state.go @@ -91,7 +91,7 @@ func (a *StateAPI) StateMinerDeadlines(ctx context.Context, m address.Address, t return stmgr.GetMinerDeadlines(ctx, a.StateManager, ts, m) } -func (a *StateAPI) StateMinerProvingDeadline(ctx context.Context, addr address.Address, tsk types.TipSetKey) (*miner.DeadlineInfo, error) { +func (a *StateAPI) StateMinerProvingDeadline(ctx context.Context, addr address.Address, tsk types.TipSetKey) (*miner.DeadlineInfo, error) { ts, err := a.Chain.GetTipSetFromKey(tsk) if err != nil { return nil, xerrors.Errorf("loading tipset %s: %w", tsk, err)