gemini-code-assist[bot] commented on code in PR #19790:
URL: https://github.com/apache/tvm/pull/19790#discussion_r3420914613


##########
web/src/webgpu.ts:
##########
@@ -169,9 +169,18 @@ function tryCreateBuffer(device: GPUDevice, descriptor: 
GPUBufferDescriptor) {
 
   const buffer = device.createBuffer(descriptor);
 
-  device.popErrorScope().then((error) => {if (error) {device.destroy(); 
console.error(error);}});
-  device.popErrorScope().then((error) => {if (error) {device.destroy(); 
console.error(error);}});
-  device.popErrorScope().then((error) => {if (error) {device.destroy(); 
console.error(error);}});
+  // Destroy at most once even if multiple error types fire.
+  Promise.all([
+    device.popErrorScope(),
+    device.popErrorScope(),
+    device.popErrorScope(),
+  ]).then((errors) => {
+    const captured = errors.filter((error) => error !== null);
+    if (captured.length > 0) {
+      device.destroy();
+      captured.forEach((error) => console.error(error));
+    }
+  });

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   1. **Unhandled Promise Rejection**: If `popErrorScope()` rejects (e.g., if 
the device is already lost), the `Promise.all` promise will reject. Without a 
`.catch()` block, this results in an unhandled promise rejection.
   2. **TypeScript Type Guard**: Using `errors.filter(error => error !== null)` 
does not narrow the type of `captured` to `GPUError[]` in TypeScript. Adding an 
explicit type guard `(error): error is GPUError => error !== null` ensures 
proper type safety.
   
   ```suggestion
     Promise.all([
       device.popErrorScope(),
       device.popErrorScope(),
       device.popErrorScope(),
     ]).then((errors) => {
       const captured = errors.filter((error): error is GPUError => error !== 
null);
       if (captured.length > 0) {
         device.destroy();
         captured.forEach((error) => console.error(error));
       }
     }).catch((err) => {
       console.error("Failed to pop error scopes:", err);
     });
   ```



-- 
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]

Reply via email to