XUpload - Race Conditions with XUPload

Message
Author
Silverado
Posts: 4
Joined: May 26, 2008 6:31 pm

Race Conditions with XUPload

#1 Postby Silverado » Jun 02, 2008 8:27 am

If you have a pop-up that automatically closes and and outstanding XMLHttpRequest, XUPload will give javascript errors sometimes.

The scenario is this:

if the XMLHttpRequest is in the state to enter the jahDone Function and you hit stop upload and the pop-up closes, the req variable can go away and that is bad.

I have updated xstatus.js to not close the pop-up window (either with the the stop transfer button or the close button of the pop-window) until any pending XML requests have completed.

Tested in Firefox and IE7.

I also noticed that the var req, wasn't defined anywhere.

Add this code to the top somewhere in the xstatus.js code

Code: Select all

// if we are going to close the pop-up
var windowClosing = false;
// not sure why this wasn't previously defined.
var req = null;
var wpTID;
// I want the close function of the pop-up to stop the upload transfer
window.onunload = ClosePopUp;

function ClosePopUp()
{
    StopUpload(false);
}

function WaitPopupClose()
{
    if (!req || !callInProgress(req))
    {
	popupClose();
	clearInterval(wpTID);
    }
}
and change the code in StopUpload

Code: Select all

function StopUpload(no_close)
{
    var op;
    var agt=navigator.userAgent.toLowerCase();
    var is_opera = (agt.indexOf("opera") != -1);
    if(window.parent.frames['xupload']){op=window.parent;} else {op=window.opener;}
    if (navigator.appVersion.indexOf("Safari")>0)
    {
        op.location.reload( true );
    }
    else if (!document.all || is_opera)
    {
        window.stop();
        op.frames['xupload'].stop();
    }
    else
    {
        window.document.execCommand('Stop');
        op.frames['xupload'].document.execCommand('Stop');
    }
    if(no_close)return;
    // wait for any pending XML transactions to complete
    if (!windowClosing)
    {
    	windowClosing = true;
 
    	if (req && callInProgress(req))
    	{
	    wpTID = setInterval('WaitPopupClose()', 100);
    	}
    	else
        {
    	    popupClose();
    	}
    }
}
And in the jahDone function

Code: Select all

function jahDone(url)
{
    if (req.readyState == 4)
    {
	// if the window is about to close, don't start another request
	if (!windowClosing)
	{
	        if (req.status == 200)
        	{
            	results = req.responseText;
            	window.clearTimeout(timeoutId);
            	try {eval(results);} catch(err) {timeoutId = window.setTimeout(function(){ jah(url); }, 1000 );}
        	}
		else if(req.status == 500)
		{
			timeoutId = window.setTimeout(function(){ jah(url); }, 1000 );
		}
	}
    }
}

larrymcp
Posts: 5
Joined: Apr 29, 2009 3:46 am

#2 Postby larrymcp » May 02, 2009 7:57 pm

Just wanted to say thanks for this post. That sounds like a good idea, and I have made these changes in my version too.

- Larry McP.