codeant-ai-for-open-source[bot] commented on code in PR #43215: URL: https://github.com/apache/superset/pull/43215#discussion_r3788995351
########## superset-frontend/src/utils/lazyWithRetry.ts: ########## @@ -0,0 +1,96 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { LazyExoticComponent, lazy } from 'react'; + +/** + * `React.lazy` memoizes the *rejection* of its factory: once the dynamic + * `import()` behind a route fails (a chunk that 404s after a redeploy, or a + * transient 502/503 from the asset server), the payload is permanently marked + * as rejected and the component throws `ChunkLoadError` forever - even after + * the asset becomes reachable again and even when the user navigates away and + * back. Retrying the import *inside* the factory, before the promise handed to + * `React.lazy` settles, is the only way to recover from a transient failure. + * + * See https://github.com/apache/superset/issues/41266 + */ +export const DEFAULT_LAZY_RETRIES = 2; +export const DEFAULT_LAZY_RETRY_DELAY_MS = 500; + +export interface LazyRetryOptions { + /** Number of *additional* attempts made after the first one fails. */ + retries?: number; + /** Base delay between attempts; doubled on every subsequent attempt. */ + retryDelayMs?: number; +} + +const sleep = (ms: number): Promise<void> => + new Promise(resolve => { + setTimeout(resolve, ms); + }); + +/** + * Calls `factory`, retrying with exponential backoff when it rejects. Rejects + * with the last error once every attempt has been exhausted. + */ +export async function retryImport<T>( + factory: () => Promise<T>, + { + retries = DEFAULT_LAZY_RETRIES, + retryDelayMs = DEFAULT_LAZY_RETRY_DELAY_MS, + }: LazyRetryOptions = {}, +): Promise<T> { + let lastError: unknown; + for (let attempt = 0; attempt <= retries; attempt += 1) { + try { + // eslint-disable-next-line no-await-in-loop + return await factory(); + } catch (error) { + lastError = error; + if (attempt < retries) { + // eslint-disable-next-line no-await-in-loop + await sleep(retryDelayMs * 2 ** attempt); + } Review Comment: **Suggestion:** The retry loop treats every factory failure as a transient chunk failure. Deterministic module-evaluation errors, syntax errors, and missing dependencies will therefore be retried twice, adding up to 1.5 seconds of delay before the error boundary can report an error, and multiplying failed requests across all lazy-loaded routes. Restrict retries to retryable chunk-load failures or make the retry predicate configurable. [performance] <details> <summary><b>Severity Level:</b> Minor ๐งน</summary> ```mdx - โ ๏ธ Route module errors are delayed by 1.5 seconds. - โ ๏ธ Failed imports generate redundant asset requests. - โ ๏ธ Dashboard and Explore feature errors retry unnecessarily. ``` </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=6277c8bc5af349a9b13780ed4234411d&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=6277c8bc5af349a9b13780ed4234411d&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) <details> <summary><b>Prompt for AI Agent ๐ค </b></summary> ```mdx This is a comment left during a code review. **Path:** superset-frontend/src/utils/lazyWithRetry.ts **Line:** 63:68 **Comment:** *Performance: The retry loop treats every factory failure as a transient chunk failure. Deterministic module-evaluation errors, syntax errors, and missing dependencies will therefore be retried twice, adding up to 1.5 seconds of delay before the error boundary can report an error, and multiplying failed requests across all lazy-loaded routes. Restrict retries to retryable chunk-load failures or make the retry predicate configurable. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F43215&comment_hash=94bcb43cf0cc004bbdc19e76182b139a598f889f9cacb9c82fd4568cd79b13ea&reaction=like'>๐</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F43215&comment_hash=94bcb43cf0cc004bbdc19e76182b139a598f889f9cacb9c82fd4568cd79b13ea&reaction=dislike'>๐</a> -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
