@@ -233,21 +233,14 @@ func (ns *Namespace) getOrCreate(ctx context.Context, key partialCacheKey, conte
233
233
}
234
234
}()
235
235
236
- // We may already have a write lock.
237
- hasLock := tpl .GetHasLockFromContext (ctx )
238
-
239
- if ! hasLock {
240
- ns .cachedPartials .RLock ()
241
- }
236
+ ns .cachedPartials .RLock ()
242
237
p , ok := ns .cachedPartials .p [key ]
243
- if ! hasLock {
244
- ns .cachedPartials .RUnlock ()
245
- }
238
+ ns .cachedPartials .RUnlock ()
246
239
247
240
if ok {
248
241
if ns .deps .Metrics != nil {
249
242
ns .deps .Metrics .TrackValue (key .templateName (), p , true )
250
- // The templates that gets executed is measued in Execute.
243
+ // The templates that gets executed is measured in Execute.
251
244
// We need to track the time spent in the cache to
252
245
// get the totals correct.
253
246
ns .deps .Metrics .MeasureSince (key .templateName (), start )
@@ -256,21 +249,28 @@ func (ns *Namespace) getOrCreate(ctx context.Context, key partialCacheKey, conte
256
249
return p , nil
257
250
}
258
251
259
- if ! hasLock {
260
- ns .cachedPartials .Lock ()
261
- defer ns .cachedPartials .Unlock ()
262
- ctx = tpl .SetHasLockInContext (ctx , true )
263
- }
264
-
265
- var name string
266
- name , p , err = ns .include (ctx , key .name , context )
252
+ // This needs to be done outside the lock.
253
+ // See #9588
254
+ _ , p , err = ns .include (ctx , key .name , context )
267
255
if err != nil {
268
256
return nil , err
269
257
}
270
258
259
+ ns .cachedPartials .Lock ()
260
+ defer ns .cachedPartials .Unlock ()
261
+ // Double-check.
262
+ if p2 , ok := ns .cachedPartials .p [key ]; ok {
263
+ if ns .deps .Metrics != nil {
264
+ ns .deps .Metrics .TrackValue (key .templateName (), p , true )
265
+ ns .deps .Metrics .MeasureSince (key .templateName (), start )
266
+ }
267
+ return p2 , nil
268
+
269
+ }
271
270
if ns .deps .Metrics != nil {
272
- ns .deps .Metrics .TrackValue (name , p , false )
271
+ ns .deps .Metrics .TrackValue (key . templateName () , p , false )
273
272
}
273
+
274
274
ns .cachedPartials .p [key ] = p
275
275
276
276
return p , nil
0 commit comments