Repository navigation
Do not change TZ and locale globally in libarchive2 #65 - #80
Open
antcybersec wants to merge 1 commit into
Open
antcybersec wants to merge 1 commit into
antcybersec wants to merge 1 commit into
Conversation
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
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.
Fixes #65
Problem
Importing
extractcode.libarchive2changed process-wide state:os.environ["TZ"] = "UTC"at import time, and again on everyextract()calllocale.setlocale(locale.LC_ALL, "")at import timeAny application using extractcode as a library therefore ended up in UTC, and any locale it had set was overwritten. Reproduced on
mainwithTZ=Europe/Berlin, after the app had calledsetlocale(LC_ALL, "C"):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.
extraction_environment()context manager inlibarchive2.py, applied toextract(). It setsTZ=UTCand the user default locale for the duration of the extraction, then restores the caller'sTZ(or removes it if it was unset) and locale.TZchanges go through a smallset_tz()helper that also callstime.tzset()where available (not on Windows). Restoringos.environalone isn't enough on Linux. libarchive converts DOS dates withmktime(), which makes glibc cache UTC. Python'stime.localtime()useslocaltime_r(), which doesn't reloadTZ, so withouttzset()the caller would stay in UTC after an extraction. macOS reloadsTZon each call, so it doesn't show the problem.set_env_with_tz(). No other repo in aboutcode-org references it.This follows the same idea as
sevenzip.py, which already passesTZonly to the 7z subprocess instead of setting it globally.Tests
Added
TestExtractionEnvironmentintests/test_libarchive2.py:libarchive2(in a subprocess) leavesTZand the locale unchangedextract()restores the originalTZand locale, including when extraction raises, and does not leaveTZset if it was unset beforeextract(),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 justos.environ, which is the glibc case above.extraction_environment()is reentrantTZ, gets the expected UTC mtime. This guards the behaviour the import-time setting used to provide. The existingc.ziptimezone fixtures carry UTC extended timestamps, so they don't exercise this. Skipped on Windows, where the C runtime cachesTZ.Verification:
maincode and all of them pass with this change.tzset()call removed, the local-time test fails on Linux.TZ=UTC, and one with a naive save/restore that isn't reference-counted.main, both in the default environment and withTZ=Europe/Berlin LANG=de_DE.UTF-8. The only failures are pre-existing: CLI tests that expect avenv/directory, and 2 patch tests.ruff checkandruff format --checkpass.Happy to add a CHANGELOG entry if you'd like one.