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

Fix race conditions when creating watches #1113

Merged
merged 9 commits into from
Nov 25, 2020

Conversation

akerekes
Copy link
Contributor

Description

Fix a race condition between creating a watch and initiating the action that emits the event it is watching for.

Currently the wait.Wait function creates the watch and is invoked in a goroutine, i.e. parallel to the command whose result the watch is targeted for. If the watch creation takes longer in the goroutine than the main execution thread's call to the backend, the event generated by the change can be emitted sooner than the watch is listening and it will wait there until timeout happens.

Changes

  • Update wait.Wait.Wait() to accept a watch, instead of the implementing types creating it
  • Update callers of wait.Wait.Wait() to create the watch upfront

Reference

Fixes #1109

/lint

@google-cla google-cla bot added the cla: yes Indicates the PR's author has signed the CLA. label Nov 11, 2020
@knative-prow-robot knative-prow-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 11, 2020
Copy link
Contributor

@knative-prow-robot knative-prow-robot left a comment

Choose a reason for hiding this comment

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

@akerekes: 2 warnings.

In response to this:

Description

Fix a race condition between creating a watch and initiating the action that emits the event it is watching for.

Currently the wait.Wait function creates the watch and is invoked in a goroutine, i.e. parallel to the command whose result the watch is targeted for. If the watch creation takes longer in the goroutine than the main execution thread's call to the backend, the event generated by the change can be emitted sooner than the watch is listening and it will wait there until timeout happens.

Changes

  • Update wait.Wait.Wait() to accept a watch, instead of the implementing types creating it
  • Update callers of wait.Wait.Wait() to create the watch upfront

Reference

Fixes #1109

/lint

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

pkg/wait/wait_for_ready.go Show resolved Hide resolved
pkg/wait/wait_for_ready.go Show resolved Hide resolved
@knative-prow-robot knative-prow-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Nov 11, 2020
@knative-prow-robot knative-prow-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 11, 2020
@knative-prow-robot knative-prow-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 13, 2020
@knative-prow-robot knative-prow-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 16, 2020
@knative-metrics-robot
Copy link

The following is the coverage report on the affected files.
Say /test pull-knative-client-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/eventing/v1beta1/client.go 87.4% 86.9% -0.5
pkg/serving/v1/client.go 65.4% 64.5% -0.8
pkg/wait/wait_for_ready.go 76.8% 76.9% 0.2

Copy link
Collaborator

@navidshaikh navidshaikh left a comment

Choose a reason for hiding this comment

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

/assign @rhuss

@knative-prow-robot knative-prow-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 17, 2020
@rhuss
Copy link
Contributor

rhuss commented Nov 17, 2020

Thanks ! looking at it tomorrow ...

Copy link
Contributor

@rhuss rhuss left a comment

Choose a reason for hiding this comment

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

Good catch ! Looks good to me (one minor question though), and sorry for the delay in reviewing this.

/approve
/lgtm

waitForEvent := wait.NewWaitForEvent("service", cl.WatchService, func(evt *watch.Event) bool { return evt.Type == watch.Deleted })
err, _ := waitForEvent.Wait(serviceName, wait.Options{Timeout: &timeout}, wait.NoopMessageCallback())
waitForEvent := wait.NewWaitForEvent("service", func(evt *watch.Event) bool { return evt.Type == watch.Deleted })
err, _ := waitForEvent.Wait(watcher, serviceName, wait.Options{Timeout: &timeout}, wait.NoopMessageCallback())
Copy link
Contributor

Choose a reason for hiding this comment

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

This closure works as long as watcher is in scope. Not sure how it works in go, but if this variable would go out of scope (e.g. when this function where it is allocated is left), wouldn't it be garbage collected and the go routine (in case it is still running) would then access a zombie watcher object ?

I know in this case this is a theoretical question, as we block on the channel before leaving the function, and only the goroutine can send to the channel.

So it's more a theoretical question, I guess :)

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label Nov 25, 2020
@knative-prow-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: akerekes, navidshaikh, rhuss

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

The pull request process is described here

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

@knative-prow-robot knative-prow-robot merged commit 83b8635 into knative:master Nov 25, 2020
@navidshaikh navidshaikh added the backport/candidate Consider this PR to be backported to the release branch label Nov 25, 2020
navidshaikh added a commit to navidshaikh/client that referenced this pull request Nov 25, 2020
* Fix a race condition between creating a watch and initiating the action that emits the event it is watching for

* update changelog

* add PR ID to changelog entry

* Fix merge in Changelog

* Fix table format in Changelog
navidshaikh added a commit to navidshaikh/client that referenced this pull request Nov 25, 2020
* Fix a race condition between creating a watch and initiating the action that emits the event it is watching for

* update changelog

* add PR ID to changelog entry

* Fix merge in Changelog

* Fix table format in Changelog
knative-prow-robot pushed a commit that referenced this pull request Nov 25, 2020
* Embed the namespace in request body while creating channels (#1117)

* Embed the namespace in request body while creating channels

 since on the eventing side, defaulting for channel isnt picking
 the namespace from the context (see knative/eventing#4514)

 workaround for #1100
 this changeset should be reverted when eventing#4514 is resolved

* Add CHANGELOG

* Update CHANGELOG for v0.19.1

* Cross-compile the kn binary for linux/s390x (#1083)

* Update CHANGELOG for v0.19.1

* Fix date in changelog

* Fix race conditions when creating watches (#1113)

* Fix a race condition between creating a watch and initiating the action that emits the event it is watching for

* update changelog

* add PR ID to changelog entry

* Fix merge in Changelog

* Fix table format in Changelog
knative-prow-robot pushed a commit that referenced this pull request Nov 25, 2020
* Cross-compile the kn binary for linux/s390x (#1083)

* Update CHANGELOG for kn v0.18.4

* Fix race conditions when creating watches (#1113)

* Fix a race condition between creating a watch and initiating the action that emits the event it is watching for

* update changelog

* add PR ID to changelog entry

* Fix merge in Changelog

* Fix table format in Changelog
@navidshaikh navidshaikh added backported-to/0.18 and removed backport/candidate Consider this PR to be backported to the release branch labels Nov 26, 2020
ericmillin added a commit to ericmillin/client that referenced this pull request Mar 18, 2021
dsimansk added a commit to dsimansk/client that referenced this pull request Aug 5, 2022
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. cla: yes Indicates the PR's author has signed the CLA. lgtm Indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Race condition in commands using watch
5 participants