kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Fall back to UTC when timezone detection yields no zone name

Open rtibbles opened this issue 6 months ago • 2 comments

❌ This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues.

Overview

Kolibri crashes at runtime with pytz.exceptions.UnknownTimeZoneError: None when the host has no resolvable timezone configuration — a common situation in docker/podman containers, where /etc/localtime is a bind-mounted plain file with no TZ env or /etc/timezone to name a zone. Timezone detection should fall back to UTC instead of crashing.

Complexity: Low Target branch: develop

Context

kolibri/deployment/default/settings/base.py already guards timezone detection:

try:
    TIME_ZONE = get_localzone_name()
except (pytz.UnknownTimeZoneError, ValueError, ZoneInfoNotFoundError):
    TIME_ZONE = pytz.utc.zone

if TIME_ZONE == "local":
    TIME_ZONE = pytz.utc.zone

But tzlocal's get_localzone_name() has a third failure mode: _get_localzone_name() returns None without raising when no explicit config is found (no TZ env var, no /etc/timezone, and /etc/localtime present but a plain file rather than a symlink — so no zone name can be derived). Neither guard fires, and TIME_ZONE = None survives settings import.

Django accepts TIME_ZONE = None until the first timezone.localtime() call, which raises pytz.exceptions.UnknownTimeZoneError: None — e.g. from the task worker's check_jobs loop, killing the server well after an apparently clean boot:

File ".../kolibri/core/tasks/storage.py", line 782, in _now
    return local_now()
File ".../kolibri/utils/time_utils.py", line 8, in local_now
    return timezone.localtime(timezone.now())
...
File ".../pytz/__init__.py", line 168, in timezone
    raise UnknownTimeZoneError(None)
pytz.exceptions.UnknownTimeZoneError: None

Note tzlocal's object-returning get_localzone() handles this exact case itself (falls back to reading the localtime file, then warns and defaults to UTC) — only the name-returning variant has the silent-None hole.

The Change

Treat a None return from get_localzone_name() the same as the existing failure modes: fall back to pytz.utc.zone silently, alongside the current exception handler and "local" guard. After settings import, TIME_ZONE should never be None regardless of the host's timezone configuration.

How to Get There

Reproduce in any container without timezone configuration — no TZ env var, no /etc/timezone, and /etc/localtime a regular file rather than a symlink (typical of docker/podman bind-mounts). In such an environment:

python -c "from tzlocal import get_localzone_name; print(get_localzone_name())"
None

Start Kolibri and the server boots apparently cleanly, then dies as soon as anything calls local_now() (the task worker's check_jobs loop gets there within seconds). Setting TZ=UTC in the environment is the current workaround.

Out of Scope

  • No warning/logging when the fallback engages.
  • No change to which tzlocal API is used for detection.

Acceptance Criteria

  • [ ] TIME_ZONE is "UTC" after settings import when get_localzone_name() returns None
  • [ ] Kolibri starts and serves in a container with no timezone configuration (as described above) without UnknownTimeZoneError — verified manually

AI usage

This issue was drafted with Claude (Claude Code). The bug surfaced while Claude was smoke-testing load-test tooling in a distrobox container; the root cause was verified by reading the bundled tzlocal and Kolibri settings source, and the traceback above is from the real failure. I directed and reviewed each section.

rtibbles avatar Jun 04 '26 23:06 rtibbles

Plan: Prevent pytz.exceptions.UnknownTimeZoneError: None by treating a None return from get_localzone_name() the same as the existing failure modes — falling back to UTC.

  • [ ] Write the failing test
  • [ ] Apply the one-line fix
  • [ ] Manual verification note

status


@rtibblesbot's comments are generated by an LLM, and should be evaluated accordingly

How was this generated?
  • Implemented code following the pre-approved plan using test-driven development (red-green-refactor)
  • Ran the project's full test suite and linter before committing
  • For UI changes: captured live screenshots to verify rendering
  • For backend changes: verified with example API/CLI output

rtibblesbot avatar Jun 04 '26 23:06 rtibblesbot

The bot has been waiting for a response for 2 hour(s). It will resume automatically when someone replies.

rtibblesbot avatar Jun 05 '26 02:06 rtibblesbot