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

Proper cleanup of HTTP leftovers #1712

Merged
merged 2 commits into from
Dec 12, 2024
Merged
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
3 changes: 3 additions & 0 deletions src/common/http/http_resumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,9 @@ http::OutgoingRequestPtr DownloadResumerClient::RemainingRangeRequest() const {
};

error::Error DownloadResumerClient::ScheduleNextResumeRequest() {
// In any case, make sure the previous HTTP request is cancelled.
client_.Cancel();

auto exp_interval = retry_.backoff.NextInterval();
if (!exp_interval) {
return http::MakeError(
Expand Down
1 change: 1 addition & 0 deletions src/mender-update/daemon/state_machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class StateMachine {
PollForDeploymentState poll_for_deployment_state_;
SendStatusUpdateState send_download_status_state_;
UpdateDownloadState update_download_state_;
UpdateDownloadCancelState update_download_cancel_state_;
SendStatusUpdateState send_install_status_state_;
UpdateInstallState update_install_state_;

Expand Down
5 changes: 4 additions & 1 deletion src/mender-update/daemon/state_machine/state_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,12 @@ StateMachine::StateMachine(Context &ctx, events::EventLoop &event_loop) :

main_states_.AddTransition(update_download_state_, se::Success, ss.download_leave_, tf::Immediate);
main_states_.AddTransition(update_download_state_, se::StateLoopDetected, state_loop_state_, tf::Immediate);
main_states_.AddTransition(update_download_state_, se::Failure, ss.download_error_, tf::Immediate);
main_states_.AddTransition(update_download_state_, se::Failure, update_download_cancel_state_, tf::Immediate);
main_states_.AddTransition(update_download_state_, se::NothingToDo, ss.download_leave_save_provides, tf::Immediate);

// Cannot fail because download cancellation is a void function as there's nothing to do if it fails, anyway.
main_states_.AddTransition(update_download_cancel_state_, se::Success, ss.download_error_, tf::Immediate);

main_states_.AddTransition(ss.download_leave_, se::Success, send_install_status_state_, tf::Immediate);
main_states_.AddTransition(ss.download_leave_, se::Failure, ss.download_error_, tf::Immediate);

Expand Down
8 changes: 6 additions & 2 deletions src/mender-update/daemon/states.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,13 @@ void UpdateDownloadState::OnEnter(Context &ctx, sm::EventPoster<StateEvent> &pos
if (resp->GetStatusCode() != http::StatusOK) {
log::Error(
"Unexpected status code while fetching artifact: " + resp->GetStatusMessage());
ctx.download_client->Cancel();
poster.PostEvent(StateEvent::Failure);
return;
}

auto http_reader = resp->MakeBodyAsyncReader();
if (!http_reader) {
log::Error(http_reader.error().String());
ctx.download_client->Cancel();
poster.PostEvent(StateEvent::Failure);
return;
}
Expand Down Expand Up @@ -455,6 +453,12 @@ void UpdateDownloadState::DoDownload(Context &ctx, sm::EventPoster<StateEvent> &
}
}

void UpdateDownloadCancelState::OnEnter(Context &ctx, sm::EventPoster<StateEvent> &poster) {
log::Debug("Entering DownloadCancel state");
ctx.download_client->Cancel();
poster.PostEvent(StateEvent::Success);
}

SendStatusUpdateState::SendStatusUpdateState(optional<deployments::DeploymentStatus> status) :
status_(status),
mode_(FailureMode::Ignore) {
Expand Down
5 changes: 5 additions & 0 deletions src/mender-update/daemon/states.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ class UpdateDownloadState : virtual public StateType {
static void DoDownload(Context &ctx, sm::EventPoster<StateEvent> &poster);
};

class UpdateDownloadCancelState : virtual public StateType {
public:
void OnEnter(Context &ctx, sm::EventPoster<StateEvent> &poster) override;
};

class SendStatusUpdateState : virtual public StateType {
public:
// Ignore-failure version.
Expand Down