Skip to content

Commit 089cdcd

Browse files
MichaelUrayCodex
authored andcommitted
Address CodeRabbit review feedback on openwisp#251
- Write PENDING_REPORT atomically via tmp file + rename so a partial write (e.g. full disk) cannot leave a truncated marker behind. - Fall back to invalidating the persistent checksum when the pending marker cannot be saved, preserving the previous force re-download behaviour as a safety net. - Only retry a pending report after configuration_changed() returns 0 (confirmed no-change). Non-0/non-1 exit codes indicate checksum fetch failures and the cached applied/error status may no longer reflect the controller's current desired configuration.
1 parent be7e11d commit 089cdcd

1 file changed

Lines changed: 25 additions & 6 deletions

File tree

openwisp-config/files/openwisp.agent

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ update_configuration() {
826826
result=$?
827827
fi
828828

829-
local status_to_report
829+
local status_to_report pending_marker_saved=0
830830
if [ "$result" -eq "0" ]; then
831831
logger "Configuration applied successfully" \
832832
-t openwisp \
@@ -843,7 +843,18 @@ update_configuration() {
843843
# Persist the pending status BEFORE calling report_status so that a
844844
# crash or reboot between the retry attempt and success does not lose
845845
# the status. The marker is removed only after report_status succeeds.
846-
printf '%s\n' "$status_to_report" >"$PENDING_REPORT"
846+
# Write atomically via a tmp file + rename so a partial write (e.g.
847+
# full disk) cannot leave a truncated marker behind.
848+
if printf '%s\n' "$status_to_report" >"${PENDING_REPORT}.tmp" \
849+
&& mv "${PENDING_REPORT}.tmp" "$PENDING_REPORT"; then
850+
pending_marker_saved=1
851+
else
852+
rm -f "${PENDING_REPORT}.tmp"
853+
logger -s "Failed to persist pending report marker, " \
854+
"falling back to checksum invalidation on failure" \
855+
-t openwisp \
856+
-p daemon.warning
857+
fi
847858

848859
retry_with_backoff report_status "$status_to_report"
849860
# shellcheck disable=SC2181
@@ -853,6 +864,12 @@ update_configuration() {
853864
logger -s "report_status failed, will retry in the next cycle" \
854865
-t openwisp \
855866
-p daemon.warning
867+
# If the marker could not be saved, invalidate the checksum so
868+
# the next cycle forces a re-download. This preserves the
869+
# previous behaviour whenever the retry marker is unavailable.
870+
if [ "$pending_marker_saved" -eq "0" ]; then
871+
rm -f "$PERSISTENT_CHECKSUM"
872+
fi
856873
fi
857874

858875
rm $APPLYING_CONF
@@ -1025,13 +1042,15 @@ while true; do
10251042
discover_management_ip
10261043

10271044
configuration_changed
1045+
config_change_status=$?
10281046

1029-
if [ "$?" -eq "1" ]; then
1047+
if [ "$config_change_status" -eq "1" ]; then
10301048
update_configuration
1031-
elif [ -f "$PENDING_REPORT" ]; then
1049+
elif [ "$config_change_status" -eq "0" ] && [ -f "$PENDING_REPORT" ]; then
10321050
# Retry a previously failed report_status without re-downloading.
1033-
# This handles the case where the config was applied successfully
1034-
# but report_status failed due to a transient server error.
1051+
# Only do this after a confirmed no-change result (exit 0); other
1052+
# exit codes indicate checksum fetch failures and the pending
1053+
# applied/error status could no longer be accurate.
10351054
retry_pending_report
10361055
fi
10371056
env -i ACTION="end-of-cycle" /sbin/hotplug-call openwisp

0 commit comments

Comments
 (0)