← 输入框|github.com|刷新
Skip to content

Do not change TZ and locale globally in libarchive2 #65 - #80

Open
antcybersec wants to merge 1 commit into
aboutcode-org:mainfrom
antcybersec:65-scope-tz-and-locale-to-extraction
Open

antcybersec wants to merge 1 commit into
aboutcode-org:mainfrom
antcybersec:65-scope-tz-and-locale-to-extraction

Conversation

@antcybersec

Copy link
Copy Markdown

Fixes #65

Problem

Importing extractcode.libarchive2 changed process-wide state:

  • os.environ["TZ"] = "UTC" at import time, and again on every extract() call
  • locale.setlocale(locale.LC_ALL, "") at import time

Any application using extractcode as a library therefore ended up in UTC, and any locale it had set was overwritten. Reproduced on main with TZ=Europe/Berlin, after the app had called setlocale(LC_ALL, "C"):

before import    TZ='Europe/Berlin'  localtime(0).tm_hour=1  LC_ALL='C'
after import     TZ='UTC'            localtime(0).tm_hour=0  LC_ALL='de_DE.UTF-8'

Fix

As discussed in the issue, UTC is still wanted for extraction: libarchive converts zip DOS dates (stored in local time) using the current TZ, so this keeps extracted file dates the same on every machine. The UTC setting just shouldn't leak out of extraction.

  • New extraction_environment() context manager in libarchive2.py, applied to extract(). It sets TZ=UTC and the user default locale for the duration of the extraction, then restores the caller's TZ (or removes it if it was unset) and locale.
  • TZ changes go through a small set_tz() helper that also calls time.tzset() where available (not on Windows). Restoring os.environ alone isn't enough on Linux. libarchive converts DOS dates with mktime(), which makes glibc cache UTC. Python's time.localtime() uses localtime_r(), which doesn't reload TZ, so without tzset() the caller would stay in UTC after an extraction. macOS reloads TZ on each call, so it doesn't show the problem.
  • It is reference-counted under a lock, so nested or concurrent extractions in threads don't restore the original settings while another extraction is still running. A plain save/restore would race there. Other threads still see UTC while an extraction runs, as noted in the docstring, because these are process-wide settings.
  • Removed the import-time calls and set_env_with_tz(). No other repo in aboutcode-org references it.

This follows the same idea as sevenzip.py, which already passes TZ only to the 7z subprocess instead of setting it globally.

Tests

Added TestExtractionEnvironment in tests/test_libarchive2.py:

  • importing libarchive2 (in a subprocess) leaves TZ and the locale unchanged
  • extract() restores the original TZ and locale, including when extraction raises, and does not leave TZ set if it was unset before
  • after extract(), time.localtime() in the caller is still in the caller's timezone. This runs in a subprocess so it checks the C library's cached zone, not just os.environ, which is the glibc case above.
  • extraction_environment() is reentrant
  • a zip with only a DOS date, extracted with a non-UTC TZ, gets the expected UTC mtime. This guards the behaviour the import-time setting used to provide. The existing c.zip timezone fixtures carry UTC extended timestamps, so they don't exercise this. Skipped on Windows, where the C runtime caches TZ.

Verification:

  • On Linux (Debian, glibc 2.41, system libarchive), 6 of the new tests fail on the current main code and all of them pass with this change.
  • With the tzset() call removed, the local-time test fails on Linux.
  • On macOS, the new tests also catch a version of the fix without TZ=UTC, and one with a naive save/restore that isn't reference-counted.
  • On macOS, the full suite shows no new failures compared with main, both in the default environment and with TZ=Europe/Berlin LANG=de_DE.UTF-8. The only failures are pre-existing: CLI tests that expect a venv/ directory, and 2 patch tests.
  • ruff check and ruff format --check pass.

Happy to add a CHANGELOG entry if you'd like one.

Importing extractcode.libarchive2 set the TZ environment variable to UTC
and the locale to the user default for the whole process, and extract()
set TZ again on each call. This changed the timezone and locale of any
application using extractcode as a library.

Instead, set UTC and the user default locale only while an extraction is
running and restore the original TZ and locale afterwards. Extracted file
dates are still computed in UTC. Call time.tzset() when changing TZ, as
glibc caches the timezone loaded by mktime() and would otherwise keep
using UTC for localtime() after the extraction. This is reentrant and safe
for concurrent extractions: the original settings are restored when the
last running extraction ends.

Reference: aboutcode-org#65
Signed-off-by: anant <170962632+antcybersec@users.noreply.github.com>

This branch has not been deployed

No deployments
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.

Side effects on locale and timezone when importing extractcode.libarchive2 as library

1 participant