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

[PR] fix(db): use a private engine for prequery connections to avoid listener race [superset]

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

   ### SUMMARY
   
   Fixes an intermittent `RuntimeError: deque mutated during iteration` (surfacing as HTTP 500s and E2E flakiness) caused by an interaction between two changes:
   
   - #40194 made `Database.get_sqla_engine` attach a per-call SQLAlchemy `connect` listener when prequeries exist (e.g. `SET search_path` on PostgreSQL), removing it again on context exit — two mutations of the engine's connect-listener deque per call.
   - #40237 introduced `_ENGINE_CACHE`, making `Engine` objects **shared across threads** per `(database_id, url, engine_kwargs)`.
   
   SQLAlchemy stores event listeners in a plain, unlocked `collections.deque`, and dispatch iterates it unguarded (`for fn in self.listeners`). With NullPool, every `raw_connection()` creates a fresh DBAPI connection and fires that dispatch. So one thread's connection checkout can iterate the shared engine's deque while another thread's `get_sqla_engine` enter/exit mutates it — a classic race that reproduces readily under concurrent load (observed as consistent `create-dataset` 500s on one Playwright run and flaked-but-recovered failures on another).
   
   **Fix:** compute prequeries *before* fetching the engine, and request a **private, uncached engine** (`cacheable=False`) whenever prequeries are present. The per-call listener add/remove then only ever touches an engine no other thread can see. No-prequery paths keep the shared cache and its #27897 semantics. The listener also closes over per-call, schema-specific prequeries and must not leak to other requests sharing a cached engine — which is why a register-once approach was rejected.
   
   This restores the pre-#40237 behaviour for prequery engines only (fresh engine per call); prequery-less databases keep full engine caching.
   
   ### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
   
   N/A (backend concurrency fix)
   
   ### TESTING INSTRUCTIONS
   
   Unit tests in `tests/unit_tests/models/core_test.py`:
   
   - `test_prequery_engine_bypasses_shared_cache` — deterministic regression: with prequeries the yielded engine is never the cached instance and `_ENGINE_CACHE` is untouched; without prequeries caching behaviour is unchanged.
   - `test_prequeries_execute_on_real_connections` — behavioural guard for #40194: prequeries still execute on every new DBAPI connection (file-backed SQLite, table-creating prequery, observed on the same connection).
   - `test_concurrent_prequery_connections_do_not_race` — 4 threads × 50 `get_raw_connection` calls with prequeries. **Against the previous code this reliably reproduces the production error** (3/4 threads raised `RuntimeError: deque mutated during iteration` in under a second); with this fix it passes deterministically.
   
   ```bash
   pytest tests/unit_tests/models/core_test.py -q
   ```
   
   ### ADDITIONAL INFORMATION
   - [ ] 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
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)


-- 
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(db): use a private engine for prequery connections to avoid listener race [superset]

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

   ## [Codecov](https://app.codecov.io/gh/apache/superset/pull/41642?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 `50.00000%` with `1 line` in your changes missing coverage. Please review.
   :white_check_mark: Project coverage is 55.80%. Comparing base ([`393adc4`](https://app.codecov.io/gh/apache/superset/commit/393adc45356b06c98b3826a25b81dd1752fbc0df?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`9d70445`](https://app.codecov.io/gh/apache/superset/commit/9d70445875864c71415e58647c5387e2a0cb4b63?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/superset/pull/41642?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/models/core.py](https://app.codecov.io/gh/apache/superset/pull/41642?src=pr&el=tree&filepath=superset%2Fmodels%2Fcore.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3VwZXJzZXQvbW9kZWxzL2NvcmUucHk=) | 50.00% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/apache/superset/pull/41642?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   > :exclamation:  There is a different number of reports uploaded between BASE (393adc4) and HEAD (9d70445). Click for more details.
   > 
   > <details><summary>HEAD has 17 uploads less than BASE</summary>
   >
   >| Flag | BASE (393adc4) | HEAD (9d70445) |
   >|------|------|------|
   >|python|15|2|
   >|presto|3|1|
   >|hive|3|1|
   ></details>
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   ```diff
   @@            Coverage Diff             @@
   ##           master   #41642      +/-   ##
   ==========================================
   - Coverage   64.52%   55.80%   -8.72%     
   ==========================================
     Files        2673     2673              
     Lines      147639   147639              
     Branches    34093    34093              
   ==========================================
   - Hits        95259    82385   -12874     
   - Misses      50651    64508   +13857     
   + Partials     1729      746     -983     
   ```
   
   | [Flag](https://app.codecov.io/gh/apache/superset/pull/41642/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/41642/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `39.06% <50.00%> (ø)` | |
   | [mysql](https://app.codecov.io/gh/apache/superset/pull/41642/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   | [postgres](https://app.codecov.io/gh/apache/superset/pull/41642/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   | [presto](https://app.codecov.io/gh/apache/superset/pull/41642/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `40.61% <50.00%> (ø)` | |
   | [python](https://app.codecov.io/gh/apache/superset/pull/41642/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `40.67% <50.00%> (-18.48%)` | :arrow_down: |
   | [sqlite](https://app.codecov.io/gh/apache/superset/pull/41642/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   | [unit](https://app.codecov.io/gh/apache/superset/pull/41642/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   
   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 Harness](https://app.codecov.io/gh/apache/superset/pull/41642?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(db): use a private engine for prequery connections to avoid listener race [superset]

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

   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