Skip to content

Commit 4ecfcc6

Browse files
committed
Fix leaking transportInfrastructure instance
1 parent fa6cae5 commit 4ecfcc6

File tree

2 files changed

+76
-48
lines changed

2 files changed

+76
-48
lines changed

src/ServiceControl.Audit/Auditing/AuditIngestion.cs

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -78,65 +78,97 @@ async Task EnsureStarted(CancellationToken cancellationToken = default)
7878

7979
if (!unitOfWorkFactory.CanIngestMore())
8080
{
81-
if (queueIngestor != null)
82-
{
83-
var stoppable = queueIngestor;
84-
queueIngestor = null;
85-
logger.Info("Shutting down due to failed persistence health check. Infrastructure shut down commencing");
86-
await stoppable.StopReceive(cancellationToken);
87-
logger.Info("Shutting down due to failed persistence health check. Infrastructure shut down completed");
88-
}
89-
81+
await StopAndTeardownInfrastructure(cancellationToken);
9082
return;
9183
}
9284

93-
if (queueIngestor != null)
85+
await SetUpAndStartInfrastructure(cancellationToken);
86+
}
87+
catch (Exception e)
88+
{
89+
try
9490
{
95-
logger.Debug("Ensure started. Already started, skipping start up");
96-
return; //Already started
91+
await StopAndTeardownInfrastructure(cancellationToken);
9792
}
93+
catch (Exception teardownException)
94+
{
95+
throw new AggregateException(e, teardownException);
96+
}
97+
98+
throw;
99+
}
100+
finally
101+
{
102+
logger.Debug("Ensure started. Start/stop semaphore releasing");
103+
startStopSemaphore.Release();
104+
logger.Debug("Ensure started. Start/stop semaphore released");
105+
}
106+
}
98107

99-
logger.Info("Ensure started. Infrastructure starting");
108+
async Task SetUpAndStartInfrastructure(CancellationToken cancellationToken)
109+
{
110+
if (queueIngestor != null)
111+
{
112+
logger.Debug("Infrastructure already Started");
113+
return;
114+
}
100115

116+
try
117+
{
118+
logger.Info("Starting infrastructure");
101119
transportInfrastructure = await transportCustomization.CreateTransportInfrastructure(
102120
inputEndpoint,
103121
transportSettings,
104122
OnMessage,
105123
errorHandlingPolicy.OnError,
106124
OnCriticalError,
107-
TransportTransactionMode.ReceiveOnly);
125+
TransportTransactionMode.ReceiveOnly
126+
);
108127

109128
queueIngestor = transportInfrastructure.Receivers[inputEndpoint];
110129

111130
await auditIngestor.VerifyCanReachForwardingAddress();
112-
113131
await queueIngestor.StartReceive(cancellationToken);
114132

115-
logger.Info("Ensure started. Infrastructure started");
133+
logger.Info("Started infrastructure");
116134
}
117-
catch
135+
catch (Exception e)
118136
{
119-
if (queueIngestor != null)
137+
logger.Error("Failed to start infrastructure", e);
138+
throw;
139+
}
140+
}
141+
142+
async Task StopAndTeardownInfrastructure(CancellationToken cancellationToken)
143+
{
144+
if (transportInfrastructure == null)
145+
{
146+
logger.Debug("Infrastructure already Stopped");
147+
return;
148+
}
149+
150+
try
151+
{
152+
logger.Info("Stopping infrastructure");
153+
try
120154
{
121-
try
155+
if (queueIngestor != null)
122156
{
123157
await queueIngestor.StopReceive(cancellationToken);
124158
}
125-
catch (OperationCanceledException e) when (e.CancellationToken == cancellationToken)
126-
{
127-
logger.Info("StopReceive cancelled");
128-
}
159+
}
160+
finally
161+
{
162+
await transportInfrastructure.Shutdown(cancellationToken);
129163
}
130164

131-
queueIngestor = null; // Setting to null so that it doesn't exit when it retries in line 185
132-
133-
throw;
165+
queueIngestor = null;
166+
logger.Info("Stopped infrastructure");
134167
}
135-
finally
168+
catch (Exception e)
136169
{
137-
logger.Debug("Ensure started. Start/stop semaphore releasing");
138-
startStopSemaphore.Release();
139-
logger.Debug("Ensure started. Start/stop semaphore released");
170+
logger.Error("Failed to stop infrastructure", e);
171+
throw;
140172
}
141173
}
142174

src/ServiceControl.Infrastructure/Watchdog.cs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class Watchdog
1212
Action<string> reportFailure;
1313
Action clearFailure;
1414
Task watchdog;
15-
CancellationTokenSource shutdownTokenSource = new CancellationTokenSource();
15+
CancellationTokenSource shutdownTokenSource = new();
1616
TimeSpan timeToWaitBetweenStartupAttempts;
1717
ILog log;
1818
string taskName;
@@ -47,41 +47,37 @@ public Task Start(Action onFailedOnStartup)
4747
{
4848
log.Debug($"Starting watching {taskName}");
4949

50-
bool? failedOnStartup = null;
50+
bool startup = true;
5151

5252
while (!shutdownTokenSource.IsCancellationRequested)
5353
{
5454
try
5555
{
56+
using var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(shutdownTokenSource.Token);
57+
cancellationTokenSource.CancelAfter(15000);
58+
5659
log.Debug($"Ensuring {taskName} is running");
57-
await ensureStarted(shutdownTokenSource.Token).ConfigureAwait(false);
60+
await ensureStarted(cancellationTokenSource.Token).ConfigureAwait(false);
5861
clearFailure();
59-
60-
failedOnStartup ??= false;
62+
startup = false;
6163
}
62-
catch (OperationCanceledException e) when (!shutdownTokenSource.IsCancellationRequested)
64+
catch (OperationCanceledException e) when (shutdownTokenSource.IsCancellationRequested)
6365
{
64-
// Continue, as OCE is not from caller
65-
log.Info("Start cancelled, retrying...", e);
66-
continue;
66+
log.Debug("Cancelled", e);
67+
return;
6768
}
6869
catch (Exception e)
6970
{
7071
reportFailure(e.Message);
7172

72-
if (failedOnStartup == null)
73+
if (startup)
7374
{
74-
failedOnStartup = true;
75-
7675
log.Error($"Error during initial startup attempt for {taskName}.", e);
77-
78-
//there was an error during startup hence we want to shut down the instance
7976
onFailedOnStartup();
77+
return;
8078
}
81-
else
82-
{
83-
log.Error($"Error while trying to start {taskName}. Starting will be retried in {timeToWaitBetweenStartupAttempts}.", e);
84-
}
79+
80+
log.Error($"Error while trying to start {taskName}. Starting will be retried in {timeToWaitBetweenStartupAttempts}.", e);
8581
}
8682
try
8783
{

0 commit comments

Comments
 (0)