fix: Clean up temp dirs when update_features! creates its own tmpdir#56
Merged
Conversation
`update_features!` created a tracked temp dir via `options.fetch(:tmpdir) { Dir.mktmpdir("ruby_spatial_features") }`, but `Hash#fetch` with a block does not write the default back into the hash, so the `options[:tmpdir]` it passed to `spatial_feature_imports` was still `nil`. `Unzip.extract` then minted its own untracked temp dir for every archive import, and the `ensure` block only removed the empty tracked dir — leaking one extraction directory per KMZ/zipped-shapefile import. Bulk runs like a nightly `update_features!` over thousands of records leaked thousands of directories per run.
Instead of passing the never-populated `options[:tmpdir]` to the importers, we now pass the local `tmpdir`, so extraction happens inside the tracked directory the `ensure` block already removes.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
Problem
update_features!has leaked one temp directory per archive import since 2022 (1ecc01a). Line 25 creates a tracked temp dir:but
Hash#fetchwith a block doesn't write the default back into the hash, so line 28 passedoptions[:tmpdir]— stillnil— down to the importers.Unzip.extractthen minted its own untracked temp dir for every KMZ / zipped-shapefile import, and theensureblock only removed the empty tracked dir (a decoy).Bulk runs amplify this: a nightly
update_features!sweep over thousands of records withskip_invalid: truenever aborts and leaks thousands of extraction directories per run. This is what got Stolo's nightlyupdate_geometrycron disabled in Nov 2024.Fix
Pass the local
tmpdirtospatial_feature_importsinstead ofoptions[:tmpdir], so extraction happens inside the tracked directory the existingensurealready removes. One line.Tests
The existing cleanup spec only covered the caller-supplied-
tmpdirpath (whereoptions[:tmpdir]is set), which is why the default path went untested for four years. Added two regression specs for the no-tmpdirpath — import success and import failure underskip_invalid: true(unzip happens before the failure, so this is exactly the leak scenario). Both fail against the old code and pass with the fix. Full suite: 248 examples, 0 failures.Not addressed (known, much smaller drips)
Download.normalize_fileuses blocklessTempfile.new(GC-finalizer cleanup only)Shapefile#project_to_4326leaks the ogr2ogr companion files (.dbf/.shx/.prj/.cpg) — theensuredeletes only the.shpBoth are plain files (not directories), so age-based /tmp sweeps reclaim them.
🤖 Generated with Claude Code