pSpaces / jSpace

Programming with Spaces in Java
MIT License
17 stars 12 forks source link

ConcurrentModificationException from SequentialSpace class #21

Open Uabanur opened 6 years ago

Uabanur commented 6 years ago

When performing queryAll on a SequentialSpace in a SpaceRepository, ConcurrentModificationExceptions are occasionally thrown. The code where this is observed most often is:

    public String[] getUsers()
    {
        List<Object[]> users = this.queryAll(new FormalField(String.class));
        String[] users_string = new String[users.size()];
        for (int i = 0; i < users.size(); i++)
        {
            users_string[i] = (String) users.get(i)[0];
        }

        return users_string;
    }

Here the ConcurrentModificationException is thrown from the line with queryAll(...). Looking in the SequentialSpace class, we see that the methods findTuple(...)and findAllTuples(...)are not synchronised in the java monitor.

This may be a deliberate choice to make it possible to query concurrently for several threads, but the concurrent exceptions are thrown since the found elements are returned, instead of a copy of their data.

michele-loreti commented 6 years ago

Both the methods findTuple(...) and findAllTuple(...) are only used in methods that are synchronized. For this reason the exception cannot originates from them. Note that ConcurrentModificationExceptions is generated when the content of an Iterable is changed while it is subject of an iteration (foreach statement). Please, can you share the stack trace you get?