mirror of
				https://github.com/optim-enterprises-bv/vault.git
				synced 2025-10-30 18:17:55 +00:00 
			
		
		
		
	VAULT-8436 remove <-time.After statements in for loops (#18818)
* replace time.After with ticker in loops * add semgrep rule * update to use timers * remove stop
This commit is contained in:
		| @@ -366,10 +366,12 @@ func (r *LifetimeWatcher) doRenewWithOptions(tokenMode bool, nonRenewable bool, | |||||||
| 			return nil | 			return nil | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|  | 		timer := time.NewTimer(sleepDuration) | ||||||
| 		select { | 		select { | ||||||
| 		case <-r.stopCh: | 		case <-r.stopCh: | ||||||
|  | 			timer.Stop() | ||||||
| 			return nil | 			return nil | ||||||
| 		case <-time.After(sleepDuration): | 		case <-timer.C: | ||||||
| 			continue | 			continue | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -267,10 +267,12 @@ func (b *backend) Login(ctx context.Context, req *logical.Request, username, pas | |||||||
| 					return nil, logical.ErrorResponse("okta auth backend unexpected failure"), nil, nil | 					return nil, logical.ErrorResponse("okta auth backend unexpected failure"), nil, nil | ||||||
| 				} | 				} | ||||||
|  |  | ||||||
|  | 				timer := time.NewTimer(1 * time.Second) | ||||||
| 				select { | 				select { | ||||||
| 				case <-time.After(1 * time.Second): | 				case <-timer.C: | ||||||
| 					// Continue | 					// Continue | ||||||
| 				case <-ctx.Done(): | 				case <-ctx.Done(): | ||||||
|  | 					timer.Stop() | ||||||
| 					return nil, logical.ErrorResponse("exiting pending mfa challenge"), nil, nil | 					return nil, logical.ErrorResponse("exiting pending mfa challenge"), nil, nil | ||||||
| 				} | 				} | ||||||
| 			case "REJECTED": | 			case "REJECTED": | ||||||
|   | |||||||
| @@ -61,10 +61,12 @@ func (h *CLIHandler) Auth(c *api.Client, m map[string]string) (*api.Secret, erro | |||||||
|  |  | ||||||
| 	go func() { | 	go func() { | ||||||
| 		for { | 		for { | ||||||
|  | 			timer := time.NewTimer(time.Second) | ||||||
| 			select { | 			select { | ||||||
| 			case <-doneCh: | 			case <-doneCh: | ||||||
|  | 				timer.Stop() | ||||||
| 				return | 				return | ||||||
| 			case <-time.After(time.Second): | 			case <-timer.C: | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			resp, _ := c.Logical().Read(fmt.Sprintf("auth/%s/verify/%s", mount, nonce)) | 			resp, _ := c.Logical().Read(fmt.Sprintf("auth/%s/verify/%s", mount, nonce)) | ||||||
|   | |||||||
| @@ -151,10 +151,12 @@ func (ss *SinkServer) Run(ctx context.Context, incoming chan string, sinks []*Si | |||||||
| 			if err := writeSink(st.sink, st.token); err != nil { | 			if err := writeSink(st.sink, st.token); err != nil { | ||||||
| 				backoff := 2*time.Second + time.Duration(ss.random.Int63()%int64(time.Second*2)-int64(time.Second)) | 				backoff := 2*time.Second + time.Duration(ss.random.Int63()%int64(time.Second*2)-int64(time.Second)) | ||||||
| 				ss.logger.Error("error returned by sink function, retrying", "error", err, "backoff", backoff.String()) | 				ss.logger.Error("error returned by sink function, retrying", "error", err, "backoff", backoff.String()) | ||||||
|  | 				timer := time.NewTimer(backoff) | ||||||
| 				select { | 				select { | ||||||
| 				case <-ctx.Done(): | 				case <-ctx.Done(): | ||||||
|  | 					timer.Stop() | ||||||
| 					return nil | 					return nil | ||||||
| 				case <-time.After(backoff): | 				case <-timer.C: | ||||||
| 					atomic.AddInt32(ss.remaining, 1) | 					atomic.AddInt32(ss.remaining, 1) | ||||||
| 					sinkCh <- st | 					sinkCh <- st | ||||||
| 				} | 				} | ||||||
|   | |||||||
| @@ -2319,9 +2319,11 @@ func (c *ServerCommand) storageMigrationActive(backend physical.Backend) bool { | |||||||
| 		} | 		} | ||||||
| 		c.logger.Warn("storage migration check error", "error", err.Error()) | 		c.logger.Warn("storage migration check error", "error", err.Error()) | ||||||
|  |  | ||||||
|  | 		timer := time.NewTimer(2 * time.Second) | ||||||
| 		select { | 		select { | ||||||
| 		case <-time.After(2 * time.Second): | 		case <-timer.C: | ||||||
| 		case <-c.ShutdownCh: | 		case <-c.ShutdownCh: | ||||||
|  | 			timer.Stop() | ||||||
| 			return true | 			return true | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| @@ -2609,10 +2611,12 @@ func runUnseal(c *ServerCommand, core *vault.Core, ctx context.Context) { | |||||||
| 		} | 		} | ||||||
| 		c.logger.Warn("failed to unseal core", "error", err) | 		c.logger.Warn("failed to unseal core", "error", err) | ||||||
|  |  | ||||||
|  | 		timer := time.NewTimer(5 * time.Second) | ||||||
| 		select { | 		select { | ||||||
| 		case <-c.ShutdownCh: | 		case <-c.ShutdownCh: | ||||||
|  | 			timer.Stop() | ||||||
| 			return | 			return | ||||||
| 		case <-time.After(5 * time.Second): | 		case <-timer.C: | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|   | |||||||
| @@ -642,8 +642,9 @@ func (i *ZooKeeperHALock) Unlock() error { | |||||||
| 					return | 					return | ||||||
| 				} | 				} | ||||||
|  |  | ||||||
|  | 				timer := time.NewTimer(time.Second) | ||||||
| 				select { | 				select { | ||||||
| 				case <-time.After(time.Second): | 				case <-timer.C: | ||||||
| 					attempts := attempts + 1 | 					attempts := attempts + 1 | ||||||
| 					if attempts >= 10 { | 					if attempts >= 10 { | ||||||
| 						i.logger.Error("release lock max attempts reached. Lock may not be released", "error", err) | 						i.logger.Error("release lock max attempts reached. Lock may not be released", "error", err) | ||||||
| @@ -651,6 +652,7 @@ func (i *ZooKeeperHALock) Unlock() error { | |||||||
| 					} | 					} | ||||||
| 					continue | 					continue | ||||||
| 				case <-i.stopCh: | 				case <-i.stopCh: | ||||||
|  | 					timer.Stop() | ||||||
| 					return | 					return | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
|   | |||||||
							
								
								
									
										17
									
								
								tools/semgrep/ci/loop-time-after.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								tools/semgrep/ci/loop-time-after.yml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | |||||||
|  | rules: | ||||||
|  |   - id: loop-time-after | ||||||
|  |     pattern: | | ||||||
|  |       for ... { | ||||||
|  |         ... | ||||||
|  |         select { | ||||||
|  |           case ... | ||||||
|  |           case <-time.After(...): | ||||||
|  |             ... | ||||||
|  |           case ... | ||||||
|  |         } | ||||||
|  |         ... | ||||||
|  |       } | ||||||
|  |     message: <-time.After() used in for loop, consider using a ticker or a timer instead | ||||||
|  |     languages: | ||||||
|  |       - go | ||||||
|  |     severity: WARNING | ||||||
							
								
								
									
										20
									
								
								vault/ha.go
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								vault/ha.go
									
									
									
									
									
								
							| @@ -663,10 +663,12 @@ func (c *Core) waitForLeadership(newLeaderCh chan func(), manualStepDownCh, stop | |||||||
| 			// Spawn this in a go routine so we can cancel the context and | 			// Spawn this in a go routine so we can cancel the context and | ||||||
| 			// unblock any inflight requests that are holding the statelock. | 			// unblock any inflight requests that are holding the statelock. | ||||||
| 			go func() { | 			go func() { | ||||||
|  | 				timer := time.NewTimer(DefaultMaxRequestDuration) | ||||||
| 				select { | 				select { | ||||||
| 				case <-activeCtx.Done(): | 				case <-activeCtx.Done(): | ||||||
|  | 					timer.Stop() | ||||||
| 					// Attempt to drain any inflight requests | 					// Attempt to drain any inflight requests | ||||||
| 				case <-time.After(DefaultMaxRequestDuration): | 				case <-timer.C: | ||||||
| 					activeCtxCancel() | 					activeCtxCancel() | ||||||
| 				} | 				} | ||||||
| 			}() | 			}() | ||||||
| @@ -796,8 +798,9 @@ func (c *Core) periodicLeaderRefresh(newLeaderCh chan func(), stopCh chan struct | |||||||
|  |  | ||||||
| 	clusterAddr := "" | 	clusterAddr := "" | ||||||
| 	for { | 	for { | ||||||
|  | 		timer := time.NewTimer(leaderCheckInterval) | ||||||
| 		select { | 		select { | ||||||
| 		case <-time.After(leaderCheckInterval): | 		case <-timer.C: | ||||||
| 			count := atomic.AddInt32(opCount, 1) | 			count := atomic.AddInt32(opCount, 1) | ||||||
| 			if count > 1 { | 			if count > 1 { | ||||||
| 				atomic.AddInt32(opCount, -1) | 				atomic.AddInt32(opCount, -1) | ||||||
| @@ -830,6 +833,7 @@ func (c *Core) periodicLeaderRefresh(newLeaderCh chan func(), stopCh chan struct | |||||||
| 				atomic.AddInt32(lopCount, -1) | 				atomic.AddInt32(lopCount, -1) | ||||||
| 			}() | 			}() | ||||||
| 		case <-stopCh: | 		case <-stopCh: | ||||||
|  | 			timer.Stop() | ||||||
| 			return | 			return | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| @@ -842,8 +846,9 @@ func (c *Core) periodicCheckKeyUpgrades(ctx context.Context, stopCh chan struct{ | |||||||
|  |  | ||||||
| 	opCount := new(int32) | 	opCount := new(int32) | ||||||
| 	for { | 	for { | ||||||
|  | 		timer := time.NewTimer(keyRotateCheckInterval) | ||||||
| 		select { | 		select { | ||||||
| 		case <-time.After(keyRotateCheckInterval): | 		case <-timer.C: | ||||||
| 			count := atomic.AddInt32(opCount, 1) | 			count := atomic.AddInt32(opCount, 1) | ||||||
| 			if count > 1 { | 			if count > 1 { | ||||||
| 				atomic.AddInt32(opCount, -1) | 				atomic.AddInt32(opCount, -1) | ||||||
| @@ -899,6 +904,7 @@ func (c *Core) periodicCheckKeyUpgrades(ctx context.Context, stopCh chan struct{ | |||||||
| 				return | 				return | ||||||
| 			}() | 			}() | ||||||
| 		case <-stopCh: | 		case <-stopCh: | ||||||
|  | 			timer.Stop() | ||||||
| 			return | 			return | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| @@ -1030,9 +1036,11 @@ func (c *Core) acquireLock(lock physical.Lock, stopCh <-chan struct{}) <-chan st | |||||||
|  |  | ||||||
| 		// Retry the acquisition | 		// Retry the acquisition | ||||||
| 		c.logger.Error("failed to acquire lock", "error", err) | 		c.logger.Error("failed to acquire lock", "error", err) | ||||||
|  | 		timer := time.NewTimer(lockRetryInterval) | ||||||
| 		select { | 		select { | ||||||
| 		case <-time.After(lockRetryInterval): | 		case <-timer.C: | ||||||
| 		case <-stopCh: | 		case <-stopCh: | ||||||
|  | 			timer.Stop() | ||||||
| 			return nil | 			return nil | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| @@ -1099,13 +1107,15 @@ func (c *Core) cleanLeaderPrefix(ctx context.Context, uuid string, leaderLostCh | |||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	for len(keys) > 0 { | 	for len(keys) > 0 { | ||||||
|  | 		timer := time.NewTimer(leaderPrefixCleanDelay) | ||||||
| 		select { | 		select { | ||||||
| 		case <-time.After(leaderPrefixCleanDelay): | 		case <-timer.C: | ||||||
| 			if keys[0] != uuid { | 			if keys[0] != uuid { | ||||||
| 				c.barrier.Delete(ctx, coreLeaderPrefix+keys[0]) | 				c.barrier.Delete(ctx, coreLeaderPrefix+keys[0]) | ||||||
| 			} | 			} | ||||||
| 			keys = keys[1:] | 			keys = keys[1:] | ||||||
| 		case <-leaderLostCh: | 		case <-leaderLostCh: | ||||||
|  | 			timer.Stop() | ||||||
| 			return | 			return | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -1995,11 +1995,13 @@ func (c *Core) validateDuo(ctx context.Context, mfaFactors *MFAFactor, mConfig * | |||||||
| 		case "allow": | 		case "allow": | ||||||
| 			return nil | 			return nil | ||||||
| 		} | 		} | ||||||
|  | 		timer := time.NewTimer(time.Second) | ||||||
|  |  | ||||||
| 		select { | 		select { | ||||||
| 		case <-ctx.Done(): | 		case <-ctx.Done(): | ||||||
|  | 			timer.Stop() | ||||||
| 			return fmt.Errorf("duo push verification operation canceled") | 			return fmt.Errorf("duo push verification operation canceled") | ||||||
| 		case <-time.After(time.Second): | 		case <-timer.C: | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| @@ -2124,11 +2126,13 @@ func (c *Core) validateOkta(ctx context.Context, mConfig *mfa.Config, username s | |||||||
| 		default: | 		default: | ||||||
| 			return fmt.Errorf("unknown status code") | 			return fmt.Errorf("unknown status code") | ||||||
| 		} | 		} | ||||||
|  | 		timer := time.NewTimer(time.Second) | ||||||
|  |  | ||||||
| 		select { | 		select { | ||||||
| 		case <-ctx.Done(): | 		case <-ctx.Done(): | ||||||
|  | 			timer.Stop() | ||||||
| 			return fmt.Errorf("push verification operation canceled") | 			return fmt.Errorf("push verification operation canceled") | ||||||
| 		case <-time.After(time.Second): | 		case <-timer.C: | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|   | |||||||
| @@ -433,8 +433,9 @@ func (c *Core) raftTLSRotateDirect(ctx context.Context, logger hclog.Logger, sto | |||||||
| 				backoff = false | 				backoff = false | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
|  | 			timer := time.NewTimer(time.Until(nextRotationTime)) | ||||||
| 			select { | 			select { | ||||||
| 			case <-time.After(time.Until(nextRotationTime)): | 			case <-timer.C: | ||||||
| 				// It's time to rotate the keys | 				// It's time to rotate the keys | ||||||
| 				next, err := rotateKeyring() | 				next, err := rotateKeyring() | ||||||
| 				if err != nil { | 				if err != nil { | ||||||
| @@ -446,6 +447,7 @@ func (c *Core) raftTLSRotateDirect(ctx context.Context, logger hclog.Logger, sto | |||||||
| 				nextRotationTime = next | 				nextRotationTime = next | ||||||
|  |  | ||||||
| 			case <-stopCh: | 			case <-stopCh: | ||||||
|  | 				timer.Stop() | ||||||
| 				return | 				return | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 miagilepner
					miagilepner