andrewdavey/cassette — Issue #141: Cannot access a closed Stream exception HtmlTemplate
Author: jslaybaugh
Created: 2012-01-21 16:59:37+00:00
State: open

I'm running into an intermittent issue similar to the one described in https://github.com/andrewdavey/cassette/issues/108, but I just updated to 1.0.1 via nuget and I'm still getting the issue, so I assume its something slightly different.

My scenario is that I've got some (non pre-compiled) HtmlTemplates set up in content/templates/results. I use this template in two places. On the home page of my application, I query and list recently added/modified items and then I also have a search page in my application that uses the same template to render results from a search.

I only ever get the `Cannot access a closed Stream` exception on the Search page and at first it seemed very intermittent. At first, I thought it was something I was doing in my recursive file search routine, but after examining the full stack trace, it looks like its Cassette-related.

On top of that, I can now reliably re-create the issue by starting a search in one tab of Chrome and then opening another tab to view the Home page (again, both are referencing the same template). If while the search is running, I repeatedly refresh the home page tab, I can pretty well make the search raise an exception every time.

My guess is that (obviously) the stream is not being closed properly and its only exacerbated by the simultaneous access (which of course will happen very often once this is in production).

Here's the full stack trace:

[ObjectDisposedException: Cannot access a closed Stream.]
System.IO.__Error.StreamIsClosed() +57
System.IO.MemoryStream.set_Position(Int64 value) +9629708
Cassette.BundleProcessing.ConcatenatedAsset.OpenStreamCore() +40
Cassette.AssetBase.OpenStream() +103
Cassette.HtmlTemplates.InlineHtmlTemplateBundleRenderer.b__0(IAsset asset) +8
System.Linq.WhereSelectListIterator`2.MoveNext() +110
System.String.Join(String separator, IEnumerable`1 values) +81
Cassette.HtmlTemplates.InlineHtmlTemplateBundleRenderer.Render(HtmlTemplateBundle bundle) +105
Cassette.HtmlTemplates.HtmlTemplateBundle.Render() +15
Cassette.ReferenceBuilder.b__c(T bundle) +17
System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +151
System.String.Join(String separator, IEnumerable`1 values) +81
Cassette.ReferenceBuilder.CreateHtml(String location) +200
Cassette.<>c__DisplayClassa`1.b__9() +45
Cassette.PlaceholderTracker.ReplacePlaceholders(String html) +243
Cassette.Web.PlaceholderReplacingResponseFilter.WriteBufferedOutput() +53
Cassette.Web.PlaceholderReplacingResponseFilter.Close() +18
System.Web.HttpWriter.FilterIntegrated(Boolean finalFiltering, IIS7WorkerRequest wr) +195
System.Web.HttpResponse.FilterOutput() +159
System.Web.CallFilterExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +52
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75