Skip to content

Commit

Permalink
Merge pull request #1712 from vpodzime/http_resumer_leftovers
Browse files Browse the repository at this point in the history
Proper cleanup of HTTP leftovers
  • Loading branch information
lluiscampos authored Dec 12, 2024
2 parents f9e821f + 6a0c890 commit 9b047a9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
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

0 comments on commit 9b047a9

Please sign in to comment.