@@ -213,6 +213,8 @@ export class InlineCompletionManager implements Disposable {
213213
214214export class AmazonQInlineCompletionItemProvider implements InlineCompletionItemProvider {
215215 private logger = getLogger ( )
216+ private pendingRequest : Promise < InlineCompletionItem [ ] > | undefined
217+
216218 constructor (
217219 private readonly languageClient : LanguageClient ,
218220 private readonly recommendationService : RecommendationService ,
@@ -300,6 +302,48 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
300302 options : JSON . stringify ( getAllRecommendationsOptions ) ,
301303 } )
302304
305+ // If there's already a pending request, wait for it to complete instead of starting a new one
306+ // This prevents race conditions where multiple concurrent calls cause the later (empty) response
307+ // to override the earlier (valid) response
308+ if ( this . pendingRequest ) {
309+ getLogger ( ) . info ( 'Reusing pending inline completion request to avoid race condition' )
310+ try {
311+ const result = await this . pendingRequest
312+ // Check if THIS call's token was cancelled (not the original call's token)
313+ if ( token . isCancellationRequested ) {
314+ getLogger ( ) . info ( 'Reused request completed but this call was cancelled' )
315+ return [ ]
316+ }
317+ return result
318+ } catch ( e ) {
319+ // If the pending request failed, continue with a new request
320+ getLogger ( ) . info ( 'Pending request failed, starting new request: %O' , e )
321+ }
322+ }
323+
324+ // Start a new request and track it
325+ this . pendingRequest = this . _provideInlineCompletionItemsImpl (
326+ document ,
327+ position ,
328+ context ,
329+ token ,
330+ getAllRecommendationsOptions
331+ )
332+
333+ try {
334+ return await this . pendingRequest
335+ } finally {
336+ this . pendingRequest = undefined
337+ }
338+ }
339+
340+ private async _provideInlineCompletionItemsImpl (
341+ document : TextDocument ,
342+ position : Position ,
343+ context : InlineCompletionContext ,
344+ token : CancellationToken ,
345+ getAllRecommendationsOptions ?: GetAllRecommendationsOptions
346+ ) : Promise < InlineCompletionItem [ ] > {
303347 if ( vsCodeState . isCodeWhispererEditing ) {
304348 getLogger ( ) . info ( 'Q is editing, returning empty' )
305349 return [ ]
0 commit comments