Skip to content

Fix flaky random ZIP creation in TestPolyglotModule#179

Merged
dguido merged 1 commit intomasterfrom
flaky-test-zip-issue-162
Jan 20, 2026
Merged

Fix flaky random ZIP creation in TestPolyglotModule#179
dguido merged 1 commit intomasterfrom
flaky-test-zip-issue-162

Conversation

@thomas-chauchefoin-tob
Copy link
Copy Markdown
Collaborator

See #162 for context. I think the user got (un)lucky and the randomly generated ZIP file looked like a valid Pickle object with 2+ valid opcodes.

@thomas-chauchefoin-tob thomas-chauchefoin-tob marked this pull request as ready for review December 3, 2025 00:51
@dguido
Copy link
Copy Markdown
Member

dguido commented Jan 9, 2026

Thanks for tracking this down! The fix is on the right track, but there's an issue: random.seed(42) doesn't affect os.urandom().

In create_random_zip():

def create_random_zip(filename, size=1024):
    tmp_filename = "".join(random.choices(...))  # ✅ Affected by seed
    with open(tmp_filename, "wb") as f:
        f.write(os.urandom(size))  # ❌ NOT affected by seed

os.urandom() reads from the OS cryptographic random source (/dev/urandom), which is independent of Python's random module. So the ZIP content remains non-deterministic.

To fully fix the flakiness, replace os.urandom(size) with random.randbytes(size) (available since Python 3.9):

f.write(random.randbytes(size))  # ✅ Respects random.seed()

This will make the entire test deterministic.

@dguido dguido force-pushed the flaky-test-zip-issue-162 branch from fa91aec to 8cc76d3 Compare January 20, 2026 16:26
@dguido dguido merged commit 17ff3a5 into master Jan 20, 2026
14 checks passed
@dguido dguido deleted the flaky-test-zip-issue-162 branch January 20, 2026 16:27
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.

2 participants