Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 98d5503

Browse files
committed
Fix premature cache disposal
Now that we're not creating a million copies of the same cache every time something wants something for the cache, `ModelService` can't just willy-nilly dispose of the `hostCache` object when it feels like it, because that cache object is in itself cached in the factory and will be reused by other services, if needed. The factory is the one responsible for keeping the lifetime of its cached objects (and yes, now it should probably not be called a factory anymore, it should be a service but oh well), and so now it needs to be disposable and take care of things.
1 parent 80c405e commit 98d5503

File tree

6 files changed

+44
-11
lines changed

6 files changed

+44
-11
lines changed

src/GitHub.App/Factories/HostCacheFactory.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,23 @@ public IBlobCache Create(HostAddress hostAddress)
3939

4040
IOperatingSystem OperatingSystem { get { return operatingSystem.Value; } }
4141
IBlobCacheFactory BlobCacheFactory { get { return blobCacheFactory.Value; } }
42+
43+
bool disposed;
44+
protected virtual void Dispose(bool disposing)
45+
{
46+
if (disposing)
47+
{
48+
if (disposed) return;
49+
disposed = true;
50+
if (blobCacheFactory.IsValueCreated)
51+
blobCacheFactory.Value.Dispose();
52+
}
53+
}
54+
55+
public void Dispose()
56+
{
57+
Dispose(true);
58+
GC.SuppressFinalize(this);
59+
}
4260
}
4361
}

src/GitHub.App/Factories/IBlobCacheFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using Akavache;
2+
using System;
23

34
namespace GitHub.Factories
45
{
5-
public interface IBlobCacheFactory
6+
public interface IBlobCacheFactory : IDisposable
67
{
78
IBlobCache CreateBlobCache(string path);
89
}

src/GitHub.App/Factories/IHostCacheFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using Akavache;
22
using GitHub.Primitives;
3+
using System;
34

45
namespace GitHub.Factories
56
{
6-
public interface IHostCacheFactory
7+
public interface IHostCacheFactory : IDisposable
78
{
89
IBlobCache Create(HostAddress hostAddress);
910
}

src/GitHub.App/Factories/RepositoryHostFactory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ protected virtual void Dispose(bool disposing)
5151

5252
loginCache.Dispose();
5353
avatarProvider.Dispose();
54+
hostCacheFactory.Dispose();
5455
disposed = true;
5556
}
5657
}

src/GitHub.App/Factories/SqlitePersistentBlobCacheFactory.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
using System;
55
using System.Collections.Generic;
66
using System.ComponentModel.Composition;
7+
using System.Reactive.Disposables;
78

89
namespace GitHub.Factories
910
{
1011
[Export(typeof(IBlobCacheFactory))]
1112
[PartCreationPolicy(CreationPolicy.Shared)]
1213
public class SqlitePersistentBlobCacheFactory : IBlobCacheFactory
1314
{
15+
readonly CompositeDisposable disposables = new CompositeDisposable();
1416
static readonly Logger log = LogManager.GetCurrentClassLogger();
1517
Dictionary<string, IBlobCache> cache = new Dictionary<string, IBlobCache>();
1618

@@ -24,6 +26,7 @@ public IBlobCache CreateBlobCache(string path)
2426
{
2527
var c = new SQLitePersistentBlobCache(path);
2628
cache.Add(path, c);
29+
disposables.Add(c);
2730
return c;
2831
}
2932
catch(Exception ex)
@@ -32,5 +35,23 @@ public IBlobCache CreateBlobCache(string path)
3235
return new InMemoryBlobCache();
3336
}
3437
}
38+
39+
bool disposed;
40+
protected virtual void Dispose(bool disposing)
41+
{
42+
if (disposing)
43+
{
44+
if (disposed) return;
45+
disposed = true;
46+
disposables.Dispose();
47+
}
48+
}
49+
50+
public void Dispose()
51+
{
52+
Dispose(true);
53+
GC.SuppressFinalize(this);
54+
}
55+
3556
}
3657
}

src/GitHub.App/Services/ModelService.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,6 @@ protected virtual void Dispose(bool disposing)
277277
if (disposing)
278278
{
279279
if (disposed) return;
280-
281-
try
282-
{
283-
hostCache.Dispose();
284-
}
285-
catch (Exception e)
286-
{
287-
log.Warn("Exception occured while disposing host cache", e);
288-
}
289280
disposed = true;
290281
}
291282
}

0 commit comments

Comments
 (0)