@@ -69,6 +69,8 @@ const computeLock = memoryCache({
6969
7070const currentSchema = '1.7.0'
7171
72+ const COMPUTE_LOCK_WARN_MS = 60_000
73+
7274const weights = {
7375 declared : 30 ,
7476 discovered : 25 ,
@@ -379,19 +381,60 @@ class DefinitionService {
379381 * @returns {Promise<Definition> } The computed definition
380382 */
381383 async computeStoreAndCurate ( coordinates ) {
382- // one coordinate a time through this method so no duplicate auto curation will be created.
384+ return this . _withComputeLock ( coordinates , async ( ) => {
385+ const definition = await this . _computeAndStore ( coordinates )
386+ await this . curationService . autoCurate ( definition )
387+ return definition
388+ } )
389+ }
390+
391+ /**
392+ * Acquire computeLock, evaluate shouldCompute(), and conditionally compute+store+curate.
393+ * @param {EntityCoordinates } coordinates
394+ * @param {() => Promise<boolean> } shouldCompute - Returns true to proceed, false to skip.
395+ * Should be lightweight and read-only (e.g. getStored + validate); it runs inside the lock.
396+ * @returns {Promise<Definition | undefined> }
397+ */
398+ async computeStoreAndCurateIf ( coordinates , shouldCompute ) {
399+ return this . _withComputeLock ( coordinates , async ( ) => {
400+ if ( ! ( await shouldCompute ( ) ) ) {
401+ return undefined
402+ }
403+ const definition = await this . _computeAndStore ( coordinates )
404+ await this . curationService . autoCurate ( definition )
405+ return definition
406+ } )
407+ }
408+
409+ /**
410+ * Acquire computeLock and run the action while holding the lock.
411+ * @param {EntityCoordinates } coordinates
412+ * @param {() => Promise<T> } action - Work to perform while holding the lock.
413+ * @returns {Promise<T> }
414+ * @template T
415+ * @private
416+ */
417+ async _withComputeLock ( coordinates , action ) {
383418 this . logger . debug ( '3:memory_lock:start' , {
384419 ts : new Date ( ) . toISOString ( ) ,
385420 coordinates : coordinates . toString ( )
386421 } )
422+ const lockWaitStart = Date . now ( )
423+ let lockWarnLogged = false
387424 while ( computeLock . get ( coordinates . toString ( ) ) ) {
388425 await new Promise ( resolve => setTimeout ( resolve , 500 ) )
426+ if ( ! lockWarnLogged && Date . now ( ) - lockWaitStart > COMPUTE_LOCK_WARN_MS ) {
427+ this . logger . warn ( `computeLock spin-wait exceeded ${ COMPUTE_LOCK_WARN_MS / 1000 } s` , {
428+ coordinates : coordinates . toString ( )
429+ } )
430+ lockWarnLogged = true
431+ }
389432 }
390433 try {
434+ // No `await` between the while-exit and set() — this is intentional.
435+ // Inserting an async call here would break the lock.
391436 computeLock . set ( coordinates . toString ( ) , true )
392- const definition = await this . _computeAndStore ( coordinates )
393- await this . curationService . autoCurate ( definition )
394- return definition
437+ return await action ( )
395438 } finally {
396439 computeLock . delete ( coordinates . toString ( ) )
397440 }
@@ -403,15 +446,7 @@ class DefinitionService {
403446 * @returns {Promise<Definition> } The computed definition
404447 */
405448 async computeAndStore ( coordinates ) {
406- while ( computeLock . get ( coordinates . toString ( ) ) ) {
407- await new Promise ( resolve => setTimeout ( resolve , 500 ) ) // one coordinate a time through this method so we always get latest
408- }
409- try {
410- computeLock . set ( coordinates . toString ( ) , true )
411- return await this . _computeAndStore ( coordinates )
412- } finally {
413- computeLock . delete ( coordinates . toString ( ) )
414- }
449+ return this . _withComputeLock ( coordinates , ( ) => this . _computeAndStore ( coordinates ) )
415450 }
416451
417452 /**
@@ -422,11 +457,7 @@ class DefinitionService {
422457 * @returns {Promise<Definition | undefined> } The computed definition or undefined if skipped
423458 */
424459 async computeAndStoreIfNecessary ( coordinates , tool , toolRevision ) {
425- while ( computeLock . get ( coordinates . toString ( ) ) ) {
426- await new Promise ( resolve => setTimeout ( resolve , 500 ) ) // one coordinate a time through this method so we always get latest
427- }
428- try {
429- computeLock . set ( coordinates . toString ( ) , true )
460+ return this . _withComputeLock ( coordinates , async ( ) => {
430461 if ( ! ( await this . _isToolResultNew ( coordinates , tool , toolRevision ) ) ) {
431462 this . logger . info ( 'definition computation skipped: tool result processed' , {
432463 coordinates : coordinates . toString ( ) ,
@@ -436,9 +467,7 @@ class DefinitionService {
436467 return undefined
437468 }
438469 return await this . _computeAndStore ( coordinates )
439- } finally {
440- computeLock . delete ( coordinates . toString ( ) )
441- }
470+ } )
442471 }
443472
444473 /**
0 commit comments