@@ -7,6 +7,7 @@ const MAX_RETRIES = 10;
77
88let pullInProgress = false ;
99let pullComplete = false ;
10+ let abortController : AbortController | null = null ;
1011
1112async function isDockerAvailable ( ) : Promise < boolean > {
1213 try {
@@ -44,8 +45,15 @@ export async function startEagerImagePull(): Promise<void> {
4445 }
4546
4647 pullInProgress = true ;
48+ abortController = new AbortController ( ) ;
49+ const signal = abortController . signal ;
4750
4851 const attemptPull = async ( attempt : number ) : Promise < void > => {
52+ if ( signal . aborted ) {
53+ pullInProgress = false ;
54+ return ;
55+ }
56+
4957 if ( attempt > MAX_RETRIES ) {
5058 console . log ( '[agent] Max retries reached for image pull - giving up background pull' ) ;
5159 pullInProgress = false ;
@@ -58,7 +66,8 @@ export async function startEagerImagePull(): Promise<void> {
5866 if ( attempt === 1 ) {
5967 console . log ( '[agent] Docker not available - will retry in background' ) ;
6068 }
61- setTimeout ( ( ) => attemptPull ( attempt + 1 ) , RETRY_INTERVAL_MS ) ;
69+ const timer = setTimeout ( ( ) => attemptPull ( attempt + 1 ) , RETRY_INTERVAL_MS ) ;
70+ timer . unref ( ) ;
6271 return ;
6372 }
6473
@@ -67,14 +76,23 @@ export async function startEagerImagePull(): Promise<void> {
6776 if ( success ) {
6877 pullComplete = true ;
6978 pullInProgress = false ;
70- } else {
71- setTimeout ( ( ) => attemptPull ( attempt + 1 ) , RETRY_INTERVAL_MS ) ;
79+ } else if ( ! signal . aborted ) {
80+ const timer = setTimeout ( ( ) => attemptPull ( attempt + 1 ) , RETRY_INTERVAL_MS ) ;
81+ timer . unref ( ) ;
7282 }
7383 } ;
7484
7585 attemptPull ( 1 ) ;
7686}
7787
88+ export function stopEagerImagePull ( ) : void {
89+ if ( abortController ) {
90+ abortController . abort ( ) ;
91+ abortController = null ;
92+ }
93+ pullInProgress = false ;
94+ }
95+
7896export function isImagePullComplete ( ) : boolean {
7997 return pullComplete ;
8098}
0 commit comments