question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Opening same GPKG file/layer for write => mysterious bug

See original GitHub issue

I wrote a class to stream dataframes into GeoPackage, and by mistake it opened a file for writing twice. It would succesfully write the data, but would crash when the program was terminating.

ERROR 1: sqlite3_exec(CREATE TRIGGER "trigger_insert_feature_count_sample" AFTER INSERT ON "sample" BEGIN UPDATE gpkg_ogr_contents SET feature_count = feature_count + 1 WHERE lower(table_name) = lower('sample'); END;) failed: trigger "trigger_insert_feature_count_sample" already exists
ERROR 1: sqlite3_exec(CREATE TRIGGER "trigger_delete_feature_count_sample" AFTER DELETE ON "sample" BEGIN UPDATE gpkg_ogr_contents SET feature_count = feature_count - 1 WHERE lower(table_name) = lower('sample'); END;) failed: trigger "trigger_delete_feature_count_sample" already exists
ERROR 1: Spatial index already existing
Traceback (most recent call last):
  File "fiona/_err.pyx", line 201, in fiona._err.GDALErrCtxManager.__exit__
fiona._err.CPLE_AppDefinedError: b'Spatial index already existing'
Exception ignored in: 'fiona._shim.gdal_flush_cache'
Traceback (most recent call last):
  File "fiona/_err.pyx", line 201, in fiona._err.GDALErrCtxManager.__exit__

I thought it must have had something to do with threads created in the background. But turned out it was just that a condition was checked in a wrong way, and fiona.open ran again and again on every chunk.

Here’s a code that reproduces this.

import fiona
from collections import OrderedDict

schema = {'geometry': 'Point', 'properties': OrderedDict()}
_handler = fiona.open('sample.gpkg', 'w', driver='GPKG', schema=schema)
row = {'geometry': {'type': 'Point', 'coordinates': (2, 2)}, 'properties': {}}
data = [row, row]

_handler.writerecords(data)

_handler = fiona.open('sample.gpkg', 'w', driver='GPKG', schema=schema)
_handler.writerecords(data)

_handler.close()

My suggestion is to probably check if a layer exists (or opened for writing if possible), and at least show a warning. Because otherwise I was thinking it’s because of not flushing the data, or closing it too quickly.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
culebroncommented, Nov 2, 2019

Just googled this bug myself another time while writing multiprocessing code. LOL.

0reactions
sgilliescommented, Nov 2, 2019

@culebron this remains a complicated issue. If Fiona datasets don’t call GDALClose when they are deallocated, data will not be written to disk and/or memory will leak in some cases. And this is what happens in your script, GDALClose is called twice for the same file, and very late in an unexpected way the second time. Ideally, the GPKG driver should lock if it’s not able to gracefully handle a double close, don’t you think? I wonder if the issue isn’t better solved in GDAL/OGR… maybe one of us should ask on gdal-dev to see what Even’s perspective is.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does simply opening a geopackage (gpkg) "modify" the ...
I believe that when the gpkg is closed and .wal and .shm files are processed something is updated in the main .gpkg file....
Read more >
Bug report #16341: Geopackage table behaves strange and ...
The gpkg file is converted from osm with FME, and FME can inspect and present both its vector and table data with no...
Read more >
248718 – graphics/qgis gpkg is not a valid or recognized data ...
When executing from terminal as a user or as root: ERROR 4: ... Moreover, I tried to load a KML file and get...
Read more >
Dataset Highlights - ESDAC - European Soil Data Centre
Description: This map provides an assessment of global soil erosion in croplands for the present (2019) and the future (2070). For a cover...
Read more >
https://raw.githubusercontent.com/qgis/QGIS/releas...
This patch takes care of deleting newly written .aux.xml files if there is a ... layers of the same GeoPackage (fixes #17034) Merge:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found