Skip to content

Fix datasource lifecycle locking, boolean detection and docs - #119

Merged
trasch merged 2 commits into
mainfrom
fix/thread-safety-and-validation
Nov 2, 2026
Merged

trasch merged 2 commits into
mainfrom
fix/thread-safety-and-validation

Conversation

@trasch

@trasch trasch commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Datasource destruction was the one lifecycle path not covered by the documented serialization, boolean detection relied on a Foundation implementation detail, and several doc comments contradicted the code.

Thread safety

Datasource destruction was unsynchronized

mapnik_datasource_create takes mapnik_lifecycle_mutex, but mapnik_datasource_destroy did not — so a datasource could be torn down while another thread was loading a stylesheet, which the class documentation says cannot happen. Destruction now takes the same mutex.

Undefined behaviour in MapnikDatasource.init

let keysPointer = UnsafeRawPointer(keysBuffer.baseAddress!)
    .assumingMemoryBound(to: UnsafePointer<CChar>?.self)

The buffer's element type was already UnsafePointer<CChar>?, so this round-tripped through UnsafeRawPointer for no reason and force-unwrapped baseAddress. It now passes the buffer pointers directly.

Documented contracts

  • MapnikDatasource is documented as safe for one concurrent query at a time.
  • Mapnik.deinit is documented as possibly blocking on the lifecycle lock.
  • The counting-datasource header now matches the implementation: the layer keeps a shared_ptr, so the handle may be destroyed early (it previously said the opposite).

Boolean detection

if number.objCType[0] == Int8(99) { // "c" = boolean

objCType is a Foundation implementation detail, and "c" also matches small integers on some versions. The check now compares against Foundation's shared boolean instances:

{"flag": true, "small": 1}  ->  ["flag": true, "small": 1]   (Bool and Int, correctly)

Documentation

Claim Reality
"Rendering XYZ tiles of a map whose SRS is not Web Mercator is therefore valid" An epsg:4326 map reads the tile box as ±20037508 degrees and renders empty
ImageFormat.svg is just another format Every raster entry point rejects it
fieldFilter controls the reported field order It is a membership filter; output keeps datasource order

MetatileOptions.validated() was added so callers can get validation errors at construction without making the default argument throwing.

Testing

  • Concurrent datasource create → inspect → query → destroy.
  • A map stays usable after its counting datasource handle is destroyed.
  • 82 tests pass across the affected suites.

Closes #41
Closes #47
Closes #61
Closes #84
Closes #87
Closes #97
Closes #102
Closes #105
Closes #110

Thread safety
- mapnik_datasource_destroy now takes the lifecycle mutex like
  mapnik_datasource_create, so a datasource cannot be torn down while
  another thread is loading a stylesheet. The class documentation claimed
  lifecycle paths could not interleave; datasource destruction was the
  exception.
- MapnikDatasource.init passes the pointer arrays directly instead of
  reinterpreting their storage through UnsafeRawPointer +
  assumingMemoryBound, which is undefined behaviour (and force-unwrapped
  baseAddress).
- Documented that MapnikDatasource is only safe for one concurrent query
  at a time, and that Mapnik.deinit may block on the lifecycle lock.

Feature queries
- Boolean detection compares against Foundation's shared boolean
  instances instead of sniffing NSNumber.objCType[0] == 'c', which is an
  implementation detail and also matches small integers on some versions.
  Verified: {"flag": true, "small": 1} now decodes as Bool and Int.

Docs
- renderTile warns that a non-Web-Mercator map interprets the tile box in
  its own SRS (an epsg:4326 map renders empty), instead of claiming it is
  "valid".
- ImageFormat.svg documents that it is not usable with the raster entry
  points.
- inspect's fieldFilter documents that it is a membership filter and does
  not control output order.
- The counting-datasource header now matches the implementation: the
  layer keeps a shared_ptr, so the handle may be destroyed early.
- MetatileOptions.validated() validates at construction for callers that
  want the error early, without making the default argument throwing.

Tests
- Concurrent datasource create/inspect/query/destroy, and that a map
  stays usable after its counting datasource handle is destroyed.

Fixes #41
Fixes #47
Fixes #61
Fixes #84
Fixes #87
Fixes #97
Fixes #102
Fixes #105
Fixes #110
@trasch trasch self-assigned this Nov 2, 2026
@trasch trasch added the bug Something isn't working label Nov 2, 2026
Rebased onto main after #118 landed. Conflicts were resolved by combining
both sides: main's newer code (dropped-feature counting, non-square metatile
dimensions) plus this PR's hardening on top.

Thread safety
- mapnik_datasource_destroy takes the lifecycle mutex like
  mapnik_datasource_create, so a datasource cannot be torn down while
  another thread is loading a stylesheet.
- MapnikDatasource.init passes the pointer arrays directly instead of
  reinterpreting their storage through UnsafeRawPointer +
  assumingMemoryBound (undefined behaviour, and it force-unwrapped
  baseAddress).
- Documented that MapnikDatasource is safe for one concurrent query at a
  time, and that Mapnik.deinit may block on the lifecycle lock.

Feature queries
- Boolean detection compares against Foundation's shared boolean instances
  instead of sniffing NSNumber.objCType[0] == 'c', which is an
  implementation detail and also matches small integers on some versions.
  Verified: {"flag": true, "small": 1} decodes as Bool and Int.

Docs
- renderTile warns that a non-Web-Mercator map interprets the tile box in
  its own SRS (an epsg:4326 map renders empty) instead of claiming it is
  "valid".
- ImageFormat.svg documents that it is not usable with the raster entry
  points.
- inspect's fieldFilter documents that it is a membership filter and does
  not control output order.
- The counting-datasource header matches the implementation: the layer
  keeps a shared_ptr, so the handle may be destroyed early.
- MetatileOptions.validated() validates at construction for callers that
  want the error early, without making the default argument throwing.

Tests
- Concurrent datasource create/inspect/query/destroy, and that a map stays
  usable after its counting datasource handle is destroyed.
@trasch
trasch merged commit d314c12 into main Nov 2, 2026
5 checks passed
@trasch
trasch deleted the fix/thread-safety-and-validation branch November 2, 2026 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment