From 784e8ae27d42d8e25deacaf37c34d79369738b00 Mon Sep 17 00:00:00 2001 From: Christopher Wilcox Date: Wed, 7 Jul 2021 11:20:50 -0700 Subject: [PATCH] fix: reseed for each auto id on 3.6 to avoid collisions (#388) * fix: Fixes #346 by reseeding for each auto id on py3.6 --- google/cloud/firestore_v1/base_collection.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/google/cloud/firestore_v1/base_collection.py b/google/cloud/firestore_v1/base_collection.py index a022e96ba..ce31bfb0a 100644 --- a/google/cloud/firestore_v1/base_collection.py +++ b/google/cloud/firestore_v1/base_collection.py @@ -14,6 +14,7 @@ """Classes for representing collections for the Google Cloud Firestore API.""" import random +import sys from google.api_core import retry as retries # type: ignore @@ -455,6 +456,12 @@ def _auto_id() -> str: str: A 20 character string composed of digits, uppercase and lowercase and letters. """ + if sys.version_info < (3, 7): + # TODO: remove when 3.6 support is discontinued. + # On python 3.6, random will provide the same results when forked. Reseed + # on each iteration to avoid collisions. + random.seed() + return "".join(random.choice(_AUTO_ID_CHARS) for _ in range(20))