From b803d5ab9b6aae1316314efcbbb80e3f1ab6322a Mon Sep 17 00:00:00 2001 From: Diva Canto Date: Sun, 4 Oct 2009 09:44:04 -0700 Subject: Make sure to close the file streams on the FlotsamCache, even if something fails. --- OpenSim/Region/CoreModules/Asset/FlotsamAssetCache.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'OpenSim/Region/CoreModules') diff --git a/OpenSim/Region/CoreModules/Asset/FlotsamAssetCache.cs b/OpenSim/Region/CoreModules/Asset/FlotsamAssetCache.cs index 817e0d4..1dc40bc 100644 --- a/OpenSim/Region/CoreModules/Asset/FlotsamAssetCache.cs +++ b/OpenSim/Region/CoreModules/Asset/FlotsamAssetCache.cs @@ -323,13 +323,13 @@ namespace Flotsam.RegionModules.AssetCache string filename = GetFileName(id); if (File.Exists(filename)) { + FileStream stream = null; try { - FileStream stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read); + stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read); BinaryFormatter bformatter = new BinaryFormatter(); asset = (AssetBase)bformatter.Deserialize(stream); - stream.Close(); UpdateMemoryCache(id, asset); @@ -349,6 +349,11 @@ namespace Flotsam.RegionModules.AssetCache { LogException(e); } + finally + { + if (stream != null) + stream.Close(); + } } @@ -493,6 +498,7 @@ namespace Flotsam.RegionModules.AssetCache private void WriteFileCache(string filename, AssetBase asset) { + Stream stream = null; try { // Make sure the target cache directory exists @@ -505,10 +511,9 @@ namespace Flotsam.RegionModules.AssetCache // Write file first to a temp name, so that it doesn't look // like it's already cached while it's still writing. string tempname = Path.Combine(directory, Path.GetRandomFileName()); - Stream stream = File.Open(tempname, FileMode.Create); + stream = File.Open(tempname, FileMode.Create); BinaryFormatter bformatter = new BinaryFormatter(); bformatter.Serialize(stream, asset); - stream.Close(); // Now that it's written, rename it so that it can be found. File.Move(tempname, filename); @@ -522,6 +527,9 @@ namespace Flotsam.RegionModules.AssetCache } finally { + if (stream != null) + stream.Close(); + // Even if the write fails with an exception, we need to make sure // that we release the lock on that file, otherwise it'll never get // cached -- cgit v1.1