Solving Race conditions with ActionFunctions

I have had a big problem with race conditions in ActionFunctions corrupting my viewstate.

When I call an AF to update one thing, and then click a button to do another, if the second finishes first, and then the actionfunction completes, ViewState gets reset to the result of the actionfunction.

This is a pretty straightforward symptom of ViewState. It is also a problem when a component calls one function, and then the page calls another, since both modify the entire viewstate.

I attempted to solve this by creating a window level function runAf(actionfunction), and in the oncomplete of every actionfunction call afComplete();

function afComplete(event){

    if (window.AF_Queue.length == 0) {
        window.RunningAF = false;
     } else {
        var fn = window.AF_Queue.shift();
    return false;

function runAf(af) {
    if (!window.RunningAF) {
       window.RunningAF = true;

    } else {

Both the page and controller use this to run their action functions. Unfortunately, nothing happens, and the actionfunctions fail to execute entirely.

I am at wits end on this, and don’t want to re-write something that works perfectly, just because clicking buttons too fast causes it to break.

I considered moving to Remote Functions, but I don’t trust that they will work used this way based on my experience with actionfunctions.

Any insight?


One simple workaround could be that if an event fires or a button is clicked, you put a fullsize div z-index 9999999 on top of the entire page. This can prevent any ui elements from being clickable.

Now depending on how you rerender the page, either it will refresh entirely and the div is just gone, or you have to remove it manually, e.g. by a script inside the rerenderd markup.

Setting the transparency of the div and also adding a kind of spinner gif it even looks ok.

This may not solve all issues. Since you can suppress ui interactons after any events or other ui interactions, you still may get race conditions triggered by events after events – e. g. repetitive resizes or keystrokes may still cause a race condition.

I don’t think that remote functions generally prevent race conditions. But they may complete way faster than action functions and simply that could reduce the chances to a sufficient degree.

Source : Link , Question Author : Alex H , Answer Author : Uwe Heim

Leave a Comment