codeant-ai-for-open-source[bot] commented on code in PR #41286:
URL: https://github.com/apache/superset/pull/41286#discussion_r3453864468


##########
superset/views/base.py:
##########
@@ -171,20 +171,25 @@ def deprecated(
     """
 
     def _deprecated(f: Callable[..., FlaskResponse]) -> Callable[..., 
FlaskResponse]:
+        _warned = False
+
         def wraps(self: BaseSupersetView, *args: Any, **kwargs: Any) -> 
FlaskResponse:
-            message = (
-                "%s.%s "
-                "This API endpoint is deprecated and will be removed in 
version %s"
-            )
-            logger_args = [
-                self.__class__.__name__,
-                f.__name__,
-                eol_version,
-            ]
-            if new_target:
-                message += " . Use the following API endpoint instead: %s"
-                logger_args.append(new_target)
-            logger.warning(message, *logger_args)
+            nonlocal _warned
+            if not _warned:
+                _warned = True
+                message = (
+                    "%s.%s "
+                    "This API endpoint is deprecated and will be removed in 
version %s"
+                )
+                logger_args = [
+                    self.__class__.__name__,
+                    f.__name__,
+                    eol_version,
+                ]
+                if new_target:
+                    message += " . Use the following API endpoint instead: %s"
+                    logger_args.append(new_target)
+                logger.warning(message, *logger_args)

Review Comment:
   **Suggestion:** The new one-time warning guard is not thread-safe: 
concurrent first requests can both pass the `if not _warned` check before 
either write is visible to the other, so the warning can still be emitted 
multiple times in a worker. This violates the intended "once per endpoint per 
process" behavior; guard the check/update with a lock (or another atomic 
one-time primitive) around the warning block. [race condition]
   
   <details>
   <summary><b>Severity Level:</b> Minor ๐Ÿงน</summary>
   
   ```mdx
   - โš ๏ธ Deprecated explore_json logs may duplicate under concurrent load.
   - โš ๏ธ Deprecation logging not strictly once-per-process as intended.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction โœ… </b></summary>
   
   ```mdx
   1. Observe the deprecated decorator implementation in 
`superset/views/base.py` at lines
   15โ€“48 (Read output), specifically the inner `wraps` function where the guard 
is
   implemented (diff hunk lines 176โ€“193): it uses `nonlocal _warned`, checks 
`if not
   _warned:`, then sets `_warned = True` and emits `logger.warning(...)` 
without any
   synchronization.
   
   2. Confirm that a real HTTP endpoint uses this decorator: in 
`superset/views/core.py`
   lines 280โ€“319, the `explore_json` view is exposed via 
`@expose("/explore_json/",
   methods=("GET","POST"))` and
   `@expose("/explore_json/<datasource_type>/<int:datasource_id>/", ...)`, and 
is decorated
   with `@deprecated(eol_version="5.0.0", new_target="/api/v1/chart/data")` at 
line 18 (Read
   output), so every call to `/explore_json` passes through the `_warned` guard 
in
   `superset/views/base.py`.
   
   3. Run Superset with this PR code under a multi-threaded WSGI server (e.g., 
gunicorn
   worker configured with `--threads > 1`), so that multiple requests to 
`/explore_json` can
   be processed concurrently by the same process and thus share the same 
`_warned` closure
   defined in `deprecated` at `superset/views/base.py:24โ€“46`.
   
   4. Immediately after starting a worker, send two or more concurrent HTTP 
requests to
   `/explore_json` (or `/explore_json/<datasource_type>/<datasource_id>/`) so 
they reach the
   same process at nearly the same time; due to the unsynchronized `if not 
_warned: _warned =
   True` sequence in `superset/views/base.py` lines 28โ€“31, two threads can both 
see `_warned`
   as `False` before either write is visible, causing the warning block to 
execute in both
   threads and resulting in multiple `"This API endpoint is deprecated and will 
be removed in
   version %s"` log entries instead of a single once-per-process warning.
   ```
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=b1515a5cfa684b3c8dbf58b036e07882&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=b1515a5cfa684b3c8dbf58b036e07882&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
   
   *(Use Cmd/Ctrl + Click for best experience)*
   <details>
   <summary><b>Prompt for AI Agent ๐Ÿค– </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** superset/views/base.py
   **Line:** 177:192
   **Comment:**
        *Race Condition: The new one-time warning guard is not thread-safe: 
concurrent first requests can both pass the `if not _warned` check before 
either write is visible to the other, so the warning can still be emitted 
multiple times in a worker. This violates the intended "once per endpoint per 
process" behavior; guard the check/update with a lock (or another atomic 
one-time primitive) around the warning block.
   
   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%2F41286&comment_hash=7a6db70e0580abe09121ad8b6bdd86b68917518e37f69ce02aebfec19eb3379a&reaction=like'>๐Ÿ‘</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41286&comment_hash=7a6db70e0580abe09121ad8b6bdd86b68917518e37f69ce02aebfec19eb3379a&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]

Reply via email to