[Jython-checkins] jython (2.5): #1854 set().pop() race condition

frank.wierzbicki jython-checkins at python.org
Mon Apr 16 18:25:00 CEST 2012


http://hg.python.org/jython/rev/6dc91e602dea
changeset:   6594:6dc91e602dea
branch:      2.5
parent:      6582:99b8d4f16639
user:        Darjus Loktevic <darjus at gmail.com>
date:        Mon Apr 16 08:42:47 2012 -0700
summary:
  #1854 set().pop() race condition

files:
  Lib/test/test_set_jy.py        |  13 ++++++++++++-
  NEWS                           |   1 +
  src/org/python/core/PySet.java |   2 +-
  3 files changed, 14 insertions(+), 2 deletions(-)


diff --git a/Lib/test/test_set_jy.py b/Lib/test/test_set_jy.py
--- a/Lib/test/test_set_jy.py
+++ b/Lib/test/test_set_jy.py
@@ -1,6 +1,8 @@
 import unittest
 from test import test_support
 
+import threading
+
 if test_support.is_jython:
     from java.io import (ByteArrayInputStream, ByteArrayOutputStream,
                          ObjectInputStream, ObjectOutputStream)
@@ -22,6 +24,16 @@
         self.assertEqual(s & foo, 'rand')
         self.assertEqual(s ^ foo, 'rxor')
 
+    def test_pop_race(self):
+        # issue 1854
+        nthreads = 200
+        # the race might not happen the first time so we try a few just in case
+        for i in xrange(4):
+            s = set(range(200))
+            threads = [threading.Thread(target=s.pop) for i in range(nthreads)]
+            for t in threads: t.start()
+            for t in threads: t.join()
+            self.assertEqual(len(s), 0)
 
 class SetInJavaTestCase(unittest.TestCase):
 
@@ -61,7 +73,6 @@
         unserializer = ObjectInputStream(input)
         self.assertEqual(s, unserializer.readObject())
 
-
 def test_main():
     tests = [SetTestCase]
     if test_support.is_jython:
diff --git a/NEWS b/NEWS
--- a/NEWS
+++ b/NEWS
@@ -5,6 +5,7 @@
 
 Jython 2.5.3b2
   Bugs Fixed
+    - [ 1854 ] set().pop() race condition
     - [ 1730 ] functools.partial incorrectly makes __doc__ property readonly
     - [ 1537 ] expat: org.python.apache.xerces.parsers.SAXParser
     - [ 1268 ] SAX parsers wants to load external DTDs, causing an exception
diff --git a/src/org/python/core/PySet.java b/src/org/python/core/PySet.java
--- a/src/org/python/core/PySet.java
+++ b/src/org/python/core/PySet.java
@@ -242,7 +242,7 @@
     }
 
     @ExposedMethod(doc = BuiltinDocs.set_pop_doc)
-    final PyObject set_pop() {
+    final synchronized PyObject set_pop() {
         Iterator iterator = _set.iterator();
         try {
             Object first = iterator.next();

-- 
Repository URL: http://hg.python.org/jython


More information about the Jython-checkins mailing list