Skip to content

Violation of atomicity on set difference operations #396

@luisggpina

Description

@luisggpina

Hi,

We're a research group focused on testing concurrent runtimes. Our work-in-progress prototype found a behavior that happens in Jython but not in Python 2.7 or even 3.14 when using concurrent operations on different threads on the same set. The program below shows the wrong result:

import threading
import sys
from collections import *
def t0(s,res):
    s.difference_update(set([2, 3]))
def t1(s,res):
    res.append(s.difference(set([2, 3])))

def Test():
  s =  set([1, 2, 3])
  threads=[]
  res = []
  threads.append(threading.Thread(target= t0, args=(s,res)))
  threads.append(threading.Thread(target= t1, args=(s,res)))
  for i in range(0, len(threads)):
      threads[i].start()
  for i in range(0, len(threads)):
      threads[i].join()
  for i in range(0, len(res)):
      if res[i] != set([1]):
          print("found bug: " + str(res))

print("test begin...")
for i in range(0,50000):
  threads = []
  if i % 1000 == 0:
      print(i)
  for i in range(0,100):
      threads.append(threading.Thread(target= Test))
  for t in threads:
      t.start()
  for t in threads:
      t.join()
print("test Done")

Here's the output on a sample execution:

test begin...
0
found bug: [set([1, 2])]
found bug: [set([1, 2, 3])]
found bug: [set([1, 2])]
found bug: [set([1, 2, 3])]
found bug: [set([1, 3])]
found bug: [set([1, 3])]
found bug: [set([1, 2])]
found bug: [set([1, 2, 3])]
found bug: [set([1, 3])]
found bug: [set([1, 2, 3])]
found bug: [set([1, 2])]

A set starts as set([1,2,3]) and two threads t0 and t1 make a concurrent operation on it: difference_update(set([2,3])) and difference(set([2,3])), respectively.

As a result, after the execution the set should be set([1]) (which it is), and t1 should observe the same result set([1]).

It appears that, as the program above shows, t1 can observe set([1,2]), set([1,3]), and set([1,2,3]). This behavior is impossible in Python 2.X and 3.X.

We tested this program on the most recent self-contained jar file available for download.

@mqbal is part of the team, adding them so they get notified about further discussion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions