test_uuid.py on HP NonStop Python-2.7.5 fails (test case: testIssue8621)
Hi, We are porting python to HP NonStop & to a large extent we have been successful. While running the unit tests we happen to hit upon the problem reported in issue8621(http://bugs.python.org/issue8621), i.e uuid4 sequences on both parent & child were same . On NonStop we lack support for both hardware randomness & uuid_generate family of calls, so on NonStop "uuid.py" falls throw to random.range() call to generate the random number which in turn is used by the UUID class. Below the code snip that can used to repo the problem... #!/usr/bin/python import os import sys import uuid import random #Code taken from Lib/uuid.py def myuuid(): bytes = [chr(random.randrange(256)) for i in range(16)] val = uuid.UUID(bytes=bytes, version=4) return val # Code taken from Lib/test/test_uuid.py # # On at least some versions of OSX myuuid generates # the same sequence of UUIDs in the parent and any # children started using fork. for i in range(10): fds = os.pipe() pid = os.fork() if pid == 0: os.close(fds[0]) value = myuuid() os.write(fds[1], value.hex) os._exit(0) else: os.close(fds[1]) parent_value = myuuid().hex os.waitpid(pid, 0) child_value = os.read(fds[0], 100) os.close(fds[0]) print parent_value, child_value, (parent_value == child_value) We also ran the above test on Linux & were seeing the same results as on NonStop, i.e uuid4 sequences on both parent & child were same. Output snippet..... [vsnag@<hostname> Python-2.7.5]$ uname -a Linux <hostname> 2.6.32-71.el6.x86_64 #1 SMP Wed Sep 1 01:33:01 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux [vsnag@<hostname> Python-2.7.5]$ ./python test.py 2b512ef47c9f471a829148ca4e779dff 2b512ef47c9f471a829148ca4e779dff True e39b605476e04ca2b992b5d055278c3b e39b605476e04ca2b992b5d055278c3b True 8249fb9f6a2045c084da549c9e393a51 8249fb9f6a2045c084da549c9e393a51 True e1d9e84b7f134930abf5fb2760f07585 e1d9e84b7f134930abf5fb2760f07585 True 068f9612f0744eeb92dbca8950028a3c 068f9612f0744eeb92dbca8950028a3c True e85df457614f47d5b6300186bdd1c798 e85df457614f47d5b6300186bdd1c798 True a8628a0817b843778e9565b835b8cfac a8628a0817b843778e9565b835b8cfac True 478d8e9e5090498e9fad5356c5ae9568 478d8e9e5090498e9fad5356c5ae9568 True 151d7f1c8f8b42099a89a0f442e98dc7 151d7f1c8f8b42099a89a0f442e98dc7 True 838571b03a5b46f8a06398e020647d89 838571b03a5b46f8a06398e020647d89 True [vsnag@<hostname> Python-2.7.5]$ Any thoughts what could be going wrong....? Thanks & Regards Nagendra.V.S
Hi, On Thu, Nov 28, 2013 at 7:14 AM, V S, Nagendra (Nonstop Filesystems Team)
on NonStop "uuid.py" falls throw to random.range() call to generate the random number
And indeed, the random module gets identical results in both parent and child after a fork(). This seems to mean that the random module is not safe to use in uuid without taking special care of this fact (e.g. feeding back os.getpid() into the seed). A bientôt, Armin.
Am 28.11.2013 10:53, schrieb Armin Rigo:
Hi,
On Thu, Nov 28, 2013 at 7:14 AM, V S, Nagendra (Nonstop Filesystems Team)
on NonStop "uuid.py" falls throw to random.range() call to generate the random number
And indeed, the random module gets identical results in both parent and child after a fork(). This seems to mean that the random module is not safe to use in uuid without taking special care of this fact (e.g. feeding back os.getpid() into the seed).
Yes, there is an issue with the fallback to random, see my 17 months old bug report: http://bugs.python.org/issue15206 The uuid module needs a fork() aware random instance like the tempfile module. Christian
Hi,
On Thu, Nov 28, 2013 at 7:14 AM, V S, Nagendra (Nonstop Filesystems Team)
on NonStop "uuid.py" falls throw to random.range() call to generate the random number
And indeed, the random module gets identical results in both parent and child after a fork(). This seems to mean that the random module is not safe to use in uuid without taking special care of this fact (e.g. feeding back os.getpid() into the seed).
Yes, there is an issue with the fallback to random, see my 17 months old bug report: http://bugs.python.org/issue15206 The uuid module needs a fork() aware random instance like the tempfile module.
Christian
Thanks all for your responses. I tried to something similar to what Armin was suggesting. Here is what I have done def myuuid(): random.seed(time()) # or just could be random.seed() <-------------New addition bytes = [chr(random.randrange(256)) for i in range(16)] val = uuid.UUID(bytes=bytes, version=4) return val With this I was able to get a unique sequences in both parent & child after fork().I have tested this function by calling it a million times & every time there has been a unique sequence both in parent & child. Do you this can be fair enough work around for this problem. Thanks & Regards Nagendra.V.S
participants (3)
-
Armin Rigo -
Christian Heimes -
V S, Nagendra (Nonstop Filesystems Team)