Why does URLStream sometimes not fire Event.COMPLETE?

129 Views Asked by At

I've got an application that is downloading several large binary files and saving them to disk. On some machines it works fine and on some other machines every once in a while a download will proceed to 99.9% complete and the URLStream object will not fire Event.COMPLETE

This is almost identical to the issue that appears here:

Why does URLStream complete event get dispatched when the file is not finished loading?

I've tried using the 'Cache Bust' method described in one of the answers but still no dice.

Any help would be appreciated.

Here is some sample code to help illustrate what I am trying to do:

var contentURL:String = "http://some-large-binary-file-in-amazon-s3.tar";

var stream:URLStream = new URLStream();
stream.addEventListener(Event.COMPLETE, function(e:Event):void{
    //This should fire when the file is done downloading
    //On some systems this fails to fire once in a while
    //On other systems it never fails to fire               
});
stream.addEventListener(ProgressEvent.PROGRESS, function(pe:ProgressEvent):void{
    //Write the bytes available in the stream and save them to disk
   //Note that a download will reach 100% complete in terms of total progress but the 'complete' event might still not fire.
});

var urlRequest:URLRequest = new URLRequest(contentURL);
//Here we might add some headers to the URL Request for resuming a file
//but they don't matter, the 'Event.COMPLETE' will fail to fire with our without
//these headers
addCustomHeaders( urlRequest );

stream.load( urlRequest );
1

There are 1 best solutions below

2
On

Imo this is a code meant to fail where you purposely give up any control on whatever is going on and just assume that everything would work by itself and go well. I never had any problems whatsoever with the URLStream class but here's basically what I never do:

  1. I never not register all the different error event available (you don't register any).

  2. I never use anonymous listeners. Even though they are supposed to not be GC until the download is complete this is imo an unnecessary unsafe bet especially since it's not rare for the URLStream to idle a little while loading the last bits. I would not be surprised if removing those anonymous listeners would actually fix the problem.