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 an error where exit status wasn't getting captured by the cleanup process #2230

Merged
merged 1 commit into from
Jul 21, 2023

Conversation

moskyb
Copy link
Contributor

@moskyb moskyb commented Jul 20, 2023

tl;dr before:

defer cancel()
defer r.cleanup(cctx, &wg, exit)

after:

defer func(ctx context.Context, wg *sync.WaitGroup) {
	cancel()
	r.cleanup(ctx, wg, exit)
}(ctx, &wg)

previously we were capturing the value of exit at the time of the call to defer, now we're capturing the value at the time the defer is actually executed, which is what we wanted in the first place.

this PR also adds a regression test to ensure that this doesn't happen again

@moskyb moskyb requested a review from a team July 20, 2023 05:23
Copy link
Contributor

@DrJosh9000 DrJosh9000 left a comment

Choose a reason for hiding this comment

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

Looking good - I have one or two tweaks


for _, b := range tae.calls[endpoint] {
var job api.Job
err := json.NewDecoder(bytes.NewBuffer(b)).Decode(&job)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
err := json.NewDecoder(bytes.NewBuffer(b)).Decode(&job)
err := json.Unmarshal(b, &job)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh, duh

@@ -230,7 +280,7 @@ func (t *testAgentEndpoint) server(jobID string) *httptest.Server {
case "/jobs/" + jobID + "/finish":
rw.WriteHeader(http.StatusOK)
default:
http.Error(rw, fmt.Sprintf("not found; method = %q, path = %q", req.Method, req.URL.Path), http.StatusNotFound)
panic(fmt.Sprintf("not found; method = %q, path = %q", req.Method, req.URL.Path))
Copy link
Contributor

Choose a reason for hiding this comment

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

What's this change for?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm curious about this too. Is it because you need a json error message? I think you should craft one if so.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is a relic from when i was bughunting and flailing a bit; i think my logic was that if an endpoint that we haven't accounted for is hit it's a Very Bad Thing, and i don't think these errors are super well surfaced in the tests???? maybe? but also this doesn't really need to be here, i might just yeet it

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's better to return a 4xx error in such circumstances and raise an error from the Test method. It will be easier to debug how the route was called then.

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess the error handling could happen entirely in the non-test code. Hopefully, writing code that ignores errors is something we have gotten out of the habit of doing.

@@ -214,6 +248,22 @@ func createTestAgentEndpoint() *testAgentEndpoint {
}
}

func (tae *testAgentEndpoint) finishesFor(t *testing.T, jobID string) []api.Job {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func (tae *testAgentEndpoint) finishesFor(t *testing.T, jobID string) []api.Job {
func (tae *testAgentEndpoint) finishesFor(t *testing.T, jobID string) []api.Job {
t.Helper()

so that if an error occurs in this method, the output will show a line number from the test method.

@moskyb moskyb merged commit 4dc15c9 into main Jul 21, 2023
1 check passed
@moskyb moskyb deleted the fix-defer-capture-bug branch July 21, 2023 00:38
@DrJosh9000 DrJosh9000 added the bug label Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants