superset icon indicating copy to clipboard operation
superset copied to clipboard

fix: streaming export losing g context

Open dpgaspar opened this issue 2 months ago β€’ 6 comments

SUMMARY

Streaming CSV exports create a new Flask app context when the generator runs, which causes flask.g attributes from the original request to be lost. This can break functionality that relies on request-scoped data stored in g.

This fix:

  1. Captures all flask.g attributes when the command is initialized (during the original request context)
  2. Restores them via preserve_g_context() when the generator executes in the new app context

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

N/A - backend change

TESTING INSTRUCTIONS

  1. Run existing tests: pytest tests/unit_tests/commands/sql_lab/streaming_export_command_test.py -v
  2. All 16 tests should pass

ADDITIONAL INFORMATION

  • [ ] Has associated issue:
  • [ ] Required feature flags:
  • [ ] Changes UI
  • [ ] Includes DB Migration (follow approval process in SIP-59)
    • [ ] 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

dpgaspar avatar Jan 07 '26 11:01 dpgaspar

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 Β· Reddit Β· LinkedIn

Nitpicks πŸ”

πŸ”’Β No security issues identified
⚑ Recommended areas for review

  • [ ] Context restore semantics
    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).

  • [ ] g capture method
    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.

  • [ ] Capture timing / staleness
    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).

Deploy Preview for superset-docs-preview ready!

Name Link
Latest commit 37119f90451106b365eaeba3f7ca6ad7a194367a
Latest deploy log https://app.netlify.com/projects/superset-docs-preview/deploys/695e49b1b3c15a0008180d16
Deploy Preview https://deploy-preview-36950--superset-docs-preview.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

netlify[bot] avatar Jan 07 '26 11:01 netlify[bot]

CodeAnt AI finished reviewing your PR.

Codecov Report

:x: Patch coverage is 23.52941% with 13 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 68.17%. Comparing base (2080633) to head (cf3d135). :warning: Report is 24 commits behind head on master.

Files with missing lines Patch % Lines
superset/commands/streaming_export/base.py 23.52% 13 Missing :warning:
Additional details and impacted files
@@             Coverage Diff             @@
##           master   #36950       +/-   ##
===========================================
+ Coverage        0   68.17%   +68.17%     
===========================================
  Files           0      639      +639     
  Lines           0    47665    +47665     
  Branches        0     5205     +5205     
===========================================
+ Hits            0    32494    +32494     
- Misses          0    13891    +13891     
- Partials        0     1280     +1280     
Flag Coverage Ξ”
hive 43.09% <23.52%> (?)
mysql 66.17% <23.52%> (?)
postgres 66.22% <23.52%> (?)
presto 46.69% <23.52%> (?)
python 68.14% <23.52%> (?)
sqlite 65.94% <23.52%> (?)
unit 100.00% <ΓΈ> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Jan 07 '26 12:01 codecov[bot]

Code Review Agent Run #8fd3f4

Actionable Suggestions - 0
Review Details
  • Files reviewed - 1 Β· Commit Range: 37119f9..cf3d135
    • superset/commands/streaming_export/base.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - βœ”οΈŽ Successful
    • Detect-secrets (Secret Scanner) - βœ”οΈŽ Successful
    • MyPy (Static Code Analysis) - βœ”οΈŽ Successful
    • Astral Ruff (Static Code Analysis) - βœ”οΈŽ Successful

Bito Usage Guide

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 documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

bito-code-review[bot] avatar Jan 07 '26 13:01 bito-code-review[bot]