Skip to content

Fix terrain data loss when the blackbox SD card is full - #12208

Open
MartinovEm wants to merge 1 commit into
iNavFlight:maintenance-10.xfrom
MartinovEm:fix/terrain-sd-access-blackbox-full
Open

MartinovEm wants to merge 1 commit into
iNavFlight:maintenance-10.xfrom
MartinovEm:fix/terrain-sd-access-blackbox-full

Conversation

@MartinovEm

Copy link
Copy Markdown
Contributor

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.

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-code-review

Copy link
Copy Markdown
Contributor

ⓘ Qodo reviews are paused because the subscription is no longer active. Ask your workspace admin to reactivate the subscription to resume reviews. Manage billing

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

PR Summary by Qodo

Keep terrain SD access available when blackbox storage is full

🐞 Bug fix 🕐 Less than 10 minutes

Grey Divider

AI Description

• Prevent full blackbox storage from leaving terrain SD access requests pending indefinitely.
• Preserve the existing access-grant path while blackbox logging remains available.
Diagram

graph TD
  T["Terrain IO"] --> C{"Access check"} -->|full or unused| F["Immediate access"] --> I{"Card idle?"} --> R["Tile read"]
  C -->|logging active| P["Pending request"] --> U["Blackbox update"] --> I
Loading
High-Level Assessment

The localized access check fits the existing ownership model: once a full device prevents further blackbox updates, terrain need not wait for a grant that cannot arrive. Changing the flight-loop or grant state machine would add complexity without addressing another case in this PR.

Files changed (1) +3 / -1

Bug fix (1) +3 / -1
blackbox.cAllow terrain SD access when the blackbox device is full +3/-1

Allow terrain SD access when the blackbox device is full

• requestToSdCardAccess() now returns access immediately if the blackbox device is full, rather than leaving terrain waiting for blackboxUpdate() to grant a request. The existing grant path for an available SD logging device remains unchanged; this code is guarded by USE_TERRAIN.

src/main/blackbox/blackbox.c

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

Tip of the day
💡 Did you know, you can hide the parts of a finding you never read, like the evidence or the agent prompt

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Previous reviews

Review updated until commit 0504b7a ⚖️ Balanced

Results up to commit 0504b7a


🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

Qodo Logo

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

RAM / Flash usage vs. base commit b006087 — commit 0504b7a

Target Flash Δ RAM Δ
MATEKF405 ±0 B (±0.00%) CCM: ±0 B (±0.00%)
RAM: ±0 B (±0.00%)
MATEKF722 ±0 B (±0.00%) ITCM_RAM: ±0 B (±0.00%)
RAM: ±0 B (±0.00%)
TCM: ±0 B (±0.00%)
MATEKF765 +16 B (+0.00%) DTCM_RAM: ±0 B (±0.00%)
SRAM1: ±0 B (±0.00%)
MATEKH743 +16 B (+0.00%) D2_RAM: ±0 B (±0.00%)
DTCM_RAM: ±0 B (±0.00%)
ITCM_RAM: ±0 B (±0.00%)
RAM: ±0 B (±0.00%)

See RAM/flash optimization guide for techniques to reduce usage.

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

Test firmware build ready — commit 0504b7a

Download firmware for PR #12208

251 targets built. Find your board's .hex file by name on that page (e.g. MATEKF405SE.hex). Files are individually downloadable — no GitHub login required.

Development build for testing only. Use Full Chip Erase when flashing.

@error414

error414 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

the fix is not fully correct, the main problem is that blackbox stops for some reasson then blackbox does not release lock.

https://github.com/MartinovEm/inav/blob/0504b7affc185216d08cd1adf1640102d2bfc3fd/src/main/blackbox/blackbox.c#L2484

So right aproach is investigate all ways how blackbox could stop, and release lock.

@sensei-hacker

Copy link
Copy Markdown
Member

This should have been handled by #12150

Let me know if you can reproduce an issue after 12150

@MartinovEm

MartinovEm commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor Author

@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 (

if (blackboxSDCard.oldestLogFirstCluster == UINT32_MAX || blackboxSDCard.oldestLogFirstCluster == 0) {
// Nothing to reclaim: no logs
blackboxSDCard.wraparoundGaveUp = true;
blackboxSDCard.state = BLACKBOX_SDCARD_READY_TO_CREATE_LOG;
), isBlackboxDeviceFull() then returns true (blackbox_io.c line 738), processBlackbox stops calling blackboxUpdate (fc_core.c line 963), and the terrain grant never runs again. The flag is only cleared inside the begin log path that blackboxUpdate itself drives, so it stays stuck until reboot. A terrain card packed with tiles where the pilot cleans out old logs is exactly that situation. This is traced on the 345233c code, and I can reproduce it on the HITL bench if that helps. The PR description already went through the open freefile work, and after your comment I read #12150 as well, the scenario above is what it leaves open.

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.

@MartinovEm

Copy link
Copy Markdown
Contributor Author

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 arming
listening on COM14 for MSP_DEBUGMSG (terrain log)... Ctrl+C to stop
[  234.7s] [   247.109] Simulator enabled
[  234.7s] [   247.215] TERRAIN SD LOCK REQUESTED, PENDING
[  234.7s] [   247.235] TERRAIN SD LOCK ACQUIRED
[  234.7s] [   247.235] TERRAIN IDLE -> CHANGE_DIR
[  234.7s] [   247.255] TERRAIN CHANGE_DIR, ALREADY ROOT -> OPEN_FILE
[  234.7s] [   247.256] Font metadata version Y: 3 (1024 chars)
[  234.7s] [   247.275] TERRAIN OPEN_FILE N42E023.TER -> OPEN_FILE_PENDING
[  234.7s] [   247.275] TERRAIN OPEN CALLBACK, FILE OK -> SEEK
[  234.7s] [   247.275] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  234.7s] [   247.295] TERRAIN SD LOCK REQUESTED, PENDING
[  234.9s] [   247.315] TERRAIN SD LOCK ACQUIRED
[  234.9s] [   247.315] TERRAIN SEEK TO POSITION 107,43
[  234.9s] [   247.315] TERRAIN SEEK OK -> READ
[  234.9s] [   247.335] TERRAIN READING DATA 0/1151
[  234.9s] [   247.355] TERRAIN READING DATA 512/1151
[  234.9s] [   247.355] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  234.9s] [   247.375] TERRAIN SD LOCK REQUESTED, PENDING
[  234.9s] [   247.395] TERRAIN SD LOCK ACQUIRED
[  234.9s] [   247.395] TERRAIN READING DATA 1024/1151
[  234.9s] [   247.395] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  234.9s] [   247.415] TERRAIN SD LOCK REQUESTED, PENDING
[  234.9s] [   247.435] TERRAIN SD LOCK ACQUIRED
[  234.9s] [   247.435] TERRAIN READING DATA 1151/1151
[  234.9s] [   247.435] TERRAIN READ FINISHED -> IDLE (file kept open)
[  234.9s] [   247.435] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  236.5s] [   249.110] Gravity calibration complete (981)
[  236.7s] [   249.159] Barometer calibration complete (53305)
[  254.2s] [   266.622] TERRAIN SD LOCK REQUESTED, PENDING
maintenance-10.x b006087 plus this PR's change, same card and freefile: healthy, 26 grants over the whole flight
listening on COM14 for MSP_DEBUGMSG (terrain log)... Ctrl+C to stop
[  491.9s] [   503.513] Simulator enabled
[  491.9s] [   503.626] TERRAIN SD LOCK REQUESTED, PENDING
[  491.9s] [   503.646] TERRAIN SD LOCK ACQUIRED
[  491.9s] [   503.646] TERRAIN IDLE -> CHANGE_DIR
[  491.9s] [   503.666] TERRAIN CHANGE_DIR, ALREADY ROOT -> OPEN_FILE
[  492.1s] [   503.686] TERRAIN OPEN_FILE N42E023.TER -> OPEN_FILE_PENDING
[  492.1s] [   503.686] TERRAIN OPEN CALLBACK, FILE OK -> SEEK
[  492.1s] [   503.686] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  492.1s] [   503.706] TERRAIN SD LOCK REQUESTED, PENDING
[  492.1s] [   503.726] TERRAIN SD LOCK ACQUIRED
[  492.1s] [   503.726] TERRAIN SEEK TO POSITION 107,43
[  492.1s] [   503.726] TERRAIN SEEK OK -> READ
[  492.1s] [   503.746] TERRAIN READING DATA 0/1151
[  492.1s] [   503.766] TERRAIN READING DATA 512/1151
[  492.1s] [   503.766] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  492.1s] [   503.786] TERRAIN SD LOCK REQUESTED, PENDING
[  492.1s] [   503.806] TERRAIN SD LOCK ACQUIRED
[  492.1s] [   503.806] TERRAIN READING DATA 1024/1151
[  492.1s] [   503.806] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  492.1s] [   503.826] TERRAIN SD LOCK REQUESTED, PENDING
[  492.1s] [   503.846] TERRAIN SD LOCK ACQUIRED
[  492.1s] [   503.846] TERRAIN READING DATA 1151/1151
[  492.1s] [   503.846] TERRAIN READ FINISHED -> IDLE (file kept open)
[  492.1s] [   503.846] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  492.3s] [   503.960] Font metadata version Y: 3 (1024 chars)
[  494.0s] [   505.514] Gravity calibration complete (981)
[  494.0s] [   505.514] Barometer calibration complete (53305)
[  504.1s] [   515.672] TERRAIN SD LOCK ACQUIRED
[  504.1s] [   515.672] TERRAIN IDLE, FILE ALREADY OPEN -> SEEK
[  504.1s] [   515.692] TERRAIN SEEK TO POSITION 107,42
[  504.1s] [   515.692] TERRAIN SEEK OK -> READ
[  504.1s] [   515.712] TERRAIN READING DATA 0/1151
[  504.1s] [   515.732] TERRAIN READING DATA 512/1151
[  504.1s] [   515.732] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  504.1s] [   515.752] TERRAIN SD LOCK ACQUIRED
[  504.1s] [   515.752] TERRAIN READING DATA 1024/1151
[  504.1s] [   515.752] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  504.1s] [   515.772] TERRAIN SD LOCK ACQUIRED
[  504.1s] [   515.772] TERRAIN READING DATA 1151/1151
[  504.1s] [   515.772] TERRAIN READ FINISHED -> IDLE (file kept open)
[  504.1s] [   515.772] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  526.3s] [   537.920] TERRAIN SD LOCK ACQUIRED
[  526.3s] [   537.920] TERRAIN IDLE, FILE ALREADY OPEN -> SEEK
[  526.3s] [   537.940] TERRAIN SEEK TO POSITION 107,41
[  526.3s] [   537.940] TERRAIN SEEK OK -> READ
[  526.3s] [   537.960] TERRAIN READING DATA 0/1151
[  526.3s] [   537.980] TERRAIN READING DATA 512/1151
[  526.3s] [   537.980] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  526.3s] [   538.000] TERRAIN SD LOCK ACQUIRED
[  526.3s] [   538.000] TERRAIN READING DATA 1024/1151
[  526.3s] [   538.000] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  526.3s] [   538.020] TERRAIN SD LOCK ACQUIRED
[  526.3s] [   538.020] TERRAIN READING DATA 1151/1151
[  526.3s] [   538.020] TERRAIN READ FINISHED -> IDLE (file kept open)
[  526.3s] [   538.020] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  544.6s] [   556.166] TERRAIN SD LOCK ACQUIRED
[  544.6s] [   556.166] TERRAIN IDLE, FILE ALREADY OPEN -> SEEK
[  544.6s] [   556.186] TERRAIN SEEK TO POSITION 107,40
[  544.6s] [   556.186] TERRAIN SEEK OK -> READ
[  544.6s] [   556.206] TERRAIN READING DATA 0/1151
[  544.6s] [   556.226] TERRAIN READING DATA 512/1151
[  544.6s] [   556.226] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  544.6s] [   556.246] TERRAIN SD LOCK ACQUIRED
[  544.6s] [   556.246] TERRAIN READING DATA 1024/1151
[  544.6s] [   556.246] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  544.6s] [   556.266] TERRAIN SD LOCK ACQUIRED
[  544.6s] [   556.266] TERRAIN READING DATA 1151/1151
[  544.6s] [   556.266] TERRAIN READ FINISHED -> IDLE (file kept open)
[  544.6s] [   556.266] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  559.9s] [   571.534] TERRAIN SD LOCK ACQUIRED
[  559.9s] [   571.534] TERRAIN IDLE, FILE ALREADY OPEN -> SEEK
[  559.9s] [   571.554] TERRAIN SEEK TO POSITION 107,39
[  559.9s] [   571.554] TERRAIN SEEK OK -> READ
[  559.9s] [   571.574] TERRAIN READING DATA 0/1151
[  559.9s] [   571.594] TERRAIN READING DATA 512/1151
[  559.9s] [   571.594] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  560.1s] [   571.614] TERRAIN SD LOCK ACQUIRED
[  560.1s] [   571.614] TERRAIN READING DATA 1024/1151
[  560.1s] [   571.614] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  560.1s] [   571.634] TERRAIN SD LOCK ACQUIRED
[  560.1s] [   571.634] TERRAIN READING DATA 1151/1151
[  560.1s] [   571.634] TERRAIN READ FINISHED -> IDLE (file kept open)
[  560.1s] [   571.634] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  560.1s] [   571.654] TERRAIN SD LOCK ACQUIRED
[  560.1s] [   571.654] TERRAIN IDLE, FILE ALREADY OPEN -> SEEK
[  560.1s] [   571.674] TERRAIN SEEK TO POSITION 107,38
[  560.1s] [   571.674] TERRAIN SEEK OK -> READ
[  560.1s] [   571.694] TERRAIN READING DATA 0/1151
[  560.1s] [   571.714] TERRAIN READING DATA 512/1151
[  560.1s] [   571.714] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  560.1s] [   571.734] TERRAIN SD LOCK ACQUIRED
[  560.1s] [   571.734] TERRAIN READING DATA 1024/1151
[  560.1s] [   571.734] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  560.1s] [   571.754] TERRAIN SD LOCK ACQUIRED
[  560.1s] [   571.754] TERRAIN READING DATA 1151/1151
[  560.1s] [   571.754] TERRAIN READ FINISHED -> IDLE (file kept open)
[  560.1s] [   571.754] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  578.8s] [   590.441] TERRAIN SD LOCK ACQUIRED
[  578.8s] [   590.441] TERRAIN IDLE, FILE ALREADY OPEN -> SEEK
[  578.8s] [   590.461] TERRAIN SEEK TO POSITION 107,37
[  578.8s] [   590.461] TERRAIN SEEK OK -> READ
[  578.8s] [   590.481] TERRAIN READING DATA 0/1151
[  578.8s] [   590.501] TERRAIN READING DATA 512/1151
[  578.8s] [   590.501] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  579.0s] [   590.521] TERRAIN SD LOCK ACQUIRED
[  579.0s] [   590.521] TERRAIN READING DATA 1024/1151
[  579.0s] [   590.521] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  579.0s] [   590.541] TERRAIN SD LOCK ACQUIRED
[  579.0s] [   590.541] TERRAIN READING DATA 1151/1151
[  579.0s] [   590.541] TERRAIN READ FINISHED -> IDLE (file kept open)
[  579.0s] [   590.541] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  639.0s] [   650.545] TERRAIN IDLE TIMEOUT -> CLOSE
[  639.0s] [   650.565] TERRAIN CLOSE -> CLOSE_PENDING
[  639.0s] [   650.565] TERRAIN CLOSE CALLBACK, FILE CLOSED
[  639.0s] [   650.565] TERRAIN RESET STATE -> IDLE
[  653.8s] [   665.491] TERRAIN SD LOCK ACQUIRED
[  653.8s] [   665.491] TERRAIN IDLE -> CHANGE_DIR
[  653.8s] [   665.511] TERRAIN CHANGE_DIR, CHANGING TO ROOT
[  653.8s] [   665.511] TERRAIN CHANGE_DIR OK -> OPEN_FILE
[  653.8s] [   665.531] TERRAIN OPEN_FILE N42E023.TER -> OPEN_FILE_PENDING
[  653.8s] [   665.531] TERRAIN OPEN CALLBACK, FILE OK -> SEEK
[  653.8s] [   665.531] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  654.0s] [   665.551] TERRAIN SD LOCK ACQUIRED
[  654.0s] [   665.551] TERRAIN SEEK TO POSITION 107,44
[  654.0s] [   665.551] TERRAIN SEEK OK -> READ
[  654.0s] [   665.571] TERRAIN READING DATA 0/1151
[  654.0s] [   665.591] TERRAIN READING DATA 512/1151
[  654.0s] [   665.591] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  654.0s] [   665.611] TERRAIN SD LOCK ACQUIRED
[  654.0s] [   665.611] TERRAIN READING DATA 1024/1151
[  654.0s] [   665.611] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  654.0s] [   665.631] TERRAIN SD LOCK ACQUIRED
[  654.0s] [   665.631] TERRAIN READING DATA 1151/1151
[  654.0s] [   665.631] TERRAIN READ FINISHED -> IDLE (file kept open)
[  654.0s] [   665.631] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  714.0s] [   725.648] TERRAIN IDLE TIMEOUT -> CLOSE
[  714.0s] [   725.668] TERRAIN CLOSE -> CLOSE_PENDING
[  714.0s] [   725.668] TERRAIN CLOSE CALLBACK, FILE CLOSED
[  714.0s] [   725.668] TERRAIN RESET STATE -> IDLE
10.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
listening on COM14 for MSP_DEBUGMSG (terrain log)... Ctrl+C to stop
[   84.6s] [    97.962] Simulator enabled
[   84.6s] [    98.008] TERRAIN SD LOCK REQUESTED, PENDING
[   84.6s] [    98.028] TERRAIN SD LOCK ACQUIRED
[   84.6s] [    98.028] TERRAIN IDLE -> CHANGE_DIR
[   84.8s] [    98.048] TERRAIN CHANGE_DIR, ALREADY ROOT -> OPEN_FILE
[   84.8s] [    98.050] Font metadata version Y: 3 (1024 chars)
[   84.8s] [    98.068] TERRAIN OPEN_FILE N42E023.TER -> OPEN_FILE_PENDING
[   84.8s] [    98.068] TERRAIN OPEN CALLBACK, FILE OK -> SEEK
[   84.8s] [    98.068] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[   84.8s] [    98.088] TERRAIN SD LOCK REQUESTED, PENDING
[   84.8s] [    98.108] TERRAIN SD LOCK ACQUIRED
[   84.8s] [    98.108] TERRAIN SEEK TO POSITION 107,43
[   84.8s] [    98.108] TERRAIN SEEK OK -> READ
[   84.8s] [    98.128] TERRAIN READING DATA 0/1151
[   84.8s] [    98.148] TERRAIN READING DATA 512/1151
[   84.8s] [    98.148] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[   84.8s] [    98.168] TERRAIN SD LOCK REQUESTED, PENDING
[   84.8s] [    98.188] TERRAIN SD LOCK ACQUIRED
[   84.8s] [    98.188] TERRAIN READING DATA 1024/1151
[   84.8s] [    98.188] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[   84.8s] [    98.208] TERRAIN SD LOCK REQUESTED, PENDING
[   84.8s] [    98.228] TERRAIN SD LOCK ACQUIRED
[   84.8s] [    98.228] TERRAIN READING DATA 1151/1151
[   84.8s] [    98.229] TERRAIN READ FINISHED -> IDLE (file kept open)
[   84.8s] [    98.229] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[   86.6s] [    99.963] Gravity calibration complete (981)
[   86.6s] [   100.012] Barometer calibration complete (53305)
[   98.2s] [   111.633] TERRAIN SD LOCK REQUESTED, PENDING

@MartinovEm

MartinovEm commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor Author

@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
listening on COM14 for MSP_DEBUGMSG (terrain log)... Ctrl+C to stop
[   63.1s] [    74.046] Simulator enabled
[   63.4s] [    74.213] TERRAIN SD LOCK REQUESTED, PENDING
[   63.4s] [    74.233] TERRAIN SD LOCK ACQUIRED
[   63.4s] [    74.233] TERRAIN IDLE -> CHANGE_DIR
[   63.4s] [    74.253] TERRAIN CHANGE_DIR, ALREADY ROOT -> OPEN_FILE
[   63.4s] [    74.273] TERRAIN OPEN_FILE N42E023.TER -> OPEN_FILE_PENDING
[   63.4s] [    74.273] TERRAIN OPEN CALLBACK, FILE OK -> SEEK
[   63.4s] [    74.273] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[   63.4s] [    74.293] TERRAIN SD LOCK REQUESTED, PENDING
[   63.4s] [    74.313] TERRAIN SD LOCK ACQUIRED
[   63.4s] [    74.313] TERRAIN SEEK TO POSITION 107,43
[   63.4s] [    74.313] TERRAIN SEEK OK -> READ
[   63.4s] [    74.333] TERRAIN READING DATA 0/1151
[   63.6s] [    74.353] TERRAIN READING DATA 512/1151
[   63.6s] [    74.353] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[   63.6s] [    74.373] TERRAIN SD LOCK REQUESTED, PENDING
[   63.6s] [    74.393] TERRAIN SD LOCK ACQUIRED
[   63.6s] [    74.393] TERRAIN READING DATA 1024/1151
[   63.6s] [    74.393] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[   63.6s] [    74.413] TERRAIN SD LOCK REQUESTED, PENDING
[   63.6s] [    74.433] TERRAIN SD LOCK ACQUIRED
[   63.6s] [    74.433] TERRAIN READING DATA 1151/1151
[   63.6s] [    74.433] TERRAIN READ FINISHED -> IDLE (file kept open)
[   63.6s] [    74.433] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[   63.8s] [    74.691] Font metadata version Y: 3 (1024 chars)
[   65.2s] [    76.047] Barometer calibration complete (53305)
[   65.2s] [    76.047] Gravity calibration complete (981)
[   78.2s] [    89.177] TERRAIN SD LOCK REQUESTED, PENDING
image

@sensei-hacker sensei-hacker reopened this Nov 7, 2026
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Code review by qodo was updated up to the latest commit 0504b7a

@error414

error414 commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

found it, it's not easy to reproduce at all.

Steps how to reproduce:

  1. fly and wait till FREESPACE.E goes to 32kB size. Don't continue with next flying because RELOCATE_FREEFILE would delete first log and FREESPECE.E would resize to 0kB. FREESPACE.E must be 32kB.
  2. mount SD card and delete whole LOG folder
  3. fly again, BB does not work, terrain does not work

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.

@MartinovEm

MartinovEm commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor Author

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
listening on COM14 for MSP_DEBUGMSG (terrain log)... Ctrl+C to stop
[  472.3s] [   482.466] Simulator enabled
[  472.5s] [   482.609] TERRAIN SD LOCK ACQUIRED
[  472.5s] [   482.609] TERRAIN IDLE -> CHANGE_DIR
[  472.5s] [   482.629] TERRAIN CHANGE_DIR, ALREADY ROOT -> OPEN_FILE
[  472.5s] [   482.649] TERRAIN OPEN_FILE N42E023.TER -> OPEN_FILE_PENDING
[  472.5s] [   482.649] TERRAIN OPEN CALLBACK, FILE OK -> SEEK
[  472.5s] [   482.649] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  472.5s] [   482.669] TERRAIN SD LOCK ACQUIRED
[  472.5s] [   482.669] TERRAIN SEEK TO POSITION 107,43
[  472.5s] [   482.669] TERRAIN SEEK OK -> READ
[  472.5s] [   482.689] TERRAIN READING DATA 0/1151
[  472.5s] [   482.709] TERRAIN READING DATA 512/1151
[  472.5s] [   482.709] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  472.5s] [   482.729] TERRAIN SD LOCK ACQUIRED
[  472.5s] [   482.729] TERRAIN READING DATA 1024/1151
[  472.5s] [   482.729] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  472.5s] [   482.741] Font metadata version Y: 3 (1024 chars)
[  472.5s] [   482.749] TERRAIN SD LOCK ACQUIRED
[  472.5s] [   482.749] TERRAIN READING DATA 1151/1151
[  472.5s] [   482.749] TERRAIN READ FINISHED -> IDLE (file kept open)
[  472.5s] [   482.749] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  474.3s] [   484.467] Barometer calibration complete (53305)
[  474.3s] [   484.467] Gravity calibration complete (981)
[  488.8s] [   498.914] TERRAIN SD LOCK ACQUIRED
[  488.8s] [   498.914] TERRAIN IDLE, FILE ALREADY OPEN -> SEEK
[  488.8s] [   498.934] TERRAIN SEEK TO POSITION 107,42
[  488.8s] [   498.934] TERRAIN SEEK OK -> READ
[  488.8s] [   498.954] TERRAIN READING DATA 0/1151
[  488.8s] [   498.974] TERRAIN READING DATA 512/1151
[  488.8s] [   498.974] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  488.8s] [   498.994] TERRAIN SD LOCK ACQUIRED
[  488.8s] [   498.994] TERRAIN READING DATA 1024/1151
[  488.8s] [   498.994] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  488.8s] [   499.014] TERRAIN SD LOCK ACQUIRED
[  488.8s] [   499.014] TERRAIN READING DATA 1151/1151
[  488.8s] [   499.014] TERRAIN READ FINISHED -> IDLE (file kept open)
[  488.8s] [   499.014] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  510.4s] [   520.642] TERRAIN SD LOCK ACQUIRED
[  510.4s] [   520.642] TERRAIN IDLE, FILE ALREADY OPEN -> SEEK
[  510.4s] [   520.662] TERRAIN SEEK TO POSITION 107,41
[  510.4s] [   520.662] TERRAIN SEEK OK -> READ
[  510.4s] [   520.682] TERRAIN READING DATA 0/1151
[  510.6s] [   520.702] TERRAIN READING DATA 512/1151
[  510.6s] [   520.702] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  510.6s] [   520.722] TERRAIN SD LOCK ACQUIRED
[  510.6s] [   520.722] TERRAIN READING DATA 1024/1151
[  510.6s] [   520.722] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  510.6s] [   520.742] TERRAIN SD LOCK ACQUIRED
[  510.6s] [   520.742] TERRAIN READING DATA 1151/1151
[  510.6s] [   520.742] TERRAIN READ FINISHED -> IDLE (file kept open)
[  510.6s] [   520.742] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  528.7s] [   538.869] TERRAIN SD LOCK ACQUIRED
[  528.7s] [   538.869] TERRAIN IDLE, FILE ALREADY OPEN -> SEEK
[  528.7s] [   538.889] TERRAIN SEEK TO POSITION 107,40
[  528.7s] [   538.889] TERRAIN SEEK OK -> READ
[  528.7s] [   538.909] TERRAIN READING DATA 0/1151
[  528.7s] [   538.929] TERRAIN READING DATA 512/1151
[  528.7s] [   538.929] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  528.7s] [   538.949] TERRAIN SD LOCK ACQUIRED
[  528.7s] [   538.949] TERRAIN READING DATA 1024/1151
[  528.7s] [   538.949] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  528.7s] [   538.969] TERRAIN SD LOCK ACQUIRED
[  528.7s] [   538.969] TERRAIN READING DATA 1151/1151
[  528.7s] [   538.970] TERRAIN READ FINISHED -> IDLE (file kept open)
[  528.7s] [   538.970] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  547.0s] [   557.157] TERRAIN SD LOCK ACQUIRED
[  547.0s] [   557.157] TERRAIN IDLE, FILE ALREADY OPEN -> SEEK
[  547.0s] [   557.177] TERRAIN SEEK TO POSITION 107,39
[  547.0s] [   557.177] TERRAIN SEEK OK -> READ
[  547.0s] [   557.197] TERRAIN READING DATA 0/1151
[  547.0s] [   557.217] TERRAIN READING DATA 512/1151
[  547.0s] [   557.217] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  547.0s] [   557.237] TERRAIN SD LOCK ACQUIRED
[  547.0s] [   557.237] TERRAIN READING DATA 1024/1151
[  547.0s] [   557.237] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  547.0s] [   557.257] TERRAIN SD LOCK ACQUIRED
[  547.0s] [   557.257] TERRAIN READING DATA 1151/1151
[  547.0s] [   557.257] TERRAIN READ FINISHED -> IDLE (file kept open)
[  547.0s] [   557.257] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  565.3s] [   575.486] TERRAIN SD LOCK ACQUIRED
[  565.3s] [   575.486] TERRAIN IDLE, FILE ALREADY OPEN -> SEEK
[  565.3s] [   575.506] TERRAIN SEEK TO POSITION 107,38
[  565.3s] [   575.506] TERRAIN SEEK OK -> READ
[  565.3s] [   575.526] TERRAIN READING DATA 0/1151
[  565.3s] [   575.546] TERRAIN READING DATA 512/1151
[  565.3s] [   575.546] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  565.3s] [   575.566] TERRAIN SD LOCK ACQUIRED
[  565.3s] [   575.566] TERRAIN READING DATA 1024/1151
[  565.3s] [   575.566] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  565.3s] [   575.586] TERRAIN SD LOCK ACQUIRED
[  565.3s] [   575.586] TERRAIN READING DATA 1151/1151
[  565.3s] [   575.587] TERRAIN READ FINISHED -> IDLE (file kept open)
[  565.3s] [   575.587] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  625.3s] [   635.588] TERRAIN IDLE TIMEOUT -> CLOSE
[  625.3s] [   635.608] TERRAIN CLOSE -> CLOSE_PENDING
[  625.3s] [   635.608] TERRAIN CLOSE CALLBACK, FILE CLOSED
[  625.3s] [   635.608] TERRAIN RESET STATE -> IDLE
[  678.9s] [   689.189] TERRAIN SD LOCK ACQUIRED
[  678.9s] [   689.189] TERRAIN IDLE -> CHANGE_DIR
[  678.9s] [   689.209] TERRAIN CHANGE_DIR, CHANGING TO ROOT
[  678.9s] [   689.209] TERRAIN CHANGE_DIR OK -> OPEN_FILE
[  679.1s] [   689.229] TERRAIN OPEN_FILE N42E023.TER -> OPEN_FILE_PENDING
[  679.1s] [   689.230] TERRAIN OPEN CALLBACK, FILE OK -> SEEK
[  679.1s] [   689.230] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  679.1s] [   689.250] TERRAIN SD LOCK ACQUIRED
[  679.1s] [   689.250] TERRAIN SEEK TO POSITION 107,44
[  679.1s] [   689.250] TERRAIN SEEK OK -> READ
[  679.1s] [   689.270] TERRAIN READING DATA 0/1151
[  679.1s] [   689.290] TERRAIN READING DATA 512/1151
[  679.1s] [   689.290] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  679.1s] [   689.310] TERRAIN SD LOCK ACQUIRED
[  679.1s] [   689.310] TERRAIN READING DATA 1024/1151
[  679.1s] [   689.310] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  679.1s] [   689.330] TERRAIN SD LOCK ACQUIRED
[  679.1s] [   689.330] TERRAIN READING DATA 1151/1151
[  679.1s] [   689.330] TERRAIN READ FINISHED -> IDLE (file kept open)
[  679.1s] [   689.330] TERRAIN SD LOCK RELEASED (held=1 requested=0)
[  739.2s] [   749.348] TERRAIN IDLE TIMEOUT -> CLOSE
[  739.2s] [   749.368] TERRAIN CLOSE -> CLOSE_PENDING
[  739.2s] [   749.368] TERRAIN CLOSE CALLBACK, FILE CLOSED
[  739.2s] [   749.368] TERRAIN RESET STATE -> IDLE

@sensei-hacker sensei-hacker added this to the 10.0 milestone Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants