@@ -118,12 +118,15 @@ export function makeUtxoEngineProcessor(
118118 // Track pending timeouts so they can be cleared on stop
119119 const pendingTimeouts : Set < NodeJS . Timeout > = new Set ( )
120120
121- const clearTaskCache = ( ) : void => {
122- // Clear all pending timeouts
121+ // Clear all pending timeouts
122+ const clearPendingTimeouts = ( ) : void => {
123123 for ( const timeoutId of pendingTimeouts ) {
124124 clearTimeout ( timeoutId )
125125 }
126126 pendingTimeouts . clear ( )
127+ }
128+
129+ const clearTaskCache = ( ) : void => {
127130 for ( const key of Object . keys ( taskCache . addressForTransactionsCache ) ) {
128131 removeItem ( taskCache . addressForTransactionsCache , key )
129132 }
@@ -367,6 +370,7 @@ export function makeUtxoEngineProcessor(
367370 async stop ( ) : Promise < void > {
368371 serverStates . stop ( )
369372 clearTaskCache ( )
373+ clearPendingTimeouts ( )
370374 running = false
371375 } ,
372376
@@ -1167,13 +1171,15 @@ async function* processCheckTransactionConfirmation(
11671171 // Delay putting the transaction back into the cache to avoid thrashing
11681172 // on dropped transactions. 10 seconds is a reasonable compromise between
11691173 // thrashing and not waiting too long.
1170- const timeoutId = setTimeout ( ( ) => {
1171- common . pendingTimeouts . delete ( timeoutId )
1172- common . taskCache . transactionUpdateCache [ txId ] = {
1173- processing : false
1174- }
1175- } , 10000 )
1176- common . pendingTimeouts . add ( timeoutId )
1174+ addPendingTimeout (
1175+ common ,
1176+ ( ) => {
1177+ common . taskCache . transactionUpdateCache [ txId ] = {
1178+ processing : false
1179+ }
1180+ } ,
1181+ 10000
1182+ )
11771183 return false
11781184 }
11791185}
@@ -1698,3 +1704,22 @@ const addToDataLayerUtxoCache = (
16981704 dataLayerUtxoCache [ scriptPubkey ] = dataLayerUtxos
16991705 dataLayerUtxos . full = dataLayerUtxos . utxos . length >= requiredCount
17001706}
1707+
1708+ /**
1709+ * This adds a pending timeout to the common.pendingTimeouts set.
1710+ * It will call the callback after the timeout.
1711+ *
1712+ * This utility is a safe way to add pending timeouts for the processor because
1713+ * it will clear the timeout when the processor stops.
1714+ */
1715+ const addPendingTimeout = (
1716+ common : CommonParams ,
1717+ callback : ( ) => void ,
1718+ timeout : number
1719+ ) : void => {
1720+ const timeoutId = setTimeout ( ( ) => {
1721+ common . pendingTimeouts . delete ( timeoutId )
1722+ callback ( )
1723+ } , timeout )
1724+ common . pendingTimeouts . add ( timeoutId )
1725+ }
0 commit comments