@@ -46,56 +46,59 @@ public Task<CollectionResponse> EnterCollect(bool openMetricsRequested)
4646 {
4747 this . EnterGlobalLock ( ) ;
4848
49- // If we are within {ScrapeResponseCacheDurationMilliseconds} of the
50- // last successful collect, return the previous view.
51- var previousDataViewGeneratedAtUtc = openMetricsRequested
52- ? this . previousOpenMetricsDataViewGeneratedAtUtc
53- : this . previousPlainTextDataViewGeneratedAtUtc ;
54-
55- if ( previousDataViewGeneratedAtUtc . HasValue
56- && this . scrapeResponseCacheDurationMilliseconds > 0
57- && previousDataViewGeneratedAtUtc . Value . AddMilliseconds ( this . scrapeResponseCacheDurationMilliseconds ) >= DateTime . UtcNow )
49+ DateTime ? previousDataViewGeneratedAtUtc ;
50+
51+ try
5852 {
59- Interlocked . Increment ( ref this . readerCount ) ;
60- this . ExitGlobalLock ( ) ;
53+ // If we are within {ScrapeResponseCacheDurationMilliseconds} of the
54+ // last successful collect, return the previous view.
55+ previousDataViewGeneratedAtUtc = openMetricsRequested
56+ ? this . previousOpenMetricsDataViewGeneratedAtUtc
57+ : this . previousPlainTextDataViewGeneratedAtUtc ;
58+
59+ if ( previousDataViewGeneratedAtUtc . HasValue
60+ && this . scrapeResponseCacheDurationMilliseconds > 0
61+ && previousDataViewGeneratedAtUtc . Value . AddMilliseconds ( this . scrapeResponseCacheDurationMilliseconds ) >= DateTime . UtcNow )
62+ {
6163#if NET
62- return new ValueTask < CollectionResponse > ( new CollectionResponse ( this . previousOpenMetricsDataView , this . previousPlainTextDataView , previousDataViewGeneratedAtUtc . Value , fromCache : true ) ) ;
64+ return new ValueTask < CollectionResponse > ( new CollectionResponse ( this . previousOpenMetricsDataView , this . previousPlainTextDataView , previousDataViewGeneratedAtUtc . Value , fromCache : true ) ) ;
6365#else
64- return Task . FromResult ( new CollectionResponse ( this . previousOpenMetricsDataView , this . previousPlainTextDataView , previousDataViewGeneratedAtUtc . Value , fromCache : true ) ) ;
66+ return Task . FromResult ( new CollectionResponse ( this . previousOpenMetricsDataView , this . previousPlainTextDataView , previousDataViewGeneratedAtUtc . Value , fromCache : true ) ) ;
6567#endif
66- }
68+ }
6769
68- // If a collection is already running, return a task to wait on the result.
69- if ( this . collectionRunning )
70- {
71- this . collectionTcs ??= new TaskCompletionSource < CollectionResponse > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
70+ // If a collection is already running, return a task to wait on the result.
71+ if ( this . collectionRunning )
72+ {
73+ this . collectionTcs ??= new TaskCompletionSource < CollectionResponse > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
7274
73- Interlocked . Increment ( ref this . readerCount ) ;
74- this . ExitGlobalLock ( ) ;
7575#if NET
76- return new ValueTask < CollectionResponse > ( this . collectionTcs . Task ) ;
76+ return new ValueTask < CollectionResponse > ( this . collectionTcs . Task ) ;
7777#else
78- return this . collectionTcs . Task ;
78+ return this . collectionTcs . Task ;
7979#endif
80- }
80+ }
8181
82- this . WaitForReadersToComplete ( ) ;
82+ this . WaitForReadersToComplete ( ) ;
8383
84- // Start a collection on the current thread.
85- this . collectionRunning = true ;
84+ // Start a collection on the current thread.
85+ this . collectionRunning = true ;
8686
87- if ( openMetricsRequested )
88- {
89- this . previousOpenMetricsDataViewGeneratedAtUtc = null ;
87+ if ( openMetricsRequested )
88+ {
89+ this . previousOpenMetricsDataViewGeneratedAtUtc = null ;
90+ }
91+ else
92+ {
93+ this . previousPlainTextDataViewGeneratedAtUtc = null ;
94+ }
9095 }
91- else
96+ finally
9297 {
93- this . previousPlainTextDataViewGeneratedAtUtc = null ;
98+ Interlocked . Increment ( ref this . readerCount ) ;
99+ this . ExitGlobalLock ( ) ;
94100 }
95101
96- Interlocked . Increment ( ref this . readerCount ) ;
97- this . ExitGlobalLock ( ) ;
98-
99102 CollectionResponse response ;
100103 var result = this . ExecuteCollect ( openMetricsRequested ) ;
101104 if ( result )
@@ -122,16 +125,21 @@ public Task<CollectionResponse> EnterCollect(bool openMetricsRequested)
122125
123126 this . EnterGlobalLock ( ) ;
124127
125- this . collectionRunning = false ;
128+ try
129+ {
130+ this . collectionRunning = false ;
126131
127- if ( this . collectionTcs != null )
132+ if ( this . collectionTcs != null )
133+ {
134+ this . collectionTcs . SetResult ( response ) ;
135+ this . collectionTcs = null ;
136+ }
137+ }
138+ finally
128139 {
129- this . collectionTcs . SetResult ( response ) ;
130- this . collectionTcs = null ;
140+ this . ExitGlobalLock ( ) ;
131141 }
132142
133- this . ExitGlobalLock ( ) ;
134-
135143#if NET
136144 return new ValueTask < CollectionResponse > ( response ) ;
137145#else
0 commit comments