Can not access a closed Stream?

Hi. I need some help with a problem I run into randomly. I will get the following error sometimes…

,
30>MSBUILD : OctoPack error OCTONUGET: Can not access a closed Stream. [C:\MyProject\Web.csproj]
30>MSBUILD : OctoPack error OCT-1805672349: There was an error calling NuGet. Please see the output above for more details. Command line: ‘C:\MyProject\packages\OctoPack.3.0.31\tools\NuGet.exe’ pack C:\MyProject\obj\octopacking\Web.nuspec" -NoPackageAnalysis -BasePath C:\MyProject" -OutputDirectory C:\MyProject\obj\octopacked" -Version 1.0.4377 [C:\MyProject\Web.csproj]
30>MSBUILD : OctoPack error OCT-1805672349: System.Exception: There was an error calling NuGet. Please see the output above for more details. Command line: ‘C:\MyProject\packages\OctoPack.3.0.31\tools\NuGet.exe’ pack C:\MyProject\obj\octopacking\Web.nuspec" -NoPackageAnalysis -BasePath C:\MyProject" -OutputDirectory C:\MyProject\obj\octopacked" -Version 1.0.4377 [C:\MyProject\Web.csproj]
MSBUILD : OctoPack error OCT-1805672349: at OctoPack.Tasks.CreateOctoPackPackage.RunNuGet(String specFilePath, String octopacking, String octopacked, String projectDirectory) in y:\work\46cfb6001f03d701\source\OctoPack.Tasks\CreateOctoPackPackage.cs:line 520 [C:\MyProject\Web.csproj]
MSBUILD : OctoPack error OCT-1805672349: at OctoPack.Tasks.CreateOctoPackPackage.Execute() in y:\work\46cfb6001f03d701\source\OctoPack.Tasks\CreateOctoPackPackage.cs:line 189 [C:\MyProject\Web.csproj]
,

I should point out that I use ProGet as my NuGet server. If I retry MSBuild, usually it goes away but it seems to happen once every few builds. Is this a more of a problem with Octopus not closing a stream or ProGet not opening the stream in time? If it is an Octopus problem, is there a workaround (besides retrying the build until it works) or a fix?

Thanks!

Hi,

Thanks for getting in touch! Sorry for the delay in getting back to you. From what we can see with the error we believe the part that the error is coming from is between Nuget.exe and ProGet. The stream we believe it is referring to is when NuGet tries to move the package to the folder. I wonder if you changed from OctoPackPublishPackageToFileShare to OctoPackPublishPackageToHttp and see if it makes a difference at all.

Hope that helps!
Vanessa

I have this problem using TFS 2013, can not access closed stream, it seems to pop up quite irregurlarly… very annoying

Hi Jonas,

Do you also use ProGet or another external feed?

Vanessa

Hi,

We also have this problem. It happens quite often. We don’t use ProGet.
I see this problem when I build from command line with OctoPackPublishPackageToFileShare.

/Jan

Update: I also get this when I run without the OctoPackPublishPackageToFileShare property and just plain RunOctoPack. I’ve a situation now where one project fails almost every time. To me this looks like a NuGet.exe issue

/Jan

Another update: Our build contains a lot of solutions. Some of these are top level applications that are packaged with OctoPack. To speed up the build process we build these in parallel. Works fine, except for OctoPack. In our case this seem to be what is causing the “Can not access a closed Stream” issue in NuGet/OctoPack. If I change the build to run everything in sequence then it always works. Running in parallel with runoctopack=true fails very often with “Can not access a closed Stream”. Running the build in parallel with runoctopack=false also work fine, but that is not what we want.:slight_smile:

/Jan

Hi Vanessa no Im not using Proget.

We use TFS 2013 Build server and I have the same issue as Jan.
We have one solution with multiple projects and we do OctoPackPublishPackageToHttp. I suspect tht this is a Nuget issue.

How do I pack and publish using Octo.exe and Nuget? I think we will have to do it this way in a post build script…

Jan, do you use TFS 2013?
If so how do you specify running the build in sequence?

Hi All,

I have created an issue in GitHub to investigate that you can track here: https://github.com/OctopusDeploy/Issues/issues/1684
We will see if we can figure out if its only NuGet or Octopack or if there is a workaround that can be used.

Vanessa

Hi all,
Is this only happening through TFS Build Server? I have manually run OctoPack via the command line with multiple large packages. I’m setting the OctoPackPublishPackageToFileShare parameter as specified by @Jan and so far been unable to reproduce when running in multiple command windows. By the sounds of things this is a NuGet.exe issue but until we can reproduce it will be difficult to make an effective fix.
Thanks for your feedback.
Cheers,
Rob

Hi,

Thanks for looking into this.

This happens from command line(and GUI) with a build using FinalBuilder. FinalBuilder makes it easy to run for parts of the build in parallel.

I believe I also observed the same problem when I experimented with parallel builds using powershell and Invoke.Build.

This looks like a race condition in nuget.exe to me. It does not happen the same place every time, but in a build producing 40+ nuget packages it happens almost every time.

/Jan

On 13. aug. 2015, at 06.31, Robert Erez <tender2+d5ee0436d5@tenderapp.commailto:tender2+d5ee0436d5@tenderapp.com> wrote:

Jan,
Are you able to reproduce with just Nuget.exe in a command line? If we can isolate and reproduce the specific scenario we might be able to work around it. I have been manually calling

msbuild “OctoConsole\OctoConsole.sln” /p:RunOctoPack=true /p:OctoPackPublishPackageToFileShare=C:\temp

and so far been unable to get the Can not access a closed Stream error.

When you run it manually and can see the NuGet.exe output, does it appear to happen during the nupkg creation or when it gets published, in this case to a file share?
Cheers,
Rob

Here is my msbuild output from today:
50>OctoPack:
OctoPack: bin64\mkl_custom.dll
50>MSBUILD : OctoPack error OCTONUGET: System.ObjectDisposedException: Can not access a closed Stream. [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
50>MSBUILD : OctoPack error OCTONUGET: at System.IO.Compression.DeflateStream.EnsureNotDisposed() [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
50>MSBUILD : OctoPack error OCTONUGET: at System.IO.Compression.DeflateStream.Flush() [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
50>MSBUILD : OctoPack error OCTONUGET: at MS.Internal.IO.Packaging.CompressStream.Flush() [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
50>MSBUILD : OctoPack error OCTONUGET: at MS.Internal.IO.Zip.ProgressiveCrcCalculatingStream.Flush() [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
50>MSBUILD : OctoPack error OCTONUGET: at MS.Internal.IO.Zip.ZipIOLocalFileBlock.FlushExposedStreams() [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
50>MSBUILD : OctoPack error OCTONUGET: at MS.Internal.IO.Zip.ZipIOLocalFileBlock.UpdateReferences(Boolean closingFlag) [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
50>MSBUILD : OctoPack error OCTONUGET: at MS.Internal.IO.Zip.ZipIOBlockManager.SaveContainer(Boolean closingFlag) [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
50>MSBUILD : OctoPack error OCTONUGET: at MS.Internal.IO.Zip.ZipIOBlockManager.SaveStream(ZipIOLocalFileBlock blockRequestingFlush, Boolean closingFlag) [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
50>MSBUILD : OctoPack error OCTONUGET: at MS.Internal.IO.Zip.ZipIOModeEnforcingStream.Dispose(Boolean disposing) [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
50>MSBUILD : OctoPack error OCTONUGET: at System.IO.Stream.Close() [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
50>MSBUILD : OctoPack error OCTONUGET: at System.IO.Packaging.PackagePart.Close() [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
50>MSBUILD : OctoPack error OCTONUGET: at System.IO.Packaging.Package.DoClose(PackagePart p) [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
50>MSBUILD : OctoPack error OCTONUGET: at System.IO.Packaging.Package.DoOperationOnEachPart(PartOperation operation) [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
50>MSBUILD : OctoPack error OCTONUGET: at System.IO.Packaging.Package.System.IDisposable.Dispose() [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
50>MSBUILD : OctoPack error OCTONUGET: at NuGet.PackageBuilder.Save(Stream stream) [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
50>MSBUILD : OctoPack error OCTONUGET: at NuGet.Commands.PackCommand.BuildPackage(PackageBuilder builder, String outputPath) [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
50>MSBUILD : OctoPack error OCTONUGET: at NuGet.Commands.PackCommand.BuildFromNuspec(String path) [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
50>MSBUILD : OctoPack error OCTONUGET: at NuGet.Commands.PackCommand.BuildPackage(String path) [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
50>MSBUILD : OctoPack error OCTONUGET: at NuGet.Commands.PackCommand.ExecuteCommand() [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
50>MSBUILD : OctoPack error OCTONUGET: at NuGet.Commands.Command.Execute() [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
50>MSBUILD : OctoPack error OCTONUGET: at NuGet.Program.Main(String[] args) [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
50>MSBUILD : OctoPack error OCT-1805672349: There was an error calling NuGet. Please see the output above for more details. Command line: ‘E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\packages\OctoPack.3.0.42\tools\NuGet.exe’ pack “E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\obj\octopacking\Apple.Banana.SystemTool.nuspec” -NoPackageAnalysis -BasePath “E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool” -OutputDirectory “E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\obj\octopacked” -Version 1.0.5707.24210 -Verbosity Detailed [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
50>MSBUILD : OctoPack error OCT-1805672349: System.Exception: There was an error calling NuGet. Please see the output above for more details. Command line: ‘E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\packages\OctoPack.3.0.42\tools\NuGet.exe’ pack “E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\obj\octopacking\Apple.Banana.SystemTool.nuspec” -NoPackageAnalysis -BasePath “E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool” -OutputDirectory “E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\obj\octopacked” -Version 1.0.5707.24210 -Verbosity Detailed [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
MSBUILD : OctoPack error OCT-1805672349: at OctoPack.Tasks.CreateOctoPackPackage.RunNuGet(String specFilePath, String octopacking, String octopacked, String projectDirectory) in y:\work\46cfb6001f03d701\source\OctoPack.Tasks\CreateOctoPackPackage.cs:line 551 [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]
MSBUILD : OctoPack error OCT-1805672349: at OctoPack.Tasks.CreateOctoPackPackage.Execute() in y:\work\46cfb6001f03d701\source\OctoPack.Tasks\CreateOctoPackPackage.cs:line 196 [E:\Builds\26\Banana\BananaNightly2013\src\Banana\Dev\Apple.Banana.SystemTool\Apple.Banana.SystemTool.csproj]

And the msbuild args attached:

Hi All,
Thanks for the detailed logs.
This definitely appears to be something fishy going on within NuGet itself. If I just run the NuGet.exe pack command across several completely different packages in parallel then I get the Cannot Access A Closed Stream error.
I have added these details to a Git Hub Issue on the NuGet repository

I will take a look at the NuGet source itself to see if there is anything obvious that I’m missing but it looks like there is something going on in NuGet which will need to be sorted for this issue to go away.

Will keep an eye on the ticket with the NuGet team and update this thread accordingly.
Sorry for the bad news guys,
Cheers,
Rob

Sweet! In the meantime, since this seems to be a problem when running several nuget pack commands in parallell, Is there a workaround when calling OctoPack to make it run the nuget pack commands sequentially? Like a parameter…?

I mean I would rather have a OctoPack command running slow but succesfully than having our gated-checkins fail because Nuget cant handle multi threading.

or nu get cant handle working with packing in multiple processes maybe either way a parameter to run packing sequentially would be handy

Hi jonas,
At the moment we would advise the standard work around to be to just run the offending builds in sequence. As this is a failure in NuGet’s library we are trying to get the fix on their side working first. Please comment on the ticket that has been raised on their queue (https://github.com/NuGet/NuGet.CommandLine/issues/18) to ensure its importance is brought to their attention.
Hopefully you are able to get your builds working again. Let us know if you have any further difficulties with this.
Cheers,
Rob

Thank you Robert!

Ok, I just checked the logs and it seems that the build is done with MultiProc= True so I added the msbuild parameter /m:1 and so far we have not seen any failed build due to “can not access closed stream”.

@Jonas.haggstrom

I’m going to try that parameter out. We’ve since upgraded to TFS 2015 and the problem seems to be even more frequent now, with a single build taking 3 or 4 tries to succeed.

@Robert

I’m definitely going to comment on that ticket!