From 53e423c8e9326cc7a85418586acf577656dd2a1f Mon Sep 17 00:00:00 2001 From: dktue <2878623+dktue@users.noreply.github.com> Date: Thu, 29 Oct 2026 10:19:04 +0100 Subject: [PATCH] fix: clean error instead of std::terminate when writing tiles fails (#824) --- src/mbtiles.cpp | 17 +++++++++++--- src/tile_worker.cpp | 14 ++++++++--- src/tilemaker.cpp | 57 ++++++++++++++++++++++++++++++++++----------- 3 files changed, 68 insertions(+), 20 deletions(-) diff --git a/src/mbtiles.cpp b/src/mbtiles.cpp index c056d3a6..43bf5fd5 100644 --- a/src/mbtiles.cpp +++ b/src/mbtiles.cpp @@ -12,7 +12,14 @@ MBTiles::MBTiles(): {} MBTiles::~MBTiles() { - if (db && inTransaction) db << "COMMIT;"; // commit all the changes if open + // commit all the changes if open (closeForWriting normally does this, and reports errors) + if (db && inTransaction) { + try { + db << "COMMIT;"; + } catch (const std::exception &e) { + cerr << "Couldn't commit .mbtiles: " << e.what() << endl; + } + } } // ---- Write .mbtiles @@ -81,10 +88,10 @@ void MBTiles::flushPendingStatements() { void MBTiles::saveTile(int zoom, int x, int y, string *data, bool isMerge) { // If the lock is available, write directly to SQLite. - if (m.try_lock()) { + std::unique_lock dbLock(m, std::try_to_lock); + if (dbLock.owns_lock()) { insertOrReplace(zoom, x, y, *data, isMerge); flushPendingStatements(); - m.unlock(); } else { // Else buffer the write for later, copying its binary blob. const std::lock_guard lock(pendingStatementsMutex); @@ -96,6 +103,10 @@ void MBTiles::closeForWriting() { flushPendingStatements(); preparedStatements[0].used(true); preparedStatements[1].used(true); + if (inTransaction) { + inTransaction = false; + db << "COMMIT;"; + } } // ---- Read mbtiles diff --git a/src/tile_worker.cpp b/src/tile_worker.cpp index 4f2f21d4..7d5c4416 100644 --- a/src/tile_worker.cpp +++ b/src/tile_worker.cpp @@ -425,8 +425,12 @@ void ProcessObjects( // Append successive linestrings, then reorder afterwards while (jt<(ooSameLayerEnd-1) && oo.oo.compatible((jt+1)->oo)) { jt++; - MultiLinestring to_merge = boost::get(source->buildWayGeometry(jt->oo.geomType, jt->oo.objectID, bbox)); - for (auto &ls : to_merge) boost::get(g).emplace_back(ls); + try { + MultiLinestring to_merge = boost::get(source->buildWayGeometry(jt->oo.geomType, jt->oo.objectID, bbox)); + for (auto &ls : to_merge) boost::get(g).emplace_back(ls); + } catch (std::out_of_range &err) { + if (verbose) cerr << "Error while processing geometry " << jt->oo.geomType << "," << static_cast(jt->oo.objectID) <<"," << err.what() << endl; + } } MultiLinestring reordered; ReorderMultiLinestring(boost::get(g), reordered); @@ -438,7 +442,11 @@ void ProcessObjects( std::vector mps; while (jt<(ooSameLayerEnd-1) && oo.oo.compatible((jt+1)->oo)) { jt++; - mps.emplace_back( boost::get(source->buildWayGeometry(jt->oo.geomType, jt->oo.objectID, bbox)) ); + try { + mps.emplace_back( boost::get(source->buildWayGeometry(jt->oo.geomType, jt->oo.objectID, bbox)) ); + } catch (std::out_of_range &err) { + if (verbose) cerr << "Error while processing geometry " << jt->oo.geomType << "," << static_cast(jt->oo.objectID) <<"," << err.what() << endl; + } } if (!mps.empty()) { mps.emplace_back(boost::get(g)); diff --git a/src/tilemaker.cpp b/src/tilemaker.cpp index 7a7ae1e5..dd1ccc64 100644 --- a/src/tilemaker.cpp +++ b/src/tilemaker.cpp @@ -503,6 +503,17 @@ int main(const int argc, const char* argv[]) { sortTileCoordinates(config.baseZoom, options.threadNum, tileCoordinates); std::atomic batchCounter(0); + // First error from a tile-writing worker; reported after pool.join() + // (an exception escaping a pool thread would call std::terminate) + std::atomic tileFailed(false); + std::string tileError; + std::mutex tileErrorMutex; + auto recordTileError = [&](unsigned int zoom, TileCoordinates coords, const std::string& what) { + const std::lock_guard lock(tileErrorMutex); + if (tileFailed) return; + tileError = "z" + std::to_string(zoom) + "/" + std::to_string(coords.x) + "/" + std::to_string(coords.y) + ": " + what; + tileFailed = true; + }; const uint cutZoom = std::max(12, config.startZoom); std::size_t batchSize = 0; for(std::size_t startIndex = 0; startIndex < tileCoordinates.size(); startIndex += batchSize) { @@ -530,13 +541,14 @@ int main(const int argc, const char* argv[]) { batchSize++; } - boost::asio::post(pool, [=, &tileCoordinates, &pool, &sharedData, &sources, &attributeStore, &io_mutex, &tilesWritten, &lastTilesWritten, &batchCounter]() { + boost::asio::post(pool, [=, &tileCoordinates, &pool, &sharedData, &sources, &attributeStore, &io_mutex, &tilesWritten, &lastTilesWritten, &batchCounter, &tileFailed, &recordTileError]() { clipCacheBatchId = ++batchCounter; // A single-tile batch has no descendants to reuse its clips. clipCacheNoStore = batchSize == 1; std::vector tileTimings; std::size_t endIndex = std::min(tileCoordinates.size(), startIndex + batchSize); for(std::size_t i = startIndex; i < endIndex; ++i) { + if (tileFailed) return; unsigned int zoom = tileCoordinates[i].first; TileCoordinates coords = tileCoordinates[i].second; @@ -546,11 +558,19 @@ int main(const int argc, const char* argv[]) { clock_gettime(CLOCK_MONOTONIC, &start); #endif - std::vector> data; - for (auto source : sources) { - data.emplace_back(source->getObjectsForTile(sortOrders, zoom, coords)); + try { + std::vector> data; + for (auto source : sources) { + data.emplace_back(source->getObjectsForTile(sortOrders, zoom, coords)); + } + outputProc(sharedData, sources, attributeStore, data, coords, zoom); + } catch (const std::exception& e) { + recordTileError(zoom, coords, e.what()); + return; + } catch (...) { + recordTileError(zoom, coords, "unknown exception"); + return; } - outputProc(sharedData, sources, attributeStore, data, coords, zoom); #ifdef CLOCK_MONOTONIC if (options.logTileTimings) { @@ -593,18 +613,27 @@ int main(const int argc, const char* argv[]) { } // Wait for all tasks in the pool to complete. pool.join(); + if (tileFailed) { + cerr << endl << "Error writing tile " << tileError << endl; + return -1; + } // ---- Close tileset - if (options.outputMode == OptionsParser::OutputMode::MBTiles) { - sharedData.writeMBTilesMetadata(jsonConfig); - sharedData.mbtiles.closeForWriting(); - } else if (options.outputMode == OptionsParser::OutputMode::PMTiles) { - sharedData.writePMTilesBounds(); - std::string metadata = sharedData.pmTilesMetadata(jsonConfig); - sharedData.pmtiles.close(metadata); - } else { - sharedData.writeFileMetadata(jsonConfig); + try { + if (options.outputMode == OptionsParser::OutputMode::MBTiles) { + sharedData.writeMBTilesMetadata(jsonConfig); + sharedData.mbtiles.closeForWriting(); + } else if (options.outputMode == OptionsParser::OutputMode::PMTiles) { + sharedData.writePMTilesBounds(); + std::string metadata = sharedData.pmTilesMetadata(jsonConfig); + sharedData.pmtiles.close(metadata); + } else { + sharedData.writeFileMetadata(jsonConfig); + } + } catch (const std::exception& e) { + cerr << endl << "Error writing " << sharedData.outputFile << ": " << e.what() << endl; + return -1; } #ifndef _MSC_VER