Repository navigation
Fix terrain data loss when the blackbox SD card is full - #12208
MartinovEm wants to merge 1 commit into
Conversation
When the device fills (on SD, the pre-allocated freefile has worn below one supercluster), the first failed write sets the filesystem full flag: blackbox goes STOPPED and processBlackbox() skips blackboxUpdate() from then on. blackboxUpdate() is the only place the shared SD access is granted, so a terrain request stayed pending forever and terrain block reads starved for the rest of the flight. requestToSdCardAccess() now reports the card free when the device is full, the same way it does when blackbox is not using the SD card.
|
ⓘ Qodo reviews are paused because the subscription is no longer active. Ask your workspace admin to reactivate the subscription to resume reviews. Manage billing |
PR Summary by QodoKeep terrain SD access available when blackbox storage is full
AI Description
Diagram
High-Level Assessment
Files changed (1)
|
Code Review by Qodo🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)
Great, no issues found!Qodo reviewed your code and found no material issues that require reviewTip of the day💡 Did you know, you can hide the parts of a finding you never read, like the evidence or the agent prompt |
|
RAM / Flash usage vs. base commit
See RAM/flash optimization guide for techniques to reduce usage. |
|
Test firmware build ready — commit Download firmware for PR #12208 251 targets built. Find your board's
|
|
the fix is not fully correct, the main problem is that blackbox stops for some reasson then blackbox does not release lock. So right aproach is investigate all ways how blackbox could stop, and release lock. |
|
This should have been handled by #12150 Let me know if you can reproduce an issue after 12150 |
|
@error414 thanks for taking a look. A stopped blackbox still hands the card over fine, STOPPED is in the allowed list of the grant block, and the bench logs show that working before arming. Terrain only starves when blackboxUpdate stops being called at all, and that happens in processBlackbox once the device reports full. The early return here covers exactly that path. I agree a wider audit of every way blackbox can stop would be even better. @sensei-hacker the wraparound in #12150 makes a full card much rarer. I think one corner stays open though, for two reasons. First, #12150 went into next-10.1 and maintenance-10.x has none of it, so 10.0 today still reproduces the dead terrain. That is what the A/B on my bench shows on the current head b006087. Second, the full state still exists after #12150. When the card is full and the logs folder has nothing left to evict, wraparound gives up and sets wraparoundGaveUp ( inav/src/main/blackbox/blackbox_io.c Lines 622 to 625 in 345233c So I see the two changes working together rather than competing. #12150 makes the full state rare, this one line makes terrain survive it when it happens anyway. Could it be reconsidered for maintenance-10.x, or is a backport of #12150 planned for 10.0? I can adjust the PR if needed. |
|
Tested on the #12150 code itself tonight, built from 345233c, same bench, same worn out 32 KB FREESPAC.E, and the logs folder on the card empty so there is nothing to evict. Same result as on maintenance-10.x. One block read works before arming, then the terrain SD request right after arming is never granted for the rest of the flight and Terrain AGL shows "---" 600 m from home. The wraparound gave up with no logs to delete, the device reported full and blackboxUpdate stopped running, the exact chain from my previous comment. The card had 26 GB free the whole time. So yes, the issue reproduces after #12150, it just needs a card with no logs to evict. The one line from this PR covers this state, bench proven on maintenance-10.x. The debug logs below are the terrain module's own debug narration pushed over MSP, first column is the listener clock, second the FC clock in seconds. Stock #12150 build (345233c), worn freefile, no logs on the card: starved right after armingmaintenance-10.x b006087 plus this PR's change, same card and freefile: healthy, 26 grants over the whole flight10.x build without the change, worn freefile: starved right after arming. That build predates b006087 and also carries my open MSP PR #12141, which touches nothing in blackbox or SD |
|
@sensei-hacker Retested with the whole next-10.1 head, built today from d2d5fe3, on a second freshly formatted 8 GB card holding only two terrain tiles and the worn 32 KB FREESPAC.E, no logs folder. Same signature as both earlier runs. The home tile reads fine before arming, then one terrain SD request right after arming is never granted for the rest of the flight, Terrain AGL shows "---" and Terrain AGL Hold refuses with "TERRAIN NOT READY". A raw image of that card, taken right before the flight, is here: https://drive.google.com/file/d/1QI1jiqUxvZ_21lJjCml-zDZ9BnnalTt5/view?usp=sharing (7.28 GB partition image zipped to 29 MB, the card is zero filled apart from the two tiles and the freefile, so it can be written to any 8 GB or larger card to replay this). next-10.1 head (d2d5fe3), fresh 8 GB card, worn freefile, no logs: starved right after arming
|
|
Code review by qodo was updated up to the latest commit 0504b7a |
|
found it, it's not easy to reproduce at all. Steps how to reproduce:
In this case because FREESPACE.E is not zero, and there are no files to realocate, blackabox detect "sdCardIsFull" and terrain never gets the lock. Not sure how to fix it, because SD card is not in valid state, user should not delete log file without deleteing FREESPACE.E file. My proposal is to fix terrain locks (I already prepared). Terrain functionality would work and blackbox not. If SD card would be in this state before sensei changes, BB would not work also. So just fix terrain to work with blocked BB seems like correct change. |
|
Your steps match what I saw. One note, the difficulty is specific to next-10.1. On maintenance-10.x there is no eviction at all, so no special card state is needed, a worn freefile starves terrain right after arming even with logs present, that was the first A/B here. Tested your #12211 CI build b222eb5 on the breaker card and it works, terrain stayed healthy for the whole flight, 23 grants, 7 complete block reads, zero failures, and the debug log has zero pending waits, the request now gets granted inline. The log is below. So #12211 covers next-10.1, and this one line covers maintenance-10.x, where 10.0 ships from. If both land, the next merge into next-10.1 simply keeps the #12211 version of that block. PR #12211 CI build b222eb5, worn freefile, no logs on the card: terrain healthy all flight |

What this fixes
On a build with terrain and SD card blackbox, terrain data can die right after arming and stay dead for the whole flight. The OSD Terrain AGL element shows "---" away from home, Terrain AGL Hold refuses to engage with "TERRAIN NOT READY", and a hold that was already engaged freezes with "TERRAIN LOST - ALT FROZEN" once the plane leaves the cached area.
Why it happens
Blackbox pre-allocates its log space in FREESPAC.E. Closing a log gives nothing back and boot reuses the file as it is (afatfs_freeFileCreated in asyncfatfs.c), so months of normal logging wear it down. #12056 covers exactly that wear.
Once the freefile is smaller than one supercluster, the first log write after arming marks the filesystem full (afatfs.filesystemFull in asyncfatfs.c). Blackbox goes to STOPPED, and processBlackbox in fc_core.c stops calling blackboxUpdate while the device is full. blackboxUpdate is the only place that grants the shared SD access to terrain. So a terrain request stays pending forever, every new block read waits for a card it will never get, and only blocks cached before arming keep working.
The fix
requestToSdCardAccess now reports the card free when the blackbox device is full, the same way it already does when blackbox does not use the SD card. Blackbox in that state writes nothing until reboot, so the card really is free. The grant path for a healthy blackbox is unchanged, and builds without USE_TERRAIN are not affected at all, since the whole access state machine only exists under USE_TERRAIN.
How it was tested
Bench HITL on an IFLIGHT_BLITZ_H7_WING with an SDIO card and X-Plane, using a real worn 32 KB FREESPAC.E placed on a card with valid terrain tiles. Without this change the terrain debug log shows one SD access request right after arming that is never granted for the rest of the flight, and the OSD behaves as described above. With this change, on the same card, freefile and flight profile, the log shows 26 grants spread over the whole flight, 8 complete block reads and no failures, and terrain stays healthy everywhere. Logs available on request.
Relation to open PRs
This complements the freefile work by @MrScothh. #12056 makes the freefile regrow at boot, #12058 and #12059 hand the card over while blackbox is paused. None of them changes the grant path in the armed and device-full state, which is how the wear actually shows up in flight, and this PR covers only that case. It would be good to have this in 10.0, since terrain altitude data ships there and any terrain plus SD blackbox setup will reach a worn freefile sooner or later. Thanks to @MrScothh for mapping this whole area and to @error414 for the terrain data layer.