diff --git a/autopilot/contractor/contractor.go b/autopilot/contractor/contractor.go index 4e6f563fd..c073f59f3 100644 --- a/autopilot/contractor/contractor.go +++ b/autopilot/contractor/contractor.go @@ -314,9 +314,15 @@ func performContractMaintenance(ctx *mCtx, alerter alerts.Alerter, bus Bus, chur // stats for later logging var formed, refreshed, renewed int + // helper to add contracts to the set of contracts we keep for the new set + var filteredContracts []api.ContractMetadata + keepContract := func(c api.ContractMetadata, h api.Host) { + filteredContracts = append(filteredContracts, c) + ipFilter.Add(h) + } + // STEP 2: perform contract maintenance dropOutReasons := make(map[types.FileContractID]string) - var filteredContracts []api.ContractMetadata if err := func() error { // fetch all contracts we already have logger.Info("fetching existing contracts") @@ -442,12 +448,13 @@ func performContractMaintenance(ctx *mCtx, alerter alerts.Alerter, bus Bus, chur // check if revision is available if c.Revision == nil { - if !inSet || remainingLeeway == 0 { + if inSet && remainingLeeway > 0 { + logger.Debug("keeping contract due to leeway") + keepContract(c.ContractMetadata, host) + remainingLeeway-- + } else { logger.Debug("ignoring contract without revision") dropOutReasons[c.ID] = errContractNoRevision.Error() - } else if ctx.ShouldFilterRedundantIPs() && ipFilter.HasRedundantIP(host) { - logger.Debug("keeping contract due to leeway") - filteredContracts = append(filteredContracts, c.ContractMetadata) } continue // no more checks without revision } @@ -529,8 +536,7 @@ func performContractMaintenance(ctx *mCtx, alerter alerts.Alerter, bus Bus, chur // we keep the contract, add the host to the filter logger.Debug("contract is usable and is added / stays in set") - ipFilter.Add(host) - filteredContracts = append(filteredContracts, contract) + keepContract(contract, host) } return nil }(); err != nil { @@ -656,8 +662,7 @@ func performContractMaintenance(ctx *mCtx, alerter alerts.Alerter, bus Bus, chur } // add new contract and host - filteredContracts = append(filteredContracts, formedContract) - ipFilter.Add(candidate.host) + keepContract(formedContract, candidate.host) formed++ } diff --git a/stores/sql/main.go b/stores/sql/main.go index 7238c5439..041332ead 100644 --- a/stores/sql/main.go +++ b/stores/sql/main.go @@ -332,6 +332,7 @@ func ContractSizes(ctx context.Context, tx sql.Tx) (map[types.FileContractID]api if err != nil { return nil, fmt.Errorf("failed to fetch contract sizes: %w", err) } + defer rows.Close() sizes := make(map[types.FileContractID]api.ContractSize) for rows.Next() { @@ -2211,6 +2212,7 @@ func Settings(ctx context.Context, tx sql.Tx) ([]string, error) { if err != nil { return nil, fmt.Errorf("failed to query settings: %w", err) } + defer rows.Close() var settings []string for rows.Next() { var setting string