Hi,
A deadlock occurs in the following situation: a first client program opens an iterative query. For each iteration, this program does some processing and sends another reading request to BaseX (using another BaseX session). All works fine until a second client program (or another thread) sends an updating command to BaseX (like optimize for instance). This locks BaseX server. To unlock it, you have to kill the first program.
I have read BaseX server code and found the reason for this behavior in the class org.basex.core.Lock:
- with the iterative query, there is always at least one reader alive (readers=1).
- when the updating query is received, it is put in the queue (index 0) and remains in it as long as there is a reading query running (that is to say, as long as the iterative reading query is running).
- then a second reading request is received, it is put in the queue (index 1 as there is already the updating query in the queue). As it is only the second item of the queue, it remains in the queue as long as the first item in the queue (the updating query) has not been processed (BaseX processes the requests in the order of arrival, FIFO queue). But this first item can not be processed because there is the iterative reading query running. All queries are thus locked.
Some may say that we should not send another query while we are in the loop of an iterative query but in our context of many sites being developed by several developers, it is possible that a developer codes this and we do not want BaseX to be locked in this case (whatever it is a mistake of the developer or not).
I have found a solution to this problem by modifying the org.basex.core.Lock class. You will find my code hereafter. I do not use a queue anymore and i use a static mutex (called queueMutex) to synchronize all pending queries (threads). The "drawback" of this solution is that the queries are not processed anymore in the order of arrival but randomly.
What do you think of this solution ? Do you plan to update BaseX locking mechanism ?
I'm using BaseX 6.7.1 but I have seen that Lock.java has not been changed in BaseX 6.7.2.
Here is my code :
package org.basex.core;
import java.util.Date;
//import java.util.LinkedList;
import java.util.Random;
import org.basex.util.Util;
/**
* Management of executing read/write processes.
* Supports multiple readers, limited by {@link MainProp#PARALLEL},
* and single writers (readers/writer lock).
*
* @author BaseX Team 2005-11, BSD License
* @author Christian Gruen
*/
final class Lock {
/** Queue for all waiting processes. */
// private final LinkedList<Object> queue = new LinkedList<Object>();
/** Mutex object. */
private final Object mutex = new Object();
/** Database context. */
private final Context ctx;
/** Static mutex used to synchronize all pending queries. **/
private final static Object queueMutex = new Object();
/** Number of active readers. */
private int readers;
/** Writer flag. */
private boolean writer;
/**
* Default constructor.
* @param c context
*/
Lock(final Context c) {
ctx = c;
}
/**
* Modifications before executing a command.
* @param w writing flag
*/
void lock(final boolean w) {
synchronized(mutex) {
int code = new Random(new Date().getTime()).nextInt();
// final Object o = new Object();
// queue.add(o);
try {
while(true) {
synchronized(queueMutex) {
// if(o == queue.get(0) && !writer) {
if(!writer) {
if(w) {
if(readers == 0) {
writer = true;
break;
}
} else if(readers < Math.max(ctx.mprop.num(MainProp.PARALLEL), 1)) {
++readers;
break;
}
}
}
mutex.wait();
}
} catch(final InterruptedException ex) {
Util.stack(ex);
}
// queue.remove(0);
}
}
/**
* Modifications after executing a command.
* @param w writing flag
*/
synchronized void unlock(final boolean w) {
synchronized(mutex) {
if(w) {
writer = false;
} else {
--readers;
}
mutex.notifyAll();
}
}
}