Bug in AzureFileSystem ListFiles method

Topics: Troubleshooting
Nov 29, 2011 at 6:06 PM

Hello There,

I don't know how to submit a patch sorry :(

I have found a bug in AzureFileSystem class in ListFiles method this method uses the yield keyword ro return the files and it wraps the "yield return" in a using statment for the HttpContextWeaver in my case this resulted in returning from this method while having an altered HttpContext.Current property with an empty "Items" collection because the Weaver was not disposed when this method returned. the fix I have done is to replace this method with the following code. I took into consideration the possibility of having ListBlobWithPrefix returns an enumerable that handles contiuation tokens (this means the enumerable will contact azure storage servers multiple times)

 

 	public IEnumerable<IStorageFile> ListFiles(string path) {
            path = path ?? String.Empty;
            
            EnsurePathIsRelative(path);
 
            string prefix = Combine(Combine(Container.Name, _root), path);
            
            if ( !prefix.EndsWith("/") )
                prefix += "/";
            //ListBlobWithPrefix may handle continuation tokens 
            foreach (var blobItem in Weav(() => BlobClient.ListBlobsWithPrefix(prefix).OfType<CloudBlockBlob>()))
            {
                // ignore directory entries
                if(blobItem.Uri.AbsoluteUri.EndsWith(FolderEntry))
                    continue;
 
                yield return new AzureBlobFileStorage(blobItem, _absoluteRoot);
            }
        }
        protected IEnumerable<T> WeavEnumerable<T>(IEnumerable<T> stream)
        {
            var enumerator = Weav(() => stream.GetEnumerator());
            while (Weav(() => enumerator.MoveNext()))
            {
                //don't think this Weav is needed but just in case
                yield return Weav(() => enumerator.Current);
            }
        }
        protected T Weav<T>(Func<T> f)
        {
            T result;
            using (new HttpContextWeaver())
            {
                result = f();
            }
            return result;
        }
 
        protected IEnumerable<T> Weav<T>(Func<IEnumerable<T>> f)
        {
            IEnumerable<T> result;
            using (new HttpContextWeaver())
            {
                result = f();
                result = WeavEnumerable(result);
            }
            return result;
        }
Best Regards,
Shady Sayed
Coordinator
Nov 29, 2011 at 7:44 PM

Can you please file a bug in the issue tracker for this?

Thanks!

Nov 30, 2011 at 7:59 AM

I have filed the bug, and made a few enhancements on the fix there.

sorry for not doing so earlier as I was up coding late and I wasn't thinking right :)

Coordinator
Nov 30, 2011 at 8:00 AM

No problem, thanks!