Skip to content

Commit d8a6df5

Browse files
committed
attempt to fix infinite loop in moment-reference-builder
1 parent 1859994 commit d8a6df5

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

Framework/OverwatchTranscript/EventBucketReader.cs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace OverwatchTranscript
77
public interface IFinalizedBucket
88
{
99
bool IsEmpty { get; }
10+
void Update();
1011
DateTime? SeeTopUtc();
1112
BucketTop? TakeTop();
1213
}
@@ -28,7 +29,8 @@ public class EventBucketReader : IFinalizedBucket
2829
private readonly string bucketFile;
2930
private readonly ConcurrentQueue<BucketTop> topQueue = new ConcurrentQueue<BucketTop>();
3031
private readonly AutoResetEvent itemDequeued = new AutoResetEvent(false);
31-
private bool stopping;
32+
private readonly AutoResetEvent itemEnqueued = new AutoResetEvent(false);
33+
private bool sourceIsEmpty;
3234

3335
public EventBucketReader(ILog log, string bucketFile)
3436
{
@@ -42,34 +44,38 @@ public EventBucketReader(ILog log, string bucketFile)
4244

4345
public bool IsEmpty { get; private set; }
4446

47+
public void Update()
48+
{
49+
if (IsEmpty) return;
50+
while (topQueue.Count == 0)
51+
{
52+
UpdateIsEmpty();
53+
if (IsEmpty) return;
54+
55+
itemDequeued.Set();
56+
itemEnqueued.WaitOne(200);
57+
}
58+
}
59+
4560
public DateTime? SeeTopUtc()
4661
{
4762
if (IsEmpty) return null;
48-
while (true)
63+
if (topQueue.TryPeek(out BucketTop? top))
4964
{
50-
UpdateIsEmpty();
51-
if (IsEmpty) return null;
52-
if (topQueue.TryPeek(out BucketTop? top))
53-
{
54-
return top.Utc;
55-
}
65+
return top.Utc;
5666
}
67+
return null;
5768
}
5869

5970
public BucketTop? TakeTop()
6071
{
6172
if (IsEmpty) return null;
62-
63-
while (true)
73+
if (topQueue.TryDequeue(out BucketTop? top))
6474
{
65-
UpdateIsEmpty();
66-
if (IsEmpty) return null;
67-
if (topQueue.TryDequeue(out BucketTop? top))
68-
{
69-
itemDequeued.Set();
70-
return top;
71-
}
75+
itemDequeued.Set();
76+
return top;
7277
}
78+
return null;
7379
}
7480

7581
private void ReadBucket()
@@ -85,23 +91,25 @@ private void ReadBucket()
8591
if (top != null)
8692
{
8793
topQueue.Enqueue(top);
94+
itemEnqueued.Set();
8895
}
8996
else
9097
{
91-
stopping = true;
98+
sourceIsEmpty = true;
99+
UpdateIsEmpty();
92100
return;
93101
}
94102
}
95103

96104
itemDequeued.Reset();
97-
itemDequeued.WaitOne();
105+
itemDequeued.WaitOne(5000);
98106
}
99107
}
100108

101109
private void UpdateIsEmpty()
102110
{
103-
var empty = stopping && topQueue.IsEmpty;
104-
if (!IsEmpty && empty)
111+
var allEmpty = sourceIsEmpty && topQueue.IsEmpty;
112+
if (!IsEmpty && allEmpty)
105113
{
106114
File.Delete(bucketFile);
107115
IsEmpty = true;

Framework/OverwatchTranscript/MomentReferenceBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ private List<BucketTop> CollectAllTopsForUtc(DateTime earliestUtc, List<IFinaliz
6868
{
6969
if (bucket.IsEmpty) continue;
7070

71+
bucket.Update();
7172
var utc = bucket.SeeTopUtc();
7273
if (utc == null) continue;
7374

0 commit comments

Comments
 (0)