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

Copy request in timeout handler #108455

Merged
merged 1 commit into from Mar 24, 2022
Merged

Conversation

Argh4k
Copy link
Contributor

@Argh4k Argh4k commented Mar 2, 2022

What type of PR is this?

/kind bug

What this PR does / why we need it:

This PR fixes race condition:

We can also get to the CleanVerb function by postTimeoutFn() called by timeout handler in case of timeout:


This function looks like this:
postTimeoutFn := func() {
metrics.RecordRequestTermination(req, requestInfo, metrics.APIServerComponent, http.StatusGatewayTimeout)
}

And calls cleanVerb (that internally calls CleanVerb):
reportedVerb := cleanVerb(CanonicalVerb(strings.ToUpper(req.Method), scope), getVerbIfWatch(req), req)

Those are spots that I've found, but there might be even more of those.

Test cases added in this pull request when run without fix and with -race flag, crash with race condition detected. In case of TestTimeoutWithLogging passing --args -v=3 is also necessary to force logging.

Which issue(s) this PR fixes:

Fixes #

Fix a race in timeout handler that could lead to kube-apiserver crashes

@k8s-ci-robot k8s-ci-robot added kind/bug Categorizes issue or PR as related to a bug. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Mar 2, 2022
@k8s-ci-robot
Copy link
Contributor

Welcome @Argh4k!

It looks like this is your first PR to kubernetes/kubernetes 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/kubernetes has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Mar 2, 2022
@k8s-ci-robot
Copy link
Contributor

Hi @Argh4k. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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.

@k8s-ci-robot k8s-ci-robot added needs-priority Indicates a PR lacks a `priority/foo` label and requires one. area/apiserver sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Mar 2, 2022
@Argh4k
Copy link
Contributor Author

Argh4k commented Mar 2, 2022

/assign @wojtek-t
/assign @liggitt
/assign @mborsz

@aojea
Copy link
Member

aojea commented Mar 2, 2022

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Mar 2, 2022
@mborsz
Copy link
Member

mborsz commented Mar 3, 2022

/cc @wojtek-t @liggitt
/retest

Please fix gofmt

staging/src/k8s.io/apiserver/pkg/server/filters/timeout.go Outdated Show resolved Hide resolved
@@ -93,6 +93,10 @@ func (t *timeoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
resultCh := make(chan interface{})
var tw timeoutWriter
tw, w = newTimeoutWriter(w)

//Make a copy of request and work on it in new goroutine
//to avoid race condition when accessing/modifying request headers
Copy link
Member

Choose a reason for hiding this comment

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

Are request headers the only problematic case?

If so, maybe we can shallow copy request and deep clone headers only to save few bytes?

Copy link
Member

Choose a reason for hiding this comment

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

I think for now we believe only headers are problematic, but I can imagine (e.g. in future) some handler modifying any other field in http.Request.

I think it's reasonable to clone entire object, but I would change the comment to say something like "to avoid races when accessing/modifying http.Request (e.g. request headers)".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I would rather make deep copy of all request metadata. This way we ensure that further changes to filter that precede timeout handler probably won't cause race condition. I think there is very low probability that any filter will touch body of the request, which is not deep copied here.

Copy link
Member

@mborsz mborsz left a comment

Choose a reason for hiding this comment

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

One nit.

Otherwise LGTM.

The potential issue with this solution is that it blocks propagation of request's headers changes to filters that are above WithTimeoutForNonLongRunningRequests. It was already possible that the request timed out before the changes were made (which generated this race), but now it happens for all requests.

I tried to skim the code around and I wasn't able to find any practical problem with this now. This may lead to some potential issues in future if e.g. someone implements a filter A that changes request and expect this change to be propagated to another filter B (with WithTimeoutForNonLongRunningRequests in the middle), but:

  • It's not clear to me if we will ever need to implement this kind of filters
  • It will be easy to catch during testing (no request will have that request change propagated)
  • Even without this change, it was possible that we never reach filter B (e.g. due to timeout), so any implementation of filter A that expected request change of B was simply invalid in general case. Now we block such changes which makes this more obvious.

So for me it looks good, but I would like to hear opinion of more senior folks about this change: @wojtek-t @liggitt

@aojea
Copy link
Member

aojea commented Mar 3, 2022

I tried to skim the code around and I wasn't able to find any practical problem with this now. This may lead to some potential issues in future if e.g. someone implements a filter A that changes request and expect this change to be propagated to another filter B (with WithTimeoutForNonLongRunningRequests in the middle), but:

what if instead of passing the clone to the handlers we use the clone for the timeout handler instead, and we pass the original request?

@fedebongio
Copy link
Contributor

/assign @tkashem
/triage accepted

@mborsz
Copy link
Member

mborsz commented Mar 17, 2022

is there a better way to implement timeouts on requests without using goroutines?

The most common way in golang I have seen in other libraries is to simply use a single goroutine and use context cancellation for timeout. There is some discussion around that in #105884 but overall it looks like e.g. Write operation doesn't use context which makes it hard to switch to this approach now: #105884 (comment)

Long term it looks like a way to go, but we will need to either to have a golang's support for Write with context support or workaround it somehow.

@aojea
Copy link
Member

aojea commented Mar 17, 2022

Long term it looks like a way to go, but we will need to either to have a golang's support for Write with context support or workaround it somehow.

It seems that all options were explored and this is the least bad option :)
LGTM , can we add a reference to this issue on the comment? there is some good context here for the future
Defer to wojtek and Jordan

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Mar 24, 2022
@wojtek-t
Copy link
Member

Added a release-note - we should cherrypick it once this is merged.

/lgtm
/aprove

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 24, 2022
@wojtek-t
Copy link
Member

/approve

@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 24, 2022
@Argh4k
Copy link
Contributor Author

Argh4k commented Mar 24, 2022

/retest

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Mar 24, 2022
@k8s-ci-robot k8s-ci-robot removed lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Mar 24, 2022
@wojtek-t
Copy link
Member

/lgtm
/approve

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

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Argh4k, wojtek-t

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

@k8s-triage-robot
Copy link

The Kubernetes project has merge-blocking tests that are currently too flaky to consistently pass.

This bot retests PRs for certain kubernetes repos according to the following rules:

  • The PR does have any do-not-merge/* labels
  • The PR does not have the needs-ok-to-test label
  • The PR is mergeable (does not have a needs-rebase label)
  • The PR is approved (has cncf-cla: yes, lgtm, approved labels)
  • The PR is failing tests required for merge

You can:

/retest

@k8s-ci-robot k8s-ci-robot merged commit 9bb5823 into kubernetes:master Mar 24, 2022
@k8s-ci-robot k8s-ci-robot added this to the v1.24 milestone Mar 24, 2022
k8s-ci-robot added a commit that referenced this pull request Mar 25, 2022
…55-upstream-release-1.21

Automated cherry pick of #108455: Copy request in timeout handler
k8s-ci-robot added a commit that referenced this pull request Mar 25, 2022
…55-upstream-release-1.22

Automated cherry pick of #108455: Copy request in timeout handler
k8s-ci-robot added a commit that referenced this pull request Mar 25, 2022
…55-upstream-release-1.23

Automated cherry pick of #108455: Copy request in timeout handler
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/apiserver cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants