Skip to content

Commit

Permalink
Prevent DatabaseServerMessenger handle leak (#12013)
Browse files Browse the repository at this point in the history
  • Loading branch information
nzdev committed Feb 27, 2022
1 parent fa0027d commit 66961d1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
Expand Up @@ -24,6 +24,7 @@ public abstract class RecurringHostedServiceBase : IHostedService, IDisposable
private TimeSpan _period;
private readonly TimeSpan _delay;
private Timer _timer;
private bool _disposedValue;

/// <summary>
/// Initializes a new instance of the <see cref="RecurringHostedServiceBase"/> class.
Expand Down Expand Up @@ -78,7 +79,24 @@ public Task StopAsync(CancellationToken cancellationToken)
return Task.CompletedTask;
}

protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
_timer?.Dispose();
}

_disposedValue = true;
}
}

/// <inheritdoc/>
public void Dispose() => _timer?.Dispose();
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
Expand Up @@ -20,6 +20,7 @@ public class InstructionProcessTask : RecurringHostedServiceBase
private readonly IRuntimeState _runtimeState;
private readonly IServerMessenger _messenger;
private readonly ILogger<InstructionProcessTask> _logger;
private bool _disposedValue;

/// <summary>
/// Initializes a new instance of the <see cref="InstructionProcessTask"/> class.
Expand Down Expand Up @@ -54,5 +55,20 @@ public override Task PerformExecuteAsync(object state)

return Task.CompletedTask;
}

protected override void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing && _messenger is IDisposable disposable)
{
disposable.Dispose();
}

_disposedValue = true;
}

base.Dispose(disposing);
}
}
}
25 changes: 24 additions & 1 deletion src/Umbraco.Infrastructure/Sync/DatabaseServerMessenger.cs
Expand Up @@ -19,7 +19,7 @@ namespace Umbraco.Cms.Infrastructure.Sync
/// <summary>
/// An <see cref="IServerMessenger"/> that works by storing messages in the database.
/// </summary>
public abstract class DatabaseServerMessenger : ServerMessengerBase
public abstract class DatabaseServerMessenger : ServerMessengerBase, IDisposable
{
/*
* this messenger writes ALL instructions to the database,
Expand All @@ -39,6 +39,7 @@ public abstract class DatabaseServerMessenger : ServerMessengerBase
private DateTime _lastPruned;
private readonly Lazy<SyncBootState?> _initialized;
private bool _syncing;
private bool _disposedValue;
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
private readonly CancellationToken _cancellationToken;

Expand Down Expand Up @@ -280,6 +281,28 @@ public override void Sync()
}
}

protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
_syncIdle?.Dispose();
}

_disposedValue = true;
}
}

/// <summary>
/// Dispose
/// </summary>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

#endregion
}
}

0 comments on commit 66961d1

Please sign in to comment.