Fix potential segfault errors#64
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens ToGo’s Cython bindings against C-level lifetime and allocation hazards that can manifest as segfaults under heavy geometry extraction / GC pressure. It primarily shifts wrapper creation toward cloning TG objects (so Python wrappers own their C pointers), adds defensive NULL checks, and improves exception-safety/cleanup around C allocations and GEOS operations.
Changes:
- Clone borrowed TG pointers before returning Python wrapper objects; add NULL-pointer and clone-failure checks.
- Make several C allocation/write paths exception-safe via
try/finally, plus checked Python→C count conversion for TG APIs. - Add regression tests covering wrapper survival after parent GC and empty-geometry meters-grid round trips.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
togo.pyx |
Adds pointer-cloning/NULL checks and improves cleanup/exception-safety across multiple TG/GEOS interop paths. |
tests/test_poly.py |
Adds a GC regression test ensuring Poly.exterior remains usable after parent collection. |
tests/test_geometry.py |
Adds GC regression tests for accessors and verifies meters-grid conversions for empty geometries. |
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Copilot stopped work on behalf of
mindflayer due to an error
June 8, 2026 09:12
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
Fixes several C-level memory safety issues in ToGo's Cython bindings that could surface as segfaults under planner-style workloads with heavy geometry extraction, GEOS operations, and garbage collection pressure.
What changed
Geometry.line()Geometry.poly()Poly.exteriorPoly.hole(...)Ring.from_ptr(...)/Poly._from_c_poly(...)pathsNULLpointer checks before wrapping TG pointers.Geometry.geomexplicitly inGeometry.__cinit__.try/finally.Geometry.unary_union(...)by destroying GEOS geometries on all success/error paths.Geometry/Polygarbage collectionWhy
The previous implementation returned Python wrapper objects around borrowed C pointers owned by parent geometries. If the parent object was garbage-collected while the child wrapper remained in use, later calls could dereference freed memory and crash.
This was especially likely in long-running planner integrations that repeatedly extract rings, polygons, boundaries, or line strings from temporary geometries.
Validation
Built successfully with: