@@ -13,6 +13,7 @@ import os from 'os'
1313import { join } from 'path'
1414import { SpaceMappings } from '../types'
1515import open from 'open'
16+ import { ConfiguredRetryStrategy } from '@smithy/util-retry'
1617export { open }
1718
1819export const mappingFilePath = join ( os . homedir ( ) , '.aws' , '.sagemaker-space-profiles' )
@@ -22,6 +23,13 @@ const tempFilePath = `${mappingFilePath}.tmp`
2223let isWriting = false
2324const writeQueue : Array < ( ) => Promise < void > > = [ ]
2425
26+ // Currently SSM registration happens asynchronously with App launch, which can lead to
27+ // StartSession Internal Failure when connecting to a fresly-started Space.
28+ // To mitigate, spread out retries over multiple seconds instead of sending all retries within a second.
29+ // Backoff sequence: 1500ms, 2250ms, 3375ms
30+ // Retry timing: 1500ms, 3750ms, 7125ms
31+ const startSessionRetryStrategy = new ConfiguredRetryStrategy ( 3 , ( attempt : number ) => 1000 * 1.5 ** attempt )
32+
2533/**
2634 * Reads the local endpoint info file (default or via env) and returns pid & port.
2735 * @throws Error if the file is missing, invalid JSON, or missing fields
@@ -83,7 +91,7 @@ export function parseArn(arn: string): { region: string; accountId: string; spac
8391
8492export async function startSagemakerSession ( { region, connectionIdentifier, credentials } : any ) {
8593 const endpoint = process . env . SAGEMAKER_ENDPOINT || `https://sagemaker.${ region } .amazonaws.com`
86- const client = new SageMakerClient ( { region, credentials, endpoint } )
94+ const client = new SageMakerClient ( { region, credentials, endpoint, retryStrategy : startSessionRetryStrategy } )
8795 const command = new StartSessionCommand ( { ResourceIdentifier : connectionIdentifier } )
8896 return client . send ( command )
8997}
0 commit comments