Repository navigation
Fix pool and work queue concurrency defects - #112
Merged
Merged
Conversation
MapnikPool - release() now tracks which objects are actually lent out, so a double release can no longer put the same Mapnik instance into the pool twice and hand it to two callers at once (which would race on a renderer that is not thread-safe). - inUseCount is a lock-protected computed property instead of a public stored var read without synchronisation. - acquire() runs the factory without holding the pool lock, so a slow stylesheet load no longer blocks every other acquire/release/stats call. A `creating` counter keeps maxPoolSize exact now that creation happens unlocked. - The logger is invoked after unlocking, so a logger calling back into the pool no longer deadlocks. - Initial creation is a plain loop: mapnik serializes stylesheet loading internally, so the concurrent GCD setup bought nothing while blocking the calling thread in group.wait(). - init validates its sizes and throws MapnikError.invalidInput instead of trapping, and rejects initialSize > maxPoolSize instead of silently clamping it. - New close() drops idle objects and rejects further acquires, so a long-lived pool no longer pins loaded stylesheets forever. - MapnikPoolError.exhausted carries the pool limit. WorkQueue - start() resets inFlight, so the actor is reusable after an error. It used to stay at maxConcurrency and silently process nothing on a second run. - fetchWork and onResult are @sendable, closing a hole in strict concurrency checking: callers could capture unsynchronised mutable state and race on it. - Documented that errors cancel and drain the remaining work. Tests - New regressions for double release, queue reuse after an error, the logger re-entering the pool, close(), init validation and inUseCount. - WorkQueue tests use a thread-safe WorkSource instead of captured iterators, which the @sendable change correctly rejects. - The pool/queue suites pass under --sanitize=thread. Fixes #8 Fixes #9 Fixes #10 Fixes #38 Fixes #43 Fixes #49 Fixes #50 Fixes #51 Fixes #55 Fixes #67 Fixes #79 Fixes #80 Fixes #82 Fixes #83 Fixes #99
trasch
added a commit
that referenced
this pull request
Nov 2, 2026
Rebased onto main after #111-#116 landed. Conflicts were resolved by keeping main's newer logic and re-applying this PR's hardening on top: - MapnikPool keeps the unlocked-factory acquire from #112. - TileSequence/MetatileSequence keep main's bounds.isGeographic check, now thrown as MapnikError.invalidInput instead of a precondition. Trapping accessors - layerVisible(_:), layerSRS(_:), layerQueryable(_:) and layerVisible(_:atScaleDenominator:) used a plain Int32(index), which traps for indices outside Int32 range. They now return nil as documented. - Metatile.init, TileSequence.init and MetatileSequence.init used precondition for user-facing validation. They now throw MapnikError.invalidInput, so callers that build blocks or sequences from external data get an error instead of a crash. - MetatileOptions.metatileSize/validate no longer trap on a huge width x scale. Silent truncation - srs and layerSRS grow the buffer instead of returning a silently truncated projection string; long PROJ.4 strings exceed 255 characters. Validation - ImageFormat validates its options (png colors 1...256, jpeg/webp quality 0...100), so a bad value surfaces as MapnikError.invalidInput instead of a renderingFailed from deep inside mapnik. - setBufferSize clamps negative values, matching RenderOptions.validate. Errors - MapnikError is Equatable and LocalizedError, and gained a serializationFailed case. - MapnikPoolError.exhausted carries the pool limit and is Equatable. BoundingBox parsing - BoundingBox(parsing:) parses positionally and rejects non-finite values. It previously used compactMap, so "1,2,foo,3,4" was accepted as (1,2,3,4) with the bad field silently dropped. Tests - APIHardeningTests: huge layer indices, long SRS round trip, negative bufferSize, ImageFormat validation, error equality, Metatile/sequence validation instead of trapping, and malformed bounding-box strings.
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
MapnikPoolandWorkQueueboth had defects that could hand a non-thread-safe renderer to two callers at once, or leave the queue permanently dead after a single error. This PR fixes the concurrency model of both.MapnikPool
Double release handed out the same object twice
release()appended the object toavailablewithout checking that it had actually been lent out, so:Mapnikis explicitly documented as not safe for concurrent rendering, so this was a data race waiting to happen. The pool now tracks alentOutset and ignores (and logs) a release of an object it did not lend out.Verified:
double release: same object twice? false.Other fixes
inUseCountread without the lockcreatingcounter keepsmaxPoolSizeexactpreconditionon sizesMapnikError.invalidInputinitialSize > maxPoolSizesilently clampedclose()exhaustedcarried no contextexhausted(limit:)WorkQueue
Unusable after an error
inFlightwas incremented when a task was added but only decremented in the drain loop, so a throwingprocessWorkleft it atmaxConcurrencyforever:start()now resetsinFlighton entry and on both exit paths, and the new test asserts a second run makes progress.@SendableclosuresfetchWorkandonResultwere stored as plain closures inside aSendableactor, so callers could capture unsynchronised mutable state. They are now@Sendable, which the compiler enforces — the test suite's captured iterators were exactly that bug and now use a thread-safeWorkSource.Testing
close(), init validation andinUseCount.--sanitize=thread.saveXML(explicitDefaults:)failures remaining — those are fixed in Honour all render options and fix metatile geometry #111.Breaking changes
MapnikPoolError.exhaustednow carrieslimit: Int.initthrowsMapnikError.invalidInputinstead of trapping, and rejectsinitialSize > maxPoolSize.WorkQueue'sfetchWork/onResultare@Sendable; a capturedvarno longer compiles.MapnikPool.inUseCountis now a computed property (still readable, stillInt).Closes #8
Closes #9
Closes #10
Closes #38
Closes #43
Closes #49
Closes #50
Closes #51
Closes #55
Closes #67
Closes #79
Closes #80
Closes #82
Closes #83
Closes #99