Lambda handler fails mid-batch when Genesys Cloud access token expires during long-running sync

No idea why this is happening, my Node.js Lambda handler crashes with an Unauthorized error when processing a batch of 5,000 queue members via the Genesys Cloud REST API. The handler fetches a new OAuth token at startup, which has a 3600-second TTL. However, the batch processing loop takes roughly 45 minutes due to rate limiting backoffs. When the loop hits item #4,200, the token has expired. I am not using the official Genesys SDK because I need fine-grained control over the retry logic and SQS batching. Instead, I am making raw fetch calls to /api/v2/users/me to verify token validity, but checking every single request adds unacceptable latency. I tried checking the expires_in field from the initial token response, but there is a clock skew issue between my Lambda execution environment and the Genesys auth server that causes premature expiry checks.

Here is the core loop where the failure occurs. The axios instance is configured with a simple interceptor, but it does not automatically handle the token rotation logic I need for this specific serverless context. The error payload returned by Genesys is standard, but it happens unpredictably depending on the network latency of the previous request. I have verified that the refresh token is valid by manually testing it in Postman, so the credential itself is not the issue. The problem is strictly the timing of the refresh within the synchronous execution of the Lambda function. I am using a global variable to store the token, which persists across concurrent invocations in the same container, but this leads to race conditions when two batches start simultaneously and try to refresh the token at the same time.

// Simplified loop causing the issue
for (const member of batch) {
 const response = await axios.get(
 `https://myinstance.mygen.com/api/v2/queueMembers/${member.id}`,
 { headers: { Authorization: `Bearer ${globalToken}` } }
 );
 // Process data...
}

I need a robust pattern for refreshing the token in the middle of this loop without blocking the entire batch or causing race conditions. Should I be using a mutex-like lock around the refresh logic? Or is there a better way to integrate the token refresh with the axios request interceptor so that it transparently retries the failed request with a new token? I want to avoid polling the expiry time. I need a reactive solution that triggers on the 401 error. What is the standard best practice for this in a serverless environment using Node.js 18.x?