Repository navigation
Fix HillshadingTileDataSource caching corrupt tiles and reporting failed renders as success - #1317
Open
jhotadhari wants to merge 1 commit into
Open
jhotadhari wants to merge 1 commit into
jhotadhari wants to merge 1 commit into
Conversation
createTile reported QueryResult.SUCCESS in four situations where the tile had not rendered or had not been written correctly. One of them committed a truncated PNG to the tile cache, which was then served from disk on every later visit -- a permanent, tile-aligned rendering artifact that survived app restarts and cache clears. - The tile bitmap was handed to the sink via setTileImage() and only then PNG-encoded for the cache. The loader may consume and recycle the bitmap in between, so the encode raced and threw "Can't compress a recycled bitmap". Encode before handing over; a failure there now costs only the cache entry, never the tile. - An IOException during the cache write was logged and swallowed, and the tile was then reported SUCCESS and committed, writing a truncated PNG. Track the write and commit only on full success. complete() still always runs, since that is what returns the pooled buffer. - Each longitude strip is a Callable<Boolean> that caught Throwable and returned true regardless, and createTile discarded the result, so a strip that threw yielded a half-shaded tile presented and cached as a success. Strips now report failure and createTile drains them all, leaving no task pending, then reports FAILED so the tile is dropped and re-queued. - A getShadingTile failure was swallowed, after which it was indistinguishable from the legitimate "no DEM data here" case, so the cell was skipped and the tile cached with a hole. The skip is correct for genuine no-data and is unchanged; only the exception path now reports failure. Exception logging now includes the stack trace and the tile.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
createTilereportedQueryResult.SUCCESSin four cases where the tile hadnot rendered or had not been written correctly. One of them committed a
truncated PNG to the on-disk tile cache, producing a permanent rendering
artifact served from disk thereafter.
This is a deliberate behaviour change, not an opt-in addition: renders and
cache writes that previously passed silently as successes are now reported as
failures.
Fixes #1316
Motivation
See the issue for the full analysis. In short, on a hillshading layer with a
tile cache configured:
getPngEncodedData()was called on the tile bitmap aftersetTileImage()had handed it to the tile loader, which races with theloader recycling it —
IllegalStateException: Can't compress a recycled bitmap, 21 times in a few minutes of panning on device;IOExceptionduring the cache write was swallowed and the tile was stillreported
SUCCESSand committed, writing a truncated PNG that was thenserved from disk forever;
trueanyway, andcreateTilediscardedthe result, so a half-shaded tile was displayed and cached as a success;
ShadingLatStep, and each cell'stile load was wrapped in a
trywhosecatchwas swallowed, so a load thatthrew was indistinguishable from the legitimate no-data case (ocean, or a
missing
.hgtfile) at the followingshadingTile == nullcheck — the cellwas skipped as if there were simply no data there, the strip still returned
true, and the tile was displayed and cached with a hole in it.What changed
All in
vtm-hillshading/src/org/oscim/tiling/source/hills/HillshadingTileDataSource.java.createTile(MapTile, ITileDataSink, ITileCache)boolean cacheOk = false.sink.setTileImage(...),into its own
try/catch (Throwable). On failure it logs and leavespngBytes == null; the tile still renders and is still reportedSUCCESS. A cache-encode failure must not cost the tile.pngBytes != null, and setscacheOk = trueonly afteroutputStream.write(...)returns normally.The
catch (IOException)now logs the tile and does not setcacheOk.finally:cacheWriter.complete(ok && cacheOk)instead ofcomplete(ok).complete()is still called unconditionally whenever awriter was obtained, since that is what returns the pooled
ByteArrayOutputStreamtoTileCache.SilentFutureTask.get()returningnull(its behaviour when the taskthrew) is treated as a failure.
renderLatStrip(...)'sCallable<Boolean>catch (Throwable t)now logs with the stack trace(
log.log(Level.SEVERE, tile + " ...", t)rather thanlog.severe(t.toString())) and returnsfalseinstead of falling throughto
return true.renderLatStrip(...), shading-tile load (lines 225-235)catch (Exception e)aroundmHillsRenderConfig.getShadingTile(...)now logs with the stack trace andthe tile, and returns
false, so the strip drain above reportsQueryResult.FAILEDfor the tile.shadingTile == null/continuepath is unchanged on purpose:that is the legitimate no-data case (ocean, or a missing
.hgtfile),where skipping that cell is correct and unshaded sea is the desired result.
A failure here leaves a hole, not wrong shading.
Behaviour change (not a pure addition)
Unlike the other changes in this series, this is a bug fix with a deliberate
behaviour change: renders and cache writes that previously passed silently as
successes are now reported as failures.
QueryResult.FAILED,TileManager.JobCompletedEventclears the tile toNONEandTileManager.addTilere-queues it on the nextPOSITION_EVENT/CLEAR_EVENT. The area shows the parent proxy tile until then.TileManager, so a tilethat failed consistently would be retried on each position change while the
map is moving. In practice these failures are races and the retry succeeds;
after the fixes for 1–3 the failure path stopped being hit at all (see
below). Flagging it as a known property of vtm's retry model rather than
something this PR introduces.
skipped. That is a deliberate asymmetry: the rendered tile is fine.
Testing notes
Verified on a Pixel 6 Pro (LineageOS 20), SRTM3 DEM,
AdaptiveClasyHillShading,hillshading layer with a 50 MB SQLite tile cache, release build.
Before the fix, in a few minutes of panning:
plus a hard, geo-anchored vertical seam in the hillshading, reproducible at the
same tile-column boundary across sessions and surviving a full cache wipe.
After the fixes for defects 1–3, same scenario (cache wiped, two app starts,
extensive panning):
and no seams. The cache also now behaves correctly, which is the clearest
evidence that defect 1 was starving it:
176 tiles persisted across a restart, then the cache filled to the configured
budget and trimmed normally.
Those measurements cover fixes 1 and 2, which have had several days of
real-world use.
Note: the failure paths for defects 3 and 4 were not observed firing —
they are fixed as defence-in-depth against the same class of bug as 1 and 2,
not because they were the observed cause.
For defect 4 I tried to exercise it deliberately, by replacing an
.hgtfilewith a zip whose central directory advertises the correct uncompressed size
(so it passes
HgtCache.indexFile's perfect-square length check and is indexedat
axisLen = 1201) but whose deflate stream is corrupted. The read does fail—
java.util.zip.ZipException: invalid stored block lengths, ten times overtwo runs — but the exception never reaches
createTile: mapsforge'sAThreadedHillShadingcatches it internally and logs it atWARNING, sogetShadingTilesimply yields no bitmap, which is indistinguishable fromgenuine no-data. So this fix guards a path that mapsforge does not expose for
I/O failures; it still matters for anything that does escape
getShadingTile, but I could not demonstrate it firing and am not claiming ithandles corrupt DEM data.
That experiment did, however, reproduce the underlying symptom: with a freshly
emptied tile cache, the corrupt DEM produced a silently unshaded area and 128
tiles were committed to the cache four seconds after the first read failure.
master(52cf6569at time of writing).createTileneeds a tile loader, a DEM and a graphicsbackend, so a meaningful test would be an instrumentation test; happy to add
one if maintainers want it.
Checklist
master(based on52cf6569, single squashed commit, one file)vtm-hillshading:compileJava, clean rerun)