[Django Code] #6648: random seed identical among preforked FastCGI instances

View: New views
9 Messages — Rating Filter:   Alert me  

[Django Code] #6648: random seed identical among preforked FastCGI instances

by noreply-71 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

#6648: random seed identical among preforked FastCGI instances
-----------------------------+----------------------------------------------
Reporter:  nezroy@...  |       Owner:  nobody                          
  Status:  new               |   Component:  Uncategorized                  
 Version:  SVN               |    Keywords:  random seed fastcgi fcgi prefork
   Stage:  Unreviewed        |   Has_patch:  0                              
-----------------------------+----------------------------------------------
 While writing a basic captcha module, I was encountering a lot of
 duplicate strings of "random" text that I could not explain. Then I
 remembered seeing this comment on a separate session issue:
 http://code.djangoproject.com/ticket/1180#comment:20

 Based on that info, I setup some extra logging, and it did appear that
 each of my FastCGI threads were producing identical random values. I was
 specifically using a call like this:
 random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ'). It's possible other random
 methods are not affected, I was only interested in that one. It seems the
 random seed is set prior to the fork, and each forked process thereafter
 produces identical random output as a result.

 In my setup, I run manage.py with method=prefork, runfcgi, and a static
 number of processes. This is on an Ubuntu Gutsy AMD 64-bit (on Xen) with
 Python 2.5.1. I'm currently running against SVN Django, revision 7049.

 My workaround was to add a middleware class with an init function that
 sets the random seed. This gets called at "startup" in each of the new
 FCGI processes, giving them unique random output. Obviously not ideal, but
 it gets the job done in my case.

 {{{
 import logging, logging.config
 import os, random, time

 class ReseedRandom(object):
   def __init__(self):
     logging.debug("setting new random seed for this process")
     random.seed("%d%s" % (os.getpid(), time.ctime()))
 }}}

 It would be much cleaner if the manage.py FCGI launcher could seed its
 random number generator AFTER the fork occurs, rather than before (or not
 at all, as the case may be).

--
Ticket URL: <http://code.djangoproject.com/ticket/6648>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django updates" group.
To post to this group, send email to django-updates@...
To unsubscribe from this group, send email to django-updates-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: [Django] #6648: random seed identical among preforked FastCGI instances

by noreply-71 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

#6648: random seed identical among preforked FastCGI instances
---------------------------------------+------------------------------------
          Reporter:  nezroy@...  |         Owner:  nobody                          
            Status:  new               |     Milestone:                                  
         Component:  Uncategorized     |       Version:  SVN                            
        Resolution:                    |      Keywords:  random seed fastcgi fcgi prefork
             Stage:  Unreviewed        |     Has_patch:  1                              
        Needs_docs:  0                 |   Needs_tests:  0                              
Needs_better_patch:  0                 |  
---------------------------------------+------------------------------------
Changes (by peritus <peritus@...>):

  * has_patch:  0 => 1
  * stage:  Design decision needed => Unreviewed

Comment:

 I can reproduce this - my users could :(

 Given the following view and lighty+fastcgi+prefork:
 {{{
 def do_random(request):
     return HttpResponse(random.sample('ABCDEFGHIJKLMNOPQRSTUVWXYZ', 5))
 }}}

 Produces in subsequent requests:
 {{{
 XDQIU AJMNL JIQXN XDQIU IROXL AJMNL YAPJL CUYOK ODKLA JIQXN WACZR IROXL
 CYUJE SNQLX YAPJL CUYOK UMOWL ODKLA PIXQG WACZR CYUJE
 }}}

 which is *NOT* random.

 Applying the patch, the same view produces
 {{{
 QBLDN THPXF VSJQB RIPTF EZQRL OSNZY HQSOB VJSED HQBTO UTOJI VSTFY NHPBY
 QMITZ WJGZB QVDRN QCIOT GWYVQ RGZHE TXHKI
 }}}

 which seems fairly random.

--
Ticket URL: <http://code.djangoproject.com/ticket/6648#comment:2>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django updates" group.
To post to this group, send email to django-updates@...
To unsubscribe from this group, send email to django-updates+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: [Django] #6648: random seed identical among preforked FastCGI instances

by noreply-71 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

#6648: random seed identical among preforked FastCGI instances
---------------------------------------+------------------------------------
          Reporter:  nezroy@...  |         Owner:  nobody                          
            Status:  new               |     Milestone:                                  
         Component:  Uncategorized     |       Version:  SVN                            
        Resolution:                    |      Keywords:  random seed fastcgi fcgi prefork
             Stage:  Unreviewed        |     Has_patch:  1                              
        Needs_docs:  0                 |   Needs_tests:  0                              
Needs_better_patch:  0                 |  
---------------------------------------+------------------------------------
Changes (by anonymous):

 * cc: flosch@... (added)

--
Ticket URL: <http://code.djangoproject.com/ticket/6648#comment:3>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django updates" group.
To post to this group, send email to django-updates@...
To unsubscribe from this group, send email to django-updates+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: [Django] #6648: random seed identical among preforked FastCGI instances

by noreply-71 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

#6648: random seed identical among preforked FastCGI instances
---------------------------------------+------------------------------------
          Reporter:  nezroy@...  |         Owner:  nobody                          
            Status:  new               |     Milestone:                                  
         Component:  Uncategorized     |       Version:  SVN                            
        Resolution:                    |      Keywords:  random seed fastcgi fcgi prefork
             Stage:  Unreviewed        |     Has_patch:  1                              
        Needs_docs:  0                 |   Needs_tests:  0                              
Needs_better_patch:  0                 |  
---------------------------------------+------------------------------------
Changes (by anonymous):

 * cc: flosch@... (removed)
 * cc: flori@... (added)

--
Ticket URL: <http://code.djangoproject.com/ticket/6648#comment:4>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django updates" group.
To post to this group, send email to django-updates@...
To unsubscribe from this group, send email to django-updates+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: [Django] #6648: random seed identical among preforked FastCGI instances

by noreply-71 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

#6648: random seed identical among preforked FastCGI instances
---------------------------------------+------------------------------------
          Reporter:  nezroy@...  |         Owner:  nobody                          
            Status:  new               |     Milestone:                                  
         Component:  Core framework    |       Version:  SVN                            
        Resolution:                    |      Keywords:  random seed fastcgi fcgi prefork
             Stage:  Unreviewed        |     Has_patch:  1                              
        Needs_docs:  0                 |   Needs_tests:  0                              
Needs_better_patch:  0                 |  
---------------------------------------+------------------------------------
Changes (by peritus <peritus@...>):

  * component:  Uncategorized => Core framework

Comment:

 Reported this upstream:

 http://trac.saddi.com/flup/ticket/35

 I think, django should to add the workaround though.

--
Ticket URL: <http://code.djangoproject.com/ticket/6648#comment:5>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django updates" group.
To post to this group, send email to django-updates@...
To unsubscribe from this group, send email to django-updates+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: [Django] #6648: random seed identical among preforked FastCGI instances

by noreply-71 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

#6648: random seed identical among preforked FastCGI instances
---------------------------------------+------------------------------------
          Reporter:  nezroy@...  |         Owner:  nobody                          
            Status:  new               |     Milestone:                                  
         Component:  Core framework    |       Version:  SVN                            
        Resolution:                    |      Keywords:  random seed fastcgi fcgi prefork
             Stage:  Unreviewed        |     Has_patch:  1                              
        Needs_docs:  0                 |   Needs_tests:  0                              
Needs_better_patch:  0                 |  
---------------------------------------+------------------------------------
Comment (by peritus <peritus@...>):

 This has been fixed in flup >= 1.0.2 (to be released):

 http://trac.saddi.com/flup/changeset/137726cdcd67

--
Ticket URL: <http://code.djangoproject.com/ticket/6648#comment:6>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django updates" group.
To post to this group, send email to django-updates@...
To unsubscribe from this group, send email to django-updates+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: [Django] #6648: random seed identical among preforked FastCGI instances

by noreply-71 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

#6648: random seed identical among preforked FastCGI instances
---------------------------------------+------------------------------------
          Reporter:  nezroy@...  |         Owner:  nobody                          
            Status:  new               |     Milestone:                                  
         Component:  Core framework    |       Version:  SVN                            
        Resolution:                    |      Keywords:  random seed fastcgi fcgi prefork
             Stage:  Accepted          |     Has_patch:  1                              
        Needs_docs:  0                 |   Needs_tests:  0                              
Needs_better_patch:  0                 |  
---------------------------------------+------------------------------------
Changes (by anonymous):

  * stage:  Unreviewed => Accepted

--
Ticket URL: <http://code.djangoproject.com/ticket/6648#comment:7>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django updates" group.
To post to this group, send email to django-updates@...
To unsubscribe from this group, send email to django-updates+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: [Django] #6648: random seed identical among preforked FastCGI instances

by noreply-71 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

#6648: random seed identical among preforked FastCGI instances
---------------------------------------+------------------------------------
          Reporter:  nezroy@...  |         Owner:  nobody                          
            Status:  new               |     Milestone:                                  
         Component:  Core framework    |       Version:  SVN                            
        Resolution:                    |      Keywords:  random seed fastcgi fcgi prefork
             Stage:  Accepted          |     Has_patch:  1                              
        Needs_docs:  0                 |   Needs_tests:  0                              
Needs_better_patch:  0                 |  
---------------------------------------+------------------------------------
Comment (by buffi):

 The current patch is broken.
 ctime() will only produce different output each second (an example output
 is 'Mon Apr 27 14:18:38 2009'), so the same process will get the same
 random seed if two requests are handed to it during the same second.

 It would be better to use something like random.seed("%d%f" % (getpid(),
 time())) , but I'm unsure if this is the correct way to fix this issue, so
 I won't submit a patch.

--
Ticket URL: <http://code.djangoproject.com/ticket/6648#comment:8>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django updates" group.
To post to this group, send email to django-updates@...
To unsubscribe from this group, send email to django-updates+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: [Django] #6648: random seed identical among preforked FastCGI instances

by noreply-71 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

#6648: random seed identical among preforked FastCGI instances
---------------------------------------+------------------------------------
          Reporter:  nezroy@...  |         Owner:  nobody                          
            Status:  new               |     Milestone:                                  
         Component:  Core framework    |       Version:  SVN                            
        Resolution:                    |      Keywords:  random seed fastcgi fcgi prefork
             Stage:  Accepted          |     Has_patch:  1                              
        Needs_docs:  0                 |   Needs_tests:  0                              
Needs_better_patch:  0                 |  
---------------------------------------+------------------------------------
Comment (by anonymous):

 Actually by using '%d%f' % (getpid(), time()) you're usually decreasing
 the randomness of the generator since if you don't pass anything then
 os.urandom() will be used on most supporting operating systems (windows
 and linux for example), which is more random than hash() which will be
 used in the case of passing a string to seed().

--
Ticket URL: <http://code.djangoproject.com/ticket/6648#comment:9>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django updates" group.
To post to this group, send email to django-updates@...
To unsubscribe from this group, send email to django-updates+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---