Skip to content

Commit 647dd08

Browse files
committed
Fix issue where we dispose the PollAndSubscribeToResponse before we delete the response from redis
1 parent b7a3354 commit 647dd08

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

source/Halibut/Queue/Redis/PollAndSubscribeToResponse.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,39 +153,46 @@ public async Task<bool> TryGetResponseFromRedis(string detectedBy, CancellationT
153153

154154
if (ResponseJsonCompletionSource.Task.IsCompleted) return true;
155155

156+
// TODO wrap in try
156157
var responseJson = await halibutRedisTransport.GetResponseMessage(endpoint, activityId, token);
158+
157159
if (responseJson != null)
158160
{
159-
log.Write(EventType.Diagnostic, "Response detected via {0} - Endpoint: {1}, ActivityId: {2}", detectedBy, endpoint, activityId);
160-
await TrySetResponse(responseJson, token);
161+
log.Write(EventType.Diagnostic, "Response detected via {0} - Endpoint: {1}, ActivityId: {2}", detectedBy, endpoint, activityId);
162+
163+
await DeleteResponseFromRedis(detectedBy, token);
164+
165+
TrySetResponse(responseJson, token);
161166
await Try.IgnoringError(async () => await watcherToken.CancelAsync());
162167
log.Write(EventType.Diagnostic, "Cancelling polling loop for response - Endpoint: {0}, ActivityId: {1}", endpoint, activityId);
163168
return true;
164169
}
165170

166171
return false;
167172
}
168-
169-
async Task TrySetResponse(string value, CancellationToken cancellationToken)
173+
174+
async Task DeleteResponseFromRedis(string detectedBy, CancellationToken token)
170175
{
171176
try
172177
{
173-
ResponseJsonCompletionSource.TrySetResult(value);
178+
await halibutRedisTransport.DeleteResponse(endpoint, activityId, token);
174179
}
175180
catch (Exception ex)
176181
{
177-
log.Write(EventType.Error, "Failed to set response - Endpoint: {0}, ActivityId: {1}, Error: {2}", endpoint, activityId, ex.Message);
182+
log.Write(EventType.Error, "Failed to delete response from Redis via {0} - Endpoint: {1}, ActivityId: {2}, Error: {3}", detectedBy, endpoint, activityId, ex.Message);
178183
}
184+
}
179185

186+
void TrySetResponse(string value, CancellationToken cancellationToken)
187+
{
180188
try
181189
{
182-
await halibutRedisTransport.DeleteResponse(endpoint, activityId, cancellationToken);
190+
ResponseJsonCompletionSource.TrySetResult(value);
183191
}
184192
catch (Exception ex)
185193
{
186-
log.Write(EventType.Error, "Failed to delete response from Redis - Endpoint: {0}, ActivityId: {1}, Error: {2}", endpoint, activityId, ex.Message);
194+
log.Write(EventType.Error, "Failed to set response - Endpoint: {0}, ActivityId: {1}, Error: {2}", endpoint, activityId, ex.Message);
187195
}
188-
189196
}
190197

191198
public async ValueTask DisposeAsync()

source/Halibut/Util/CancelOnDisposeCancellationToken.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public sealed class CancelOnDisposeCancellationToken : IAsyncDisposable
1616
readonly CancellationTokenSource cancellationTokenSource;
1717
bool disposed;
1818

19-
AwaitAllAndIgnoreException awaitAllAndIgnoreException = new AwaitAllAndIgnoreException();
19+
readonly AwaitAllAndIgnoreException awaitAllAndIgnoreException = new();
2020

2121
public CancelOnDisposeCancellationToken(params CancellationToken[] token)
2222
: this(CancellationTokenSource.CreateLinkedTokenSource(token))

0 commit comments

Comments
 (0)