Skip to content
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

Contract Lock Ctx #839

Merged
merged 3 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions autopilot/autopilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@ func (ap *Autopilot) Run() error {

// perform maintenance
setChanged, err := ap.c.performContractMaintenance(ctx, w)
if err != nil {
if err != nil && isErr(err, context.Canceled) {
return
} else if err != nil {
ap.logger.Errorf("contract maintenance failed, err: %v", err)
}
maintenanceSuccess := err == nil
Expand All @@ -288,8 +290,6 @@ func (ap *Autopilot) Run() error {
ap.logger.Debug("account refills loop launched")
go ap.a.refillWorkersAccountsLoop(ap.shutdownCtx)
})
} else {
ap.logger.Errorf("contract maintenance failed, err: %v", err)
}

// migration
Expand Down Expand Up @@ -376,7 +376,9 @@ func (ap *Autopilot) blockUntilConfigured(interrupt <-chan time.Time) (configure
cancel()

// if the config was not found, or we were unable to fetch it, keep blocking
if err != nil && strings.Contains(err.Error(), api.ErrAutopilotNotFound.Error()) {
if isErr(err, context.Canceled) {
return
} else if isErr(err, api.ErrAutopilotNotFound) {
once.Do(func() { ap.logger.Info("autopilot is waiting to be configured...") })
} else if err != nil {
ap.logger.Errorf("autopilot is unable to fetch its configuration from the bus, err: %v", err)
Expand Down Expand Up @@ -407,7 +409,9 @@ func (ap *Autopilot) blockUntilOnline() (online bool) {
online = len(peers) > 0
cancel()

if err != nil {
if isErr(err, context.Canceled) {
return
} else if err != nil {
ap.logger.Errorf("failed to get peers, err: %v", err)
} else if !online {
once.Do(func() { ap.logger.Info("autopilot is waiting on the bus to connect to peers...") })
Expand Down Expand Up @@ -439,7 +443,9 @@ func (ap *Autopilot) blockUntilSynced(interrupt <-chan time.Time) (synced, block
cancel()

// if an error occurred, or if we're not synced, we continue
if err != nil {
if isErr(err, context.Canceled) {
return
} else if err != nil {
ap.logger.Errorf("failed to get consensus state, err: %v", err)
} else if !synced {
once.Do(func() { ap.logger.Info("autopilot is waiting for consensus to sync...") })
Expand Down
6 changes: 3 additions & 3 deletions worker/contract_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ type contractLock struct {
stopWG sync.WaitGroup
}

func newContractLock(fcid types.FileContractID, lockID uint64, d time.Duration, locker ContractLocker, logger *zap.SugaredLogger) *contractLock {
ctx, cancel := context.WithCancel(context.Background())
func newContractLock(ctx context.Context, fcid types.FileContractID, lockID uint64, d time.Duration, locker ContractLocker, logger *zap.SugaredLogger) *contractLock {
ctx, cancel := context.WithCancel(ctx)
cl := &contractLock{
lockID: lockID,
fcid: fcid,
Expand All @@ -56,7 +56,7 @@ func (w *worker) acquireContractLock(ctx context.Context, fcid types.FileContrac
if err != nil {
return nil, err
}
return newContractLock(fcid, lockID, w.contractLockingDuration, w.bus, w.logger), nil
return newContractLock(w.shutdownCtx, fcid, lockID, w.contractLockingDuration, w.bus, w.logger), nil
}

func (w *worker) withContractLock(ctx context.Context, fcid types.FileContractID, priority int, fn func() error) error {
Expand Down
2 changes: 1 addition & 1 deletion worker/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (u *uploader) execute(req *sectorUploadReq) (types.Hash256, time.Duration,
}

// defer the release
lock := newContractLock(fcid, lockID, req.contractLockDuration, u.cl, u.logger)
lock := newContractLock(u.shutdownCtx, fcid, lockID, req.contractLockDuration, u.cl, u.logger)
defer func() {
ctx, cancel := context.WithTimeout(u.shutdownCtx, 10*time.Second)
lock.Release(ctx)
Expand Down