You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by "dpgaspar (via GitHub)" <gi...@apache.org> on 2026/01/07 11:55:29 UTC

[PR] fix: streaming export losing g context [superset]

dpgaspar opened a new pull request, #36950:
URL: https://github.com/apache/superset/pull/36950

   <!---
   Please write the PR title following the conventions at https://www.conventionalcommits.org/en/v1.0.0/
   Example:
   fix(dashboard): load charts correctly
   -->
   
   ### SUMMARY
   <!--- Describe the change below, including rationale and design decisions -->
   
   ### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
   <!--- Skip this if not applicable -->
   
   ### TESTING INSTRUCTIONS
   <!--- Required! What steps can be taken to manually verify the changes? -->
   
   ### ADDITIONAL INFORMATION
   <!--- Check any relevant boxes with "x" -->
   <!--- HINT: Include "Fixes #nnn" if you are fixing an existing issue -->
   - [ ] Has associated issue:
   - [ ] Required feature flags:
   - [ ] Changes UI
   - [ ] Includes DB Migration (follow approval process in [SIP-59](https://github.com/apache/superset/issues/13351))
     - [ ] Migration is atomic, supports rollback & is backwards-compatible
     - [ ] Confirm DB migration upgrade and downgrade tested
     - [ ] Runtime estimates and downtime expectations provided
   - [ ] Introduces new feature or API
   - [ ] Removes existing feature or API
   


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix: streaming export losing g context [superset]

Posted by "netlify[bot] (via GitHub)" <gi...@apache.org>.
netlify[bot] commented on PR #36950:
URL: https://github.com/apache/superset/pull/36950#issuecomment-3718543834

   ### <span aria-hidden="true">βœ…</span> Deploy Preview for *superset-docs-preview* ready!
   
   
   |  Name | Link |
   |:-:|------------------------|
   |<span aria-hidden="true">πŸ”¨</span> Latest commit | 37119f90451106b365eaeba3f7ca6ad7a194367a |
   |<span aria-hidden="true">πŸ”</span> Latest deploy log | https://app.netlify.com/projects/superset-docs-preview/deploys/695e49b1b3c15a0008180d16 |
   |<span aria-hidden="true">😎</span> Deploy Preview | [https://deploy-preview-36950--superset-docs-preview.netlify.app](https://deploy-preview-36950--superset-docs-preview.netlify.app) |
   |<span aria-hidden="true">πŸ“±</span> Preview on mobile | <details><summary> Toggle QR Code... </summary><br /><br />![QR Code](https://app.netlify.com/qr-code/eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1cmwiOiJodHRwczovL2RlcGxveS1wcmV2aWV3LTM2OTUwLS1zdXBlcnNldC1kb2NzLXByZXZpZXcubmV0bGlmeS5hcHAifQ.OCz05J_SqrWtBTiXys_UWgMiB4ihB5-PDYmKaW1dZgY)<br /><br />_Use your smartphone camera to open QR code link._</details> |
   ---
   <!-- [superset-docs-preview Preview](https://deploy-preview-36950--superset-docs-preview.netlify.app) -->
   _To edit notification comments on pull requests, go to your [Netlify project configuration](https://app.netlify.com/projects/superset-docs-preview/configuration/notifications#deploy-notifications)._


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix: streaming export losing g context [superset]

Posted by "codecov[bot] (via GitHub)" <gi...@apache.org>.
codecov[bot] commented on PR #36950:
URL: https://github.com/apache/superset/pull/36950#issuecomment-3718587299

   ## [Codecov](https://app.codecov.io/gh/apache/superset/pull/36950?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :x: Patch coverage is `16.66667%` with `20 lines` in your changes missing coverage. Please review.
   :white_check_mark: Project coverage is 48.48%. Comparing base ([`2080633`](https://app.codecov.io/gh/apache/superset/commit/2080633e57b7c2c5b4a819a2a03f681f96e17158?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`3966ee9`](https://app.codecov.io/gh/apache/superset/commit/3966ee988ceeadc32c5ec51acd19a987a9b2e703?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   :warning: Report is 13 commits behind head on master.
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/superset/pull/36950?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
   |---|---|---|
   | [superset/commands/streaming\_export/base.py](https://app.codecov.io/gh/apache/superset/pull/36950?src=pr&el=tree&filepath=superset%2Fcommands%2Fstreaming_export%2Fbase.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3VwZXJzZXQvY29tbWFuZHMvc3RyZWFtaW5nX2V4cG9ydC9iYXNlLnB5) | 16.66% | [20 Missing :warning: ](https://app.codecov.io/gh/apache/superset/pull/36950?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   ```diff
   @@             Coverage Diff             @@
   ##           master   #36950       +/-   ##
   ===========================================
   + Coverage        0   48.48%   +48.48%     
   ===========================================
     Files           0      639      +639     
     Lines           0    47672    +47672     
     Branches        0     5208     +5208     
   ===========================================
   + Hits            0    23114    +23114     
   - Misses          0    23903    +23903     
   - Partials        0      655      +655     
   ```
   
   | [Flag](https://app.codecov.io/gh/apache/superset/pull/36950/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Ξ” | |
   |---|---|---|
   | [hive](https://app.codecov.io/gh/apache/superset/pull/36950/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `43.09% <16.66%> (?)` | |
   | [presto](https://app.codecov.io/gh/apache/superset/pull/36950/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `46.68% <16.66%> (?)` | |
   | [python](https://app.codecov.io/gh/apache/superset/pull/36950/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `48.48% <16.66%> (?)` | |
   | [unit](https://app.codecov.io/gh/apache/superset/pull/36950/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `100.00% <ΓΈ> (?)` | |
   
   Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
   </details>
   
   [:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/superset/pull/36950?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).   
   :loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   - :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
   </details>


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix: streaming export losing g context [superset]

Posted by "codeant-ai-for-open-source[bot] (via GitHub)" <gi...@apache.org>.
codeant-ai-for-open-source[bot] commented on PR #36950:
URL: https://github.com/apache/superset/pull/36950#issuecomment-3718547988

   CodeAnt AI finished reviewing your PR.


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix: streaming export losing g context [superset]

Posted by "codeant-ai-for-open-source[bot] (via GitHub)" <gi...@apache.org>.
codeant-ai-for-open-source[bot] commented on PR #36950:
URL: https://github.com/apache/superset/pull/36950#issuecomment-3718540490

   ## Nitpicks πŸ”
   
   <table>
   <tr><td>πŸ”’&nbsp;<strong>No security issues identified</strong></td></tr>
   <tr><td>⚑&nbsp;<strong>Recommended areas for review</strong><br><br>
   
   - [ ] <a href='https://github.com/apache/superset/pull/36950/files#diff-a75c0b815273844db2c4d225e6f4e7a1350ebfbdf311e976304cb96b8027a9b6R38-R61'><strong>Context restore semantics</strong></a><br>`preserve_g_context` sets attributes on `g` and then deletes them in the finally block. If the target app context already had attributes with the same keys, they will be overwritten and then removed, potentially losing original values. The context manager should preserve and restore previous values (or avoid deleting pre-existing attributes).<br>
   
   - [ ] <a href='https://github.com/apache/superset/pull/36950/files#diff-a75c0b815273844db2c4d225e6f4e7a1350ebfbdf311e976304cb96b8027a9b6R78-R96'><strong>g capture method</strong></a><br>Capturing attributes via `dir(g)` and `getattr(g, key)` may pick up class attributes, descriptors, callables, or attributes that raise on access. This can cause unexpected behavior or exceptions during capture and may include non-instance data that should not be copied to the streaming context. Consider using the g instance dict (e.g. `g.__dict__`) or defensive access to only copy user-set attributes.<br>
   
   - [ ] <a href='https://github.com/apache/superset/pull/36950/files#diff-a75c0b815273844db2c4d225e6f4e7a1350ebfbdf311e976304cb96b8027a9b6R218-R236'><strong>Capture timing / staleness</strong></a><br>The code captures `g` attributes in `__init__`. If the command object is constructed outside the request/app context (or long before `run()` is called), the captured data may be empty or stale. Capture request-scoped state at the time `run()` is invoked (or immediately before starting the streaming generator).<br>
   
   </td></tr>
   </table>
   


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix: streaming export losing g context [superset]

Posted by "bito-code-review[bot] (via GitHub)" <gi...@apache.org>.
bito-code-review[bot] commented on PR #36950:
URL: https://github.com/apache/superset/pull/36950#issuecomment-3718781630

   AI Code Review is in progress (usually takes 3 to 15 minutes unless it's a very large PR).
   
   <details>
   <summary>
   Bito Usage Guide
   </summary>
   
   **Commands**
         
   Type the following command in the pull request comment and save the comment.
         
   - `/review` - Manually triggers a full AI review.
   
   - `/pause` - Pauses automatic reviews on this pull request.
   - `/resume` - Resumes automatic reviews.
   - `/resolve` - Marks all Bito-posted review comments as resolved.
   - `/abort` - Cancels all in-progress reviews.
   
         
   Refer to the <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/available-commands" target="_blank" rel="noopener noreferrer">documentation</a> for additional commands.
         
   **Configuration**
   
   This repository uses `Superset` You can customize the agent settings <a href="https://alpha.bito.ai/home/ai-agents/code-review-agent" target="_blank" rel="noopener noreferrer">here</a> or contact your Bito workspace admin at evan@preset.io.
         
   **Documentation & Help**
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/getting-started/install-run-using-bito-cloud/create-or-customize-an-agent-instance" target="_blank" rel="noopener noreferrer">Customize agent settings</a>
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/implementing-custom-code-review-rules" target="_blank" rel="noopener noreferrer">Review rules</a>
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent" target="_blank" rel="noopener noreferrer">General documentation</a>
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/faqs" target="_blank" rel="noopener noreferrer">FAQ</a>
   
   </details>
   


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix: streaming export losing g context [superset]

Posted by "bito-code-review[bot] (via GitHub)" <gi...@apache.org>.
bito-code-review[bot] commented on PR #36950:
URL: https://github.com/apache/superset/pull/36950#issuecomment-3718534264

   AI Code Review is in progress (usually takes 3 to 15 minutes unless it's a very large PR).


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix: streaming export losing g context [superset]

Posted by "codeant-ai-for-open-source[bot] (via GitHub)" <gi...@apache.org>.
codeant-ai-for-open-source[bot] commented on PR #36950:
URL: https://github.com/apache/superset/pull/36950#issuecomment-3718534007

   CodeAnt AI is reviewing your PR.
   
   ---
   
   ### Thanks for using CodeAnt! πŸŽ‰
   
   We're free for open-source projects. if you're enjoying it, help us grow by sharing.
   
   [Share on X](https://twitter.com/intent/tweet?text=Just%20tried%20%40CodeAntAI%20for%20automated%20code%20review%20and%20I%27m%20impressed%21%20Free%20for%20open%20source%20with%20a%20free%20trial%20for%20private%20repos.%20Worth%20checking%20out%3A&url=https%3A//codeant.ai) Β·
   [Reddit](https://www.reddit.com/submit?title=Check%20out%20CodeAnt%20for%20automated%20code%20review&text=Just%20tried%20CodeAnt%20for%20automated%20code%20review%20and%20I%27m%20impressed%21%20Free%20for%20open%20source%20with%20a%20free%20trial%20for%20private%20repos.%20Worth%20checking%20out%3A%20https%3A//codeant.ai) Β·
   [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcodeant.ai&mini=true&title=Check%20out%20CodeAnt%20for%20automated%20code%20review&summary=Just%20tried%20CodeAnt%20for%20automated%20code%20review%20and%20I%27m%20impressed%21%20Free%20for%20open%20source%20with%20a%20free%20trial%20for%20private%20repos)
   


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix: streaming export losing g context [superset]

Posted by "dpgaspar (via GitHub)" <gi...@apache.org>.
dpgaspar merged PR #36950:
URL: https://github.com/apache/superset/pull/36950


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix: streaming export losing g context [superset]

Posted by "codeant-ai-for-open-source[bot] (via GitHub)" <gi...@apache.org>.
codeant-ai-for-open-source[bot] commented on code in PR #36950:
URL: https://github.com/apache/superset/pull/36950#discussion_r2668161471


##########
superset/commands/streaming_export/base.py:
##########
@@ -34,6 +35,31 @@
 logger = logging.getLogger(__name__)
 
 
+@contextmanager
+def preserve_g_context(
+    captured_g: dict[str, Any],
+) -> Generator[None, None, None]:
+    """
+    Context manager that restores captured flask.g attributes.
+
+    This is needed for streaming responses where the generator runs in a new
+    app context but needs access to request-scoped data (like tenant info
+    in multi-tenant setups).
+
+    Args:
+        captured_g: Dictionary of g attributes captured before context switch
+    """
+    for key, value in captured_g.items():
+        setattr(g, key, value)
+    try:
+        yield
+    finally:
+        # Clean up the attributes we set
+        for key in captured_g:
+            if hasattr(g, key):
+                delattr(g, key)

Review Comment:
   **Suggestion:** The cleanup in `preserve_g_context` deletes attributes on `flask.g` unconditionally, which will remove attributes that existed before the context manager ran and cause state loss across contexts; instead capture the original presence/value for each key and restore original values (or delete only keys that were not present originally) and guard attribute restores/deletions to avoid raising during cleanup. [logic error]
   
   **Severity Level:** Minor ⚠️
   ```suggestion
       # Record original state for keys we may override so we can restore it
       original_values: dict[str, Any] = {}
       original_present: set[str] = set()
       for key, value in captured_g.items():
           if hasattr(g, key):
               try:
                   original_values[key] = getattr(g, key)
                   original_present.add(key)
               except Exception:
                   # If reading the original raises, treat as not present
                   pass
           # set the captured value for the new context
           try:
               setattr(g, key, value)
           except Exception:
               # If setting fails, skip that attribute
               continue
       try:
           yield
       finally:
           # Restore original values if they existed, otherwise remove keys we set
           for key in captured_g:
               if key in original_present:
                   try:
                       setattr(g, key, original_values[key])
                   except Exception:
                       # Best-effort restore; swallow to avoid breaking cleanup
                       pass
               else:
                   try:
                       if hasattr(g, key):
                           delattr(g, key)
                   except Exception:
                       # Best-effort deletion; swallow to avoid breaking cleanup
                       pass
   ```
   <details>
   <summary><b>Why it matters? ⭐ </b></summary>
   
   The current implementation sets captured values onto flask.g then indiscriminately deletes those attributes on exit, which will remove any attributes that originally existed and cause state loss across contexts. The proposed change records original presence/values and restores them (or deletes only what was introduced), fixing a real logic bug that can break request-scoped state.
   </details>
   <details>
   <summary><b>Prompt for AI Agent πŸ€– </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** superset/commands/streaming_export/base.py
   **Line:** 53:60
   **Comment:**
   	*Logic Error: The cleanup in `preserve_g_context` deletes attributes on `flask.g` unconditionally, which will remove attributes that existed before the context manager ran and cause state loss across contexts; instead capture the original presence/value for each key and restore original values (or delete only keys that were not present originally) and guard attribute restores/deletions to avoid raising during cleanup.
   
   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.
   ```
   </details>



##########
superset/commands/streaming_export/base.py:
##########
@@ -58,6 +84,15 @@ def __init__(self, chunk_size: int = 1000):
         """
         self._chunk_size = chunk_size
         self._current_app = app._get_current_object()
+        # Capture flask.g attributes for multi-tenant support.
+        # When streaming responses run in a new app context, we need to
+        # preserve request-scoped data like tenant/workspace info.
+        self._captured_g: dict[str, Any] = {}
+        if has_app_context():
+            # Capture all g attributes (excludes internal Flask attributes)
+            self._captured_g = {
+                key: getattr(g, key) for key in dir(g) if not key.startswith("_")
+            }

Review Comment:
   **Suggestion:** Capturing attributes from `g` using `dir(g)` will include methods, descriptors and other non-data attributes (and may include objects that can't/shouldn't be moved between contexts); capture only actual stored data attributes (e.g. from `g.__dict__`) and skip callables and private attributes, and avoid sharing mutable objects by shallow-copying container types. [possible bug]
   
   **Severity Level:** Critical 🚨
   ```suggestion
               # Capture only actual data attributes stored on g (avoid methods/descriptors)
               try:
                   g_dict = getattr(g, "__dict__", {}) or {}
                   captured: dict[str, Any] = {}
                   for key, val in g_dict.items():
                       if key.startswith("_"):
                           continue
                       # skip callables / methods to avoid overriding behavior on g
                       if callable(val):
                           continue
                       # shallow copy mutable containers to avoid sharing references
                       try:
                           if hasattr(val, "copy"):
                               captured[key] = val.copy()
                           else:
                               captured[key] = val
                       except Exception:
                           # Skip attributes that cannot be copied/read safely
                           continue
                   self._captured_g = captured
               except Exception:
                   # If capturing fails for any reason, fall back to empty capture
                   self._captured_g = {}
   ```
   <details>
   <summary><b>Why it matters? ⭐ </b></summary>
   
   Using dir(g) pulls in methods and descriptors and may capture non-data attributes; additionally, storing references to mutable objects can cause surprising shared-state when restored in another context. Restricting capture to g.__dict__, skipping callables/private names, and shallow-copying container types addresses real correctness concerns.
   </details>
   <details>
   <summary><b>Prompt for AI Agent πŸ€– </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** superset/commands/streaming_export/base.py
   **Line:** 92:95
   **Comment:**
   	*Possible Bug: Capturing attributes from `g` using `dir(g)` will include methods, descriptors and other non-data attributes (and may include objects that can't/shouldn't be moved between contexts); capture only actual stored data attributes (e.g. from `g.__dict__`) and skip callables and private attributes, and avoid sharing mutable objects by shallow-copying container types.
   
   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.
   ```
   </details>



##########
superset/commands/streaming_export/base.py:
##########
@@ -192,23 +227,27 @@ def run(self) -> Callable[[], Generator[str, None, None]]:
         # to avoid DetachedInstanceError
         sql, database = self._get_sql_and_database()
         limit = self._get_row_limit()
+        # Capture g attributes for restoration in the new context
+        captured_g = self._captured_g
 

Review Comment:
   **Suggestion:** Capturing request-scoped `g` attributes at object construction time can be too early (no request or different request), causing stale or missing values during streaming; capture `g` lazily at the time `run()` is invoked (when an app/request context is available) if nothing was captured earlier. [state inconsistency]
   
   **Severity Level:** Minor ⚠️
   ```suggestion
           # If nothing was captured at init time, capture lazily here when an app/request
           # context is available so we don't store stale or partial request-scoped state.
           captured_g = self._captured_g
           if not captured_g and has_app_context():
               try:
                   g_dict = getattr(g, "__dict__", {}) or {}
                   captured: dict[str, Any] = {}
                   for key, val in g_dict.items():
                       if key.startswith("_"):
                           continue
                       if callable(val):
                           continue
                       try:
                           captured[key] = val.copy() if hasattr(val, "copy") else val
                       except Exception:
                           continue
                   captured_g = captured
               except Exception:
                   captured_g = {}
   ```
   <details>
   <summary><b>Why it matters? ⭐ </b></summary>
   
   Capturing request-scoped state at object construction (when an app/request context may not exist) can produce empty or stale captures. Capturing lazily in run() (when an app context is available) avoids missing or stale values during streaming. This change prevents state inconsistencies for long-lived objects created outside request handling.
   </details>
   <details>
   <summary><b>Prompt for AI Agent πŸ€– </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** superset/commands/streaming_export/base.py
   **Line:** 232:232
   **Comment:**
   	*State Inconsistency: Capturing request-scoped `g` attributes at object construction time can be too early (no request or different request), causing stale or missing values during streaming; capture `g` lazily at the time `run()` is invoked (when an app/request context is available) if nothing was captured earlier.
   
   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.
   ```
   </details>



-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix: streaming export losing g context [superset]

Posted by "bito-code-review[bot] (via GitHub)" <gi...@apache.org>.
bito-code-review[bot] commented on code in PR #36950:
URL: https://github.com/apache/superset/pull/36950#discussion_r2668167408


##########
superset/commands/streaming_export/base.py:
##########
@@ -192,23 +227,27 @@ def run(self) -> Callable[[], Generator[str, None, None]]:
         # to avoid DetachedInstanceError
         sql, database = self._get_sql_and_database()
         limit = self._get_row_limit()
+        # Capture g attributes for restoration in the new context
+        captured_g = self._captured_g
 
         def csv_generator() -> Generator[str, None, None]:
             """Generator that yields CSV data chunks."""
             with self._current_app.app_context():
-                try:
-                    yield from self._execute_query_and_stream(sql, database, limit)
-                except Exception as e:
-                    logger.error("Error in streaming CSV generator: %s", e)
-                    import traceback
-
-                    logger.error("Traceback: %s", traceback.format_exc())
-
-                    # Send error marker for frontend to detect
-                    error_marker = (
-                        "__STREAM_ERROR__:Export failed. "
-                        "Please try again in some time.\n"
-                    )
-                    yield error_marker
+                # Restore flask.g attributes (e.g., tenant info for multi-tenant setups)
+                with preserve_g_context(captured_g):
+                    try:
+                        yield from self._execute_query_and_stream(sql, database, limit)
+                    except Exception as e:

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Blind exception catch too broad</b></div>
   <div id="fix">
   
   Replace the broad `Exception` catch with specific exception types (e.g., `DatabaseError`, `SQLAlchemyError`) to avoid catching unexpected errors.
   </div>
   
   
   <details>
   <summary>
   <b>Code suggestion</b>
   </summary>
   <blockquote>Check the AI-generated fix before applying</blockquote>
   <div id="code">
   
   
   ````suggestion
                       except (SQLAlchemyError, Exception) as e:
   ````
   
   </div>
   </details>
   
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #65a420</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org