Skip to content

fix: LRU eviction never removes cached MBTiles (for...in on an array) - #54

Open
masaru87 wants to merge 2 commits into
geolonia:developfrom
masaru87:fix-lru-eviction-for-in
Open

fix: LRU eviction never removes cached MBTiles (for...in on an array)#54
masaru87 wants to merge 2 commits into
geolonia:developfrom
masaru87:fix-lru-eviction-for-in

Conversation

@masaru87

@masaru87 masaru87 commented Jul 24, 2026

Copy link
Copy Markdown

The LRU eviction in getMBTilesInstance (src/lib.ts) never actually removes anything from MBTILES_CACHE:

const filenamesToDelete = MBTILES_LRU_INDEX.splice(0, idxToDelete);
for (const toDel in filenamesToDelete) {   // for...in over an array
  delete MBTILES_CACHE[toDel];
}

filenamesToDelete is an array, so for...in iterates its indices ("0", "1", …), not its values. MBTILES_CACHE is keyed by cacheKey (size-mtime-filename), so delete MBTILES_CACHE["0"] is always a no-op. MBTILES_LRU_INDEX is trimmed by splice, so the index looks bounded, but the actual MBTILES_CACHE (and the MBTiles SQLite handles it holds) grows without bound past MBTILES_CACHE_MAX.

On a warm Lambda serving several mbtiles files/versions this leaks memory and file descriptors over time. Changed for...in to for...of so the eviction deletes the intended cache keys.

Summary by CodeRabbit

  • バグ修正
    • キャッシュ削除処理を見直し、不要になった地図タイルデータが確実に削除されるようになりました。
    • キャッシュ管理の削除対象の選定が改善され、より安定して運用できるようになりました。

@coderabbitai

coderabbitai Bot commented Jul 24, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 0ffdd660-0e69-4a9e-ae84-d3da9787c73b

📥 Commits

Reviewing files that changed from the base of the PR and between b875350 and 4ba26c2.

📒 Files selected for processing (1)
  • src/lib.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/lib.ts

📝 Walkthrough

Walkthrough

LRUキャッシュ削除処理で削除対象数の計算を調整し、配列のインデックスではなく削除対象のファイル名・キャッシュキーを使ってエントリを削除するよう変更しました。

Changes

LRUキャッシュ削除修正

Layer / File(s) Summary
削除対象数とキー走査の修正
src/lib.ts
idxToDelete の算出式を調整し、filenamesToDeletefor...of で走査して配列要素を MBTILES_CACHE の削除キーに使うようにしました。

Estimated code review effort: 1 (Trivial) | ~2 minutes

Poem

ぴょんと跳ねたらキーが見え
配列の中身をそっと消す
キャッシュも軽くなり
うさぎは満足
月夜にぱたり。

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed LRUキャッシュ削除の不具合修正を的確に表しており、変更内容と整合しています。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/lib.ts`:
- Around line 76-81:
MBTILES_LRU_INDEXの上限判定で、現在の件数がMBTILES_CACHE_MAXと等しい場合も新規追加前に1件削除されるよう、idxToDeleteの計算を修正してください。既存の削除処理は維持し、上限を超えないことを確認する回帰テストを追加してください。
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 59b206ce-0537-4d25-8503-53f32145c596

📥 Commits

Reviewing files that changed from the base of the PR and between 7198906 and b875350.

📒 Files selected for processing (1)
  • src/lib.ts

Comment thread src/lib.ts
When MBTILES_LRU_INDEX.length === MBTILES_CACHE_MAX, idxToDelete was 0,
so nothing was evicted before the new entry was pushed and the cache
grew to MAX+1 (staying one over from then on). Delete length-MAX+1 so
the cache holds at MAX after insertion.
@masaru87

masaru87 commented Jul 24, 2026

Copy link
Copy Markdown
Author

ご指摘ありがとうございます。off-by-one、その通りでした。

MBTILES_LRU_INDEX.length === MBTILES_CACHE_MAX のとき idxToDelete が 0 になり、削除ゼロのまま新規挿入で MAX+1 に到達(以降その状態で張り付き)していました。length - MAX + 1 に修正し、挿入50回のシミュレーションで常に上限6を維持することを確認して push しました。元の for...of 修正(削除ループが no-op だった件)と合わせて、これでエビクションが上限を正しく守ります。

回帰テストについては、MBTILES_CACHE / MBTILES_LRU_INDEX / getMBTilesInstance がいずれもモジュール private なため、ユニットテストを足すには内部を export するか SQLite の実 mbtiles を使った統合テストが必要で、この小さな PR の範囲を超えてしまいます。テスト用の seam を切る follow-up が必要でしたら別途出します。ご判断お任せします。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant