Skip to content

🌱 use select with time.After instead of time.Sleep#13480

Merged
k8s-ci-robot merged 1 commit intokubernetes-sigs:mainfrom
sivchari:use-select-with-timeafter
Mar 22, 2026
Merged

🌱 use select with time.After instead of time.Sleep#13480
k8s-ci-robot merged 1 commit intokubernetes-sigs:mainfrom
sivchari:use-select-with-timeafter

Conversation

@sivchari
Copy link
Copy Markdown
Member

What this PR does / why we need it:

use time.After instead of time.Sleep for receiving context cancellation while sleeping.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Mar 18, 2026
@k8s-ci-robot k8s-ci-robot added do-not-merge/needs-area PR is missing an area label size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Mar 18, 2026
@sivchari sivchari force-pushed the use-select-with-timeafter branch from 3fd8a4c to 67a4299 Compare March 18, 2026 04:45
return
}
time.Sleep(5 * time.Second)
case <-time.After(5 * time.Second):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does using a 5 second time ticker? simplifies even more?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

@sivchari sivchari force-pushed the use-select-with-timeafter branch 2 times, most recently from d37ba1b to a20d4e1 Compare March 18, 2026 06:19
Comment on lines 287 to 303
go func() {
for {
select {
case <-timeoutCtx.Done():
return
default:
updatedDockerMachine := &infrav1.DockerMachine{}
if err := r.Get(ctx, client.ObjectKeyFromObject(dockerMachine), updatedDockerMachine); err == nil &&
!updatedDockerMachine.DeletionTimestamp.IsZero() {
log.Info("Cancelling Bootstrap because the underlying machine has been deleted")
cancel()
return
}
time.Sleep(5 * time.Second)
case <-ticker.C:
}

updatedDockerMachine := &infrav1.DockerMachine{}
if err := r.Get(ctx, client.ObjectKeyFromObject(dockerMachine), updatedDockerMachine); err == nil &&
!updatedDockerMachine.DeletionTimestamp.IsZero() {
log.Info("Cancelling Bootstrap because the underlying machine has been deleted")
cancel()
return
}
}
}()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought something like this,(may be wrong aswell)

go func() {
    ticker := time.NewTicker(5 * time.Second)
    defer ticker.Stop() 

    for {
        select {
        case <-timeoutCtx.Done():
            return
            
        case <-ticker.C: 
            updatedDockerMachine := &infrav1.DockerMachine{}
            
            err := r.Get(ctx, client.ObjectKeyFromObject(dockerMachine), updatedDockerMachine)
            if err == nil && !updatedDockerMachine.DeletionTimestamp.IsZero() {
                log.Info("Cancelling Bootstrap because the underlying machine has been deleted")
                cancel()
                return
            }
        }
    }
}()

Copy link
Copy Markdown
Member Author

@sivchari sivchari Mar 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion! I moved the ticker creation inside the goroutine.

Since CAPI uses Go 1.25, tickers are automatically stopped by the garbage collector, so an explicit Stop() is not necessary. I intentionally omitted it to keep the code minimal.
I intentionally placed the logic after the select block rather than inside case <-ticker.C: to keep the nesting shallow and improve readability.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, thanks

Signed-off-by: sivchari <shibuuuu5@gmail.com>
@sivchari sivchari force-pushed the use-select-with-timeafter branch from a20d4e1 to 0e46697 Compare March 18, 2026 08:13
@sbueringer sbueringer added the area/provider/infrastructure-docker Issues or PRs related to the docker infrastructure provider label Mar 19, 2026
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/needs-area PR is missing an area label label Mar 19, 2026
@sbueringer
Copy link
Copy Markdown
Member

Thx!

/approve

/assign @Karthik-K-N
For another look

@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: sbueringer

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 19, 2026
@sbueringer
Copy link
Copy Markdown
Member

/test pull-cluster-api-e2e-main-gke

Copy link
Copy Markdown
Contributor

@Karthik-K-N Karthik-K-N left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

Thank you.

Comment on lines 287 to 303
go func() {
for {
select {
case <-timeoutCtx.Done():
return
default:
updatedDockerMachine := &infrav1.DockerMachine{}
if err := r.Get(ctx, client.ObjectKeyFromObject(dockerMachine), updatedDockerMachine); err == nil &&
!updatedDockerMachine.DeletionTimestamp.IsZero() {
log.Info("Cancelling Bootstrap because the underlying machine has been deleted")
cancel()
return
}
time.Sleep(5 * time.Second)
case <-ticker.C:
}

updatedDockerMachine := &infrav1.DockerMachine{}
if err := r.Get(ctx, client.ObjectKeyFromObject(dockerMachine), updatedDockerMachine); err == nil &&
!updatedDockerMachine.DeletionTimestamp.IsZero() {
log.Info("Cancelling Bootstrap because the underlying machine has been deleted")
cancel()
return
}
}
}()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, thanks

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 22, 2026
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

LGTM label has been added.

DetailsGit tree hash: 15a694c885bb441cce13eba35896d979699a09f4

@k8s-ci-robot k8s-ci-robot merged commit b5e62fa into kubernetes-sigs:main Mar 22, 2026
20 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.13 milestone Mar 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. area/provider/infrastructure-docker Issues or PRs related to the docker infrastructure provider cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants