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

daemon, cmd/snapd: propagate context #14130

Merged
merged 10 commits into from
Jun 28, 2024

Conversation

bboozzoo
Copy link
Contributor

This is another take at #13961, but this time around the branch is attempting at a refactor of how the context is propagated and how it interacts with cancelation.

Related: SNAPDENG-22871

@github-actions github-actions bot added the Needs Documentation -auto- Label automatically added which indicates the change needs documentation label Jun 26, 2024
@bboozzoo bboozzoo changed the title Bboozzoo/context cleanup cancel daemon, cmd/snapd: propagate context Jun 26, 2024
@bboozzoo bboozzoo closed this Jun 26, 2024
@bboozzoo bboozzoo reopened this Jun 26, 2024
@bboozzoo bboozzoo removed the Needs Documentation -auto- Label automatically added which indicates the change needs documentation label Jun 26, 2024
Copy link
Contributor

@sergio-costas sergio-costas left a comment

Choose a reason for hiding this comment

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

Little detail, not very important.

@@ -145,6 +145,7 @@ func postSnap(c *Command, r *http.Request, user *auth.UserState) Response {
return BadRequest("unknown action %s", inst.Action)
}

// TODO use an epxlicit context parameter
Copy link
Contributor

Choose a reason for hiding this comment

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

typo

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks for catching, I've updated the patch

@bboozzoo bboozzoo force-pushed the bboozzoo/context-cleanup-cancel branch from c87a81d to d57db28 Compare June 26, 2024 10:48
@github-actions github-actions bot added the Needs Documentation -auto- Label automatically added which indicates the change needs documentation label Jun 26, 2024
@bboozzoo bboozzoo removed the Needs Documentation -auto- Label automatically added which indicates the change needs documentation label Jun 26, 2024
@github-actions github-actions bot added the Needs Documentation -auto- Label automatically added which indicates the change needs documentation label Jun 26, 2024
@sergio-costas
Copy link
Contributor

sergio-costas commented Jun 26, 2024

Tested with snapd-desktop-integration. Seems to work as expected. (EDIT: at least after the last patch; before it crashed).

@bboozzoo bboozzoo removed the Needs Documentation -auto- Label automatically added which indicates the change needs documentation label Jun 26, 2024
Establish a cancelation chain for incoming API requests, to ensure orderly
shutdown. This prevents a situation in which an API request, such as notices
wait can block snapd shtudown for a long time.

Signed-off-by: Maciej Borzecki <[email protected]>
Request's can be canceled based on the code actually issuing a cancel on the
associted context, hence an Internal Server Error seems more appropriate.

Signed-off-by: Maciej Borzecki <[email protected]>
Pass the context from the API request further down.

Signed-off-by: Maciej Borzecki <[email protected]>
@bboozzoo bboozzoo force-pushed the bboozzoo/context-cleanup-cancel branch from f80e7ce to f01e830 Compare June 27, 2024 06:42
@github-actions github-actions bot added the Needs Documentation -auto- Label automatically added which indicates the change needs documentation label Jun 27, 2024
@bboozzoo
Copy link
Contributor Author

there were some conflicts in cmd/snapd/main.go, so I rebased on top of current master

Copy link
Contributor

@MiguelPires MiguelPires left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@ZeyadYasser ZeyadYasser left a comment

Choose a reason for hiding this comment

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

I really like this, Thank you!

@@ -882,7 +881,7 @@ func meetSnapConstraintsForEnforce(inst *snapInstruction, st *state.State, vErr
return snapstateResolveValSetsEnforcementError(context.TODO(), st, vErr, pinnedSeqs, inst.userID)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to pass context here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks for spotting

Copy link
Collaborator

@pedronis pedronis left a comment

Choose a reason for hiding this comment

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

thanks, I think the changes need a bit more direct testing

daemon/daemon.go Outdated
@@ -320,7 +321,7 @@ func (d *Daemon) initStandbyHandling() {
}

// Start the Daemon
Copy link
Collaborator

Choose a reason for hiding this comment

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

the doc comment should probably say something about ctx

defer func() {
// cancel the context on any errors
if err != nil {
cancel()
Copy link
Collaborator

Choose a reason for hiding this comment

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

we don't really have tests that show that is relevant, do we ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not really, but coverage shows this place is hit in TestHandleUnexpectedRestart test

@@ -482,6 +499,10 @@ func (d *Daemon) Stop(sigCh chan<- os.Signal) error {
return fmt.Errorf("internal error: no Overlord")
}

if d.cancel != nil {
d.cancel()
Copy link
Collaborator

Choose a reason for hiding this comment

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

this probably needs an explicit test in daemon_test.go

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I"ve added couple more tests that verify context cancelation and propagation, and one which hijacks d.cancel

@pedronis pedronis added the Needs Samuele review Needs a review from Samuele before it can land label Jun 27, 2024
@bboozzoo bboozzoo requested a review from pedronis June 28, 2024 06:57
Copy link
Collaborator

@pedronis pedronis left a comment

Choose a reason for hiding this comment

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

thank you, one test question

// since the tests aren't in a package, we directly hijack d.cancel
c.Assert(d.cancel, check.NotNil)
cancel := d.cancel
defer cancel()
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not entirely sure what this tests is about, shouldn't this check a bit more?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I'll drop this one for now. The idea was to check that Stop() wouldn't call cancel if it's nil. Another way to trigger this would be to call Stop(), without a prior call to Start(), but we clearly do not support that and it segfaults on calling d.standbyOpinions.Stop().

The test isn't very useful. Another option to trigger this would be to call
Stop() without a prior call to Start(), but this segfaults on
d.standbyOpinions.Stop(), so it'c clear this needs a followup fix or callign
Stop() this way isn't supported.

Signed-off-by: Maciej Borzecki <[email protected]>
@bboozzoo bboozzoo merged commit 61d7eba into canonical:master Jun 28, 2024
48 of 51 checks passed
@bboozzoo bboozzoo deleted the bboozzoo/context-cleanup-cancel branch June 28, 2024 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Documentation -auto- Label automatically added which indicates the change needs documentation Needs Samuele review Needs a review from Samuele before it can land
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants