fix(geoip): reuse one City reader across reloads - #175
Merged
Conversation
`OpenWeather.__init__` opened a fresh `geoip2.database.Reader` every time `CommandHandler.Setup()` ran. #173 added `close()` on the outgoing command so the previous mmap of the database no longer leaked until GC (#55), but rebuilding the reader on every `!reload` is still fragile: any failure to map the file now aborts command setup with an error traceback in the logs (#174). Move the reader into a dedicated `torchlight.GeoIP` module (only `Commands` is reloaded, so the cache survives `importlib.reload`). It is opened once per path and only reopened when the file on disk actually changes, e.g. after a GeoIP update; a failed reopen keeps serving the previous reader instead of dropping GeoIP entirely. `OpenWeather` no longer raises out of `__init__` when the database cannot be opened: it logs the error, leaves `geo_ip` unset, and `!ow` without an explicit location asks the player to name a city instead of crashing. Bumps VERSION to 1.8.26. Fixes #174 Co-Authored-By: Claude Sonnet 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
!reloadproduces an error traceback and drops the!owcommand (#174):CommandHandler.Setup()rebuilds every command on each reload, soOpenWeather.__init__opened a brand-newgeoip2.database.Reader(anmmapofthe ~60 MB City database) every time. #173 added
close()on the outgoingcommand so that mapping no longer leaked until GC (#55), but re-opening the
database on every
!reloadis still fragile — and when the open fails, theexception propagates out of
__init__, aborts the command inSetup(), and islogged as an error. The database is read-only and static; there is no reason to
tear it down and map it again on a config reload.
Fix
torchlight.GeoIPmodule owns the reader. OnlyCommandsis passed toimportlib.reload, so a cache in its own module survives!reload.get_city_reader()opens the database once per path and reuses it. Itreopens only when the file's mtime changes (e.g. after a GeoIP update), closing
the previous reader once the new one is open. If a reopen fails, it keeps
serving the previous reader instead of losing GeoIP entirely.
OpenWeatherno longer raises out of__init__when the database can't beopened: it logs the failure, leaves
geo_ipunset, and!owwith no argumentreplies asking for a city instead of crashing.
!ow <city>is unaffected.OpenWeatherno longer needs itsclose()override; the genericBaseCommand.close()hook from fix(#55): close YoutubeDL, geoip reader, and the audio-clip retain graph #173 stays for future use.Testing
ruff check .,ruff format . --diff,mypy— clean (the two pre-existingMyInstants.pymypy errors are unrelated).get_city_readerwith a stubbedgeoip2.database.Readercovering: reuse across calls, surviving repeated
Setup()with no file change,reopen on mtime change (old reader closed), reopen failure falling back to the
previous reader, missing file with a warm cache, and first-open failure
propagating.
Fixes #174
🤖 Generated with Claude Code