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 timeoutSeconds #15630

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 4 additions & 17 deletions pkg/http/handler/timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,8 @@ func (h *timeoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
case <-timeout.C():
timeoutDrained = true
if tw.tryTimeoutAndWriteError(h.body) {
return
}
tw.forceTimeoutAndWriteError(h.body)
return
case now := <-idleTimeoutCh:
timedOut, timeToNextTimeout := tw.tryIdleTimeoutAndWriteError(now, revIdleTimeout, h.body)
if timedOut {
Expand Down Expand Up @@ -214,22 +213,10 @@ func (tw *timeoutWriter) WriteHeader(code int) {
tw.w.WriteHeader(code)
}

// tryTimeoutAndWriteError writes an error to the responsewriter if
// nothing has been written to the writer before. Returns whether
// an error was written or not.
//
// If this writes an error, all subsequent calls to Write will
// result in http.ErrHandlerTimeout.
func (tw *timeoutWriter) tryTimeoutAndWriteError(msg string) bool {
func (tw *timeoutWriter) forceTimeoutAndWriteError(msg string) {
tw.mu.Lock()
defer tw.mu.Unlock()

if tw.lastWriteTime.IsZero() {
tw.timeoutAndWriteError(msg)
return true
}

return false
tw.timeoutAndWriteError(msg)
}

// tryResponseStartTimeoutAndWriteError writes an error to the responsewriter if
Expand Down
2 changes: 0 additions & 2 deletions pkg/http/handler/timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ func TestTimeoutWriterAllowsForAdditionalWritesBeforeTimeout(t *testing.T) {
clock := clock.RealClock{}
handler := &timeoutWriter{w: recorder, clock: clock}
handler.WriteHeader(http.StatusOK)
handler.tryTimeoutAndWriteError("error")
handler.tryResponseStartTimeoutAndWriteError("error")
Comment on lines -37 to -38
Copy link
Member

Choose a reason for hiding this comment

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

why drop tryResponseStartTimeoutAndWriteError ?

handler.tryIdleTimeoutAndWriteError(clock.Now(), 10*time.Second, "error")
if _, err := io.WriteString(handler, "test"); err != nil {
t.Fatalf("handler.Write() = %v, want no error", err)
Expand Down
12 changes: 9 additions & 3 deletions test/conformance/api/v1/revision_timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (

// sendRequest send a request to "endpoint", returns error if unexpected response code, nil otherwise.
func sendRequest(t *testing.T, clients *test.Clients, endpoint *url.URL,
initialSleep, sleep time.Duration, expectedResponseCode int) error {
initialSleep, sleep time.Duration, expectedResponseCode int, expectedBody string) error {
client, err := pkgtest.NewSpoofingClient(context.Background(), clients.KubeClient, t.Logf, endpoint.Hostname(), test.ServingFlags.ResolvableDomain, test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS))
if err != nil {
return fmt.Errorf("error creating Spoofing client: %w", err)
Expand Down Expand Up @@ -68,7 +68,12 @@ func sendRequest(t *testing.T, clients *test.Clients, endpoint *url.URL,
if expectedResponseCode != resp.StatusCode {
return fmt.Errorf("response status code = %v, want = %v, response = %v", resp.StatusCode, expectedResponseCode, resp)
}

if expectedBody != "" {
gotBody := string(resp.Body)
if expectedBody != gotBody {
return fmt.Errorf("response body = %v, want = %v, response = %v", gotBody, expectedBody, resp)
}
}
return nil
}

Expand Down Expand Up @@ -99,6 +104,7 @@ func TestRevisionTimeout(t *testing.T) {
expectedStatus: http.StatusOK,
sleep: 15 * time.Second,
initialSleep: 0,
expectedBody: "activator request timeout",
Copy link
Member

Choose a reason for hiding this comment

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

We should delete the test like @izabelacg recommended since it was originally intended to test idle timeout and we have that in our e2e test

#15089 (comment)

We now know why this test was passing when it shouldn't have.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Once you write a 200 back you cant fail it. I dont think we should avoid testing this.

Copy link
Member

@dprotaso dprotaso Jan 17, 2025

Choose a reason for hiding this comment

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

conformance tests can't assume things about the implementation. This scenario isn't defined in the knative spec so I'd rather remove it from these tests.

I believe we have the coverage in our e2e tests

Copy link
Contributor Author

@skonto skonto Jan 20, 2025

Choose a reason for hiding this comment

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

@dprotaso As we discussed it is not in the spec but imho it should be defined as it is something the user sees from our implementation (user gets a body txt that is part of the system behavior). Who should we ask about Cloud run? Maybe we could add this to the spec.

}}

for _, tc := range testCases {
Expand Down Expand Up @@ -136,7 +142,7 @@ func TestRevisionTimeout(t *testing.T) {
t.Fatalf("Error probing %s: %v", serviceURL, err)
}

if err := sendRequest(t, clients, serviceURL, tc.initialSleep, tc.sleep, tc.expectedStatus); err != nil {
if err := sendRequest(t, clients, serviceURL, tc.initialSleep, tc.sleep, tc.expectedStatus, tc.expectedBody); err != nil {
t.Errorf("Failed request with initialSleep %v, sleep %v, with revision timeout %ds, expecting status %v: %v",
tc.initialSleep, tc.sleep, tc.timeoutSeconds, tc.expectedStatus, err)
}
Expand Down
Loading