Skip to content

Web UI: restore expanded databases when reopening the database panel - #108964

Merged
alexey-milovidov merged 15 commits into
masterfrom
play-restore-expanded-databases
Jul 2, 2026
Merged

alexey-milovidov merged 15 commits into
masterfrom
play-restore-expanded-databases

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jun 30, 2026 •

Copy link
Copy Markdown
Member

In the Web UI (play.html), closing the database panel removed all database elements from the DOM, so reopening it rebuilt the list with every database collapsed. This change remembers which databases were expanded and re-expands the ones that still exist when the panel is reopened.

It also fixes a race condition when several databases are expanded in parallel (which happens during the restore above): loadTables used a single global staleness token, so when multiple loads ran concurrently all but the last were cancelled and their tables never appeared. The token is now scoped per container via a WeakMap, so a load is only superseded by a newer load of the same database. Additionally, restoring the expanded state no longer moves the current-database highlight away from the actual currentDatabase.

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Web UI: when the database panel is reopened, the previously expanded databases are restored.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.7.1.456 (included in 26.7 and later)

In the Web UI (play.html), closing the database panel removed all database
elements, so reopening it rebuilt the list with every database collapsed.
Remember which databases were expanded and re-expand the ones that still
exist when the panel is reopened.

Fix a race condition when several databases are expanded in parallel (e.g.
during restore): loadTables used a single global staleness token, so all
but the last concurrent load were cancelled. The token is now scoped per
container via a WeakMap, and restoring the expanded state no longer moves
the current-database highlight.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f689697]

Summary: ✅


AI Review

Summary

This PR makes the Web UI remember expanded databases across closing and reopening the database panel, and it adds the right staleness guards for overlapping table loads, connection changes, and background restores. Most of the earlier race conditions now look covered, but one state transition is still wrong: once a previously remembered database starts failing to restore, the current was_remembered logic leaves it remembered forever, so reopening the panel keeps silently retrying the same broken system.tables load and the user has no normal panel action that clears that state.

Findings

⚠️ Majors

  • programs/server/play.html:3399 A database that was already in opened_databases is never removed on a failed reopen, even when the failure comes from an explicit user click on a still-collapsed row. That makes the remembered-open state effectively write-only for persistently failing databases: the row never reaches dataset['opened'], so the collapse path is unavailable, and every future panel reopen silently retries the same failing system.tables query.
    Suggested fix: preserve remembered state only for background restore (set_current = false), or add another explicit path that clears the remembered state for a row that failed to re-open.
Tests
  • ⚠️ I do not see any automated coverage for the panel lifecycle this PR now depends on. The smallest useful proof would exercise: a remembered database restoring successfully after panel reopen, a restore failure of an already-remembered database, an explicit retry after that failure, and connection-change invalidation while a database-list or table-list load is still in flight.
Final Verdict

Request changes.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jun 30, 2026
Comment thread programs/server/play.html Resolved
…n token

Addresses the AI review on the PR: the new per-container `loadTables`
staleness token (a `WeakMap` keyed by the database container) only guards
each container's own table list, but the `.current` highlight is a singleton.

If the user opens database `a` and then `b` before `a`'s `loadTables` query
returns, `b` could finish first and become current, then the slower `a` load
would finish later, pass its own container's token check, and move `.current`
back to `a` - leaving the UI highlighting response order rather than the user's
last selection. Before the per-container change the single global token would
have cancelled `a`'s stale load, so this is a regression introduced by it.

The highlight now uses a separate `current_selection_num` token, captured
synchronously before awaiting so the latest click wins regardless of which
query returns first. Restoring the expanded state still passes
`set_current = false` and never touches the highlight. Also guard against a
missing `.current` element instead of dereferencing `null`.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Pushed fe5d4b2e8b5..017a3205a57 addressing the AI review ⚠️ Request changes (the only blocking feedback).

Fix: The per-container loadTables WeakMap token only guards each database container's own table list, but the .current highlight is a singleton, so overlapping manual opens could leave it on the response-order database instead of the last-selected one. The highlight now uses a separate current_selection_num token, captured synchronously before awaiting, so the latest click wins regardless of which load's query returns first. Restore (set_current = false) never touches the highlight; also added a null guard for a missing .current element. Resolved the corresponding review thread.

CI: The 2 red Stress (arm_asan_ubsan) jobs are unrelated master-wide flakes — impossible for a play.html-only change to cause:

The change is play.html-only (+15/-4 on top of the original), JS validated with node --check. Fresh CI is running against the new commit.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

Merged current master (017a3205a57..2c0e72f8171).

Motivation: master evolved programs/server/play.html in the exact same functions this PR touches — #108891 added a table count next to each opened database (modifying the collapse branch of loadDatabases and the body of loadTables), and #108941 fixed a broken Web UI / horizontal-scroll issue. Although GitHub reported the branch as mergeable (no textual conflict), re-merging validates that both features coexist and runs fresh CI on the combined result.

The auto-merge was conflict-free and the combined code is coherent:

  • loadDatabases collapse branch now does all three: delete database.dataset['opened'], opened_databases.delete(...) (this PR), clear tables, and remove the .database-table-count element (Show table count next to opened database in Web UI #108891).
  • loadTables keeps the per-container staleness token + latest-selection highlight guard (this PR) before the table-count block (Show table count next to opened database in Web UI #108891); the count <span> is appended inside database_link so container.firstChild still resolves to the link, and the explicit .database-table-count { font-weight: normal } rule keeps the count non-bold even when the database is .current.

The PR's net contribution vs master is unchanged (play.html only, +53/-15). JS validated with node --check.

@alexey-milovidov
alexey-milovidov requested a review from pamarcos July 1, 2026 06:03
@pamarcos pamarcos self-assigned this Jul 1, 2026

@pamarcos pamarcos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice UX improvement. I've tested it and it works fine.

…ed-databases

Resolve the `programs/server/play.html` conflict with the database refresh
button feature that landed on master (#108958).

Both features independently reworked the database-list loop body and the
6th parameter of `loadTables`: this PR added `openDatabase(set_current)`
plus the `opened_databases` restore set and a per-container `WeakMap`
staleness token with a `set_current`/`current_selection_num` highlight
guard, while master added `refreshTables`, the per-row `row_load_generation`
counter and the `is_current` predicate for the refresh button.

The merge keeps both: `refreshTables` now takes `set_current` (defaulting to
true, preserving master's refresh behaviour) and threads it into
`loadTables`; `openDatabase` wraps `refreshTables` to mark the row opened,
remember it in `opened_databases` and attach the refresh button; restore
calls `openDatabase(false)` so it never moves the current-database
highlight. `loadTables` keeps the per-container token, the `is_current`
predicate guard and the latest-selection highlight guard together.
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Merged master to resolve the conflict (2c0e72f8171..44b7e368c81). The PR had become CONFLICTING because the database refresh-button feature (#108958) landed on master and reworked the exact same programs/server/play.html regions this PR touches — the database-list loop body and the 6th parameter of loadTables.

Both features are kept:

  • refreshTables now takes set_current (default true, preserving master's refresh behaviour) and threads it into loadTables.
  • openDatabase(set_current) wraps refreshTables to mark the row opened, remember it in opened_databases and attach the refresh button; restore calls openDatabase(false) so it never moves the current-database highlight.
  • loadTables keeps all three guards together: the per-container WeakMap staleness token (concurrent multi-database restore), master's is_current predicate (collapse / panel-close invalidation), and the set_current/current_selection_num latest-selection highlight guard (including the .current null-guard).

Net change versus master is exactly the restore feature layered on top of the refresh button; nothing from #108958 is lost. node --check on the extracted inline script passes.

Comment thread programs/server/play.html Resolved
Address review (#108964,
commit 44b7e36): the `opened_databases` set that remembers which
databases the user expanded was page-global, so it survived changes to
`url` / `user` / `password`. Reopening the panel on a different server
or as a different user would then auto-expand same-named databases and
issue unintended `system.tables` queries there.

`system.tables` (like `system.completions`) is connection-scoped, so
clear `opened_databases` from the same input handlers that already
invalidate the cached completions on a connection change. Renamed the
handler `resetCompletions` -> `resetConnectionScopedState` to reflect
that it now resets both pieces of connection-scoped state.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108964&sha=44b7e368c817e3a3873ae053193d99f8e6d6ddda&name_0=PR

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Addressed the AI review's Request changes finding in 861b55c: the opened_databases set that restores expanded databases was page-global, so it survived url/user/password changes and would auto-expand same-named databases (firing extra system.tables queries) after switching to a different server or user.

system.tables is connection-scoped just like system.completions, so opened_databases is now cleared from the same input handler that already invalidates the cached completions on a connection change (renamed resetCompletions -> resetConnectionScopedState). Reopening the panel after a connection change now starts from a clean slate. node --check on the extracted inline script passes.

…ight loads

The previous fix cleared `opened_databases` in `resetConnectionScopedState`
when the connection settings change, but a database load started before that
change could still complete afterwards and unconditionally re-add its database
to the set in `openDatabase`. Reopening the panel on the new connection would
then auto-expand a same-named database and fire an extra `system.tables` query
there.

Generalize the existing completions connection-generation counter (renamed
`completionsLoadGeneration` -> `connectionGeneration`, since it already tracks
exactly the connection-change event) and capture it in `openDatabase` before
the load, so a stale completion no longer re-populates `opened_databases`
after the connection has changed. Only the connection-scoped set is guarded;
the row's own DOM state still reflects the load it actually performed.

Addresses the AI review's Request-changes finding.
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Addressed the AI review's follow-up Request changes finding in e688c9e.

The earlier fix cleared opened_databases when the connection settings change, but a database load already in flight could complete afterwards and unconditionally re-add its database to the set in openDatabase, resurrecting it for the next panel reopen on the new connection.

openDatabase now captures the connection generation before the load and re-adds to opened_databases only while that generation is still current. I generalized the existing completions counter (completionsLoadGeneration -> connectionGeneration) because it is already bumped in exactly this connection-change handler, so completions and the expanded-databases set now share one connection-generation guard. Only the connection-scoped set is guarded — the row's own DOM (table list, opened flag, refresh button) still reflects the load it actually performed. Completions behavior is unchanged (a pure rename, no new bump).

Comment thread programs/server/play.html Resolved
Address the AI review's follow-up `Request changes` finding on the
connection-scoped state introduced in the previous commits.

`resetConnectionScopedState` bumps `connectionGeneration` (invalidating
completions and the remembered expanded-database set) when the `url` /
`user` / `password` fields change, and `loadCompletions` / `openDatabase`
already honor that generation. `loadDatabases` did not: it only checked
`load_databases_request_num`, which is bumped only when the panel is
(re)opened, not when the connection settings change. So if the user starts
opening the database panel on server A and edits the connection fields to
server B before `system.databases` returns, the stale A response still
filled `#databases` and `toggleDatabases` opened the panel. Every row
handler created there closes over the connection `loadDatabases` was called
with, so subsequent expand/refresh actions kept querying the old server
until the panel was closed and reopened.

Capture `connectionGeneration` at the start of `loadDatabases` and drop the
response if it changed while the query was in flight, mirroring the guard in
`loadCompletions`.
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Addressed the AI review's follow-up Request changes finding in 8c95d0e.

The previous commits made loadCompletions, openDatabase, and the remembered expanded-database set (opened_databases) honor connectionGeneration, but loadDatabases itself did not — it only checked load_databases_request_num, which is bumped when the panel is (re)opened, not when the connection settings change. So starting to open the panel on server A and then editing url/user/password to server B before system.databases returned still let the stale A response fill #databases and open the panel, with every row handler closing over the old connection.

loadDatabases now captures connectionGeneration at the start and discards the response if it changed while the query was in flight, mirroring the guard in loadCompletions. The stale panel and its row handlers no longer appear after a connection change.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

Merged master into the branch (9cd22598df2) to integrate the overlapping play.html changes that landed since — the database-panel keyboard navigation (#108972) and the column color-coding modes (#108873), which rework the same database-panel region this PR restructures. The auto-merge was conflict-free; both the keyboard navigation and the restore-expanded-databases / refresh-button behavior are preserved, resetConnectionScopedState still bumps connectionGeneration and clears opened_databases, and the extracted inline script passes node --check. The diff against master is this PR's feature only (no master change dropped).

Comment thread programs/server/play.html Resolved
Addresses the AI review's follow-up `Request changes` finding
(programs/server/play.html:5367).

`resetConnectionScopedState` already bumped `connectionGeneration`, cleared
the cached completions, and cleared the remembered `opened_databases` set on
a connection edit, but a database panel that was *already fully open* was left
untouched. Its rows and refresh buttons are closures created by the earlier
`loadDatabases(server_address, user, password)` call, so after editing
`url`/`user`/`password` to a different server, clicking a still-visible row
(or its refresh button) kept querying the previous server. None of the
existing `connectionGeneration` guards covered this: they discard in-flight
responses, but the query parameters are baked into each row's closure at
`loadDatabases` time, so a fresh click still targets the old connection.

The panel-close logic is factored out of `toggleDatabases` into `closeDatabases`,
and `resetConnectionScopedState` now closes the panel when it is open. The
panel is rebuilt against the new connection on the next open, and detaching
the rows also drops any in-flight `loadTables` load through its `isConnected`
check.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Addressed the AI review's follow-up Request changes finding in ea546e2.

Editing url/user/password while the database panel was already open left the panel bound to the previous connection: its rows and refresh buttons are closures created by the earlier loadDatabases(server_address, user, password) call, so clicking a still-visible row (or its refresh button) kept querying the previous server. The earlier connectionGeneration guards do not cover this, because they only discard in-flight responses — the connection parameters are baked into each row closure at loadDatabases time, so a fresh click still targets the old connection.

resetConnectionScopedState now closes the panel when it is open (the close logic is factored out of toggleDatabases into closeDatabases). The panel is rebuilt against the new connection on the next open, and detaching the rows also drops any in-flight loadTables load through its isConnected check. node --check on the extracted inline script passes.

This completes the connection-scoped invalidation started by the earlier commits (completions, opened_databases, loadDatabases, openDatabase) so that a connection change now fully invalidates the connection-scoped UI state, including the live panel.

Comment thread programs/server/play.html Resolved
Address the AI review's `Request changes` finding
(programs/server/play.html:3310-3313).

The restore feature records an expanded database in `opened_databases` only
in the success callback of `openDatabase`, after its `system.tables` load
finishes. If the user clicks a collapsed database and then closes the whole
panel before the query returns, `closeDatabases` detaches the row, `loadTables`
bails out through its `isConnected` check, so the success callback never runs
and the just-performed open is never remembered. Reopening the panel then
comes back fully collapsed, contradicting the advertised behavior.

Record the open synchronously at the start of `openDatabase` instead, so the
user's intent survives a panel close (or any other reason the in-flight load
drops). The entry is still removed only on an explicit collapse or a
connection change: `resetConnectionScopedState` both clears `opened_databases`
and closes the panel, so a stale load can no longer resurrect an entry after a
connection edit. This makes the previous `connectionGeneration` guard around
the add redundant, so it is removed.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Addressed the AI review's latest Request changes finding (programs/server/play.html:3310-3313) in 0fa3017.

Bug: the restore feature recorded an expanded database in opened_databases only in the success callback of openDatabase, after its system.tables load finished. Clicking a collapsed database and then closing the whole panel before the query returned detached the row, loadTables bailed out via isConnected, the success callback never ran, and the just-performed open was forgotten — so reopening the panel showed it collapsed, contradicting the advertised behavior.

Fix: record the open synchronously at the start of openDatabase, so the user's intent survives a panel close (or any other in-flight-load drop). Removal still happens only on an explicit collapse or a connection change (resetConnectionScopedState clears the set and closes the panel), so a stale load cannot resurrect an entry after a connection edit; the previous connectionGeneration guard around the add is now redundant and was removed.

node --check on the extracted inline script passes. Fresh CI is running on 0fa3017.

Comment thread programs/server/play.html Resolved
The restore feature records an expanded database in `opened_databases`
synchronously at the start of `openDatabase`, so that closing the panel
while the `system.tables` load is still in flight does not forget the
user's just-performed open. However, if the load instead *fails* while
the row is still on screen (for example `system.tables` throws an
exception or the request errors out), the row never actually expands
(`database.dataset['opened']` stays unset), yet the database name is
left in the restore set. Reopening the panel then auto-retries that
database and replays the same error, even though the user never had a
successfully expanded row.

`openDatabase` now reconciles the restore set with the row's actual
state once the load settles, but only while the row is still attached:
a failed open is rolled back, while a genuine success (re-)adds it. A
panel close detaches the row (`isConnected === false`) and is skipped,
so a just-performed open still survives for restore. This keeps the
restore set in sync with the visible panel.

`node --check` on the extracted inline script passes.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Addressed the AI review's Request changes finding (programs/server/play.html:3310) in 89ea47e.

Bug: the previous commit recorded an expanded database in opened_databases synchronously at the start of openDatabase (so closing the panel while the system.tables load is still in flight does not forget the open). But if the load instead failed while the row was still on screen (e.g. system.tables threw or the request errored), the row never actually expanded (database.dataset[opened] stays unset), yet the name stayed in the restore set — so reopening the panel auto-retried that database and replayed the same error even though the user never had a successfully expanded row.

Fix: openDatabase now reconciles the restore set with the row's actual state once the load settles, but only while the row is still attached (database.isConnected): a failed open is rolled back (opened_databases.delete), while a genuine success (re-)adds it. A panel close detaches the row (isConnected === false) and is skipped, so a just-performed open still survives for restore. This keeps the restore set in sync with the visible panel, exactly as suggested ("roll the name back when the load fails while the row is still current/connected").

The symmetric add-on-success also covers the double-click case (a superseded sibling load of the same row rolling the entry back just before the winning load completes). node --check on the extracted inline script passes. Fresh CI is running on 89ea47e.

Comment thread programs/server/play.html Resolved
The previous commit reconciled `opened_databases` with the row's actual
state once the load settled, dropping any database whose open did not
expand the row. But that also dropped a database that had been remembered
from an earlier successful open: on panel reopen the restore fires
`openDatabase(false)`, and if its `system.tables` request fails once
(network blip or transient server exception) the row never expands, so
the reconciliation deleted the entry and the next reopen showed the
database collapsed even though the user never collapsed it. That breaks
the feature's contract that restored databases survive.

Capture whether the database was already remembered before this open
began and only roll back a brand-new open on failure; a restore (or a
re-open) of an already-remembered database is kept, so a transient
restore failure no longer forgets a previously expanded database. A
still-failing manual open of a not-yet-remembered database is still
rolled back to avoid auto-retrying it on every panel reopen, and a panel
close during the load still leaves a just-performed open in the set.

`node --check` on the extracted inline script passes.
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Addressed the AI review's Request changes finding (programs/server/play.html:3328) in 689aa6c.

Bug: the previous commit reconciled opened_databases with the row's actual state once each open settled, deleting any database whose open did not expand the row while it was still attached. That also deleted a database that had been remembered from an earlier successful open: on panel reopen the restore fires openDatabase(false), and if its system.tables request failed once (a transient network error or server exception) the row never expanded, so the reconciliation forgot the entry and the next reopen showed the database collapsed — even though the user never collapsed it. That breaks the PR's contract that restored databases survive.

Fix: openDatabase captures whether the database was already remembered before the open began, and on a failed open rolls it back only when it was not already remembered. A brand-new manual open that never expands is still dropped (so it is not auto-retried on every panel reopen), a restore/re-open of a previously expanded database survives a transient failure, and a panel close during the load still leaves a just-performed open in the set (reconciliation is skipped once the row is detached).

node --check on the extracted inline script passes. Master has not touched the database-panel code since the merge base and the branch is mergeable, so no re-merge was needed; fresh CI is running on 689aa6c.

Comment thread programs/server/play.html Resolved
alexey-milovidov and others added 2 commits July 2, 2026 11:38
…sult pane

Restoring the previously expanded databases on panel reopen fires background
`system.tables` requests via `openDatabase(false)`, but those still went through
`auxiliaryQuery`, which unconditionally calls `clear()`, `resultEl.renderError`,
`resultEl.clearError()` and toggles `logoEl`. So merely reopening the panel could
overwrite the main result pane: a transient restore failure wiped the current
query result and replaced it with the auxiliary `system.tables` error, and a
successful restore cleared an error left on screen by the user's own query.

Add a `quiet` mode to `auxiliaryQuery` that suppresses those global result/error
side effects, and use it for restore loads (`loadTables` with `set_current =
false`). Explicit panel actions (a manual open or refresh) keep reporting their
errors in the result pane as before.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…ed-databases

# Conflicts:
#	programs/server/play.html
@alexey-milovidov

Copy link
Copy Markdown
Member Author

continue-pr: pushed the `quiet`-mode fix (`8e0d7435a15`, includes a clean master re-merge that resolved a fresh conflict with the new `/docs` icon block) for the AI review's Request-changes finding at `play.html:3358`.

Unlike the prior findings on this PR (all in the opened_databases / connection / panel-lifecycle family), this one is in a distinct subsystem: background database-restore loads (openDatabase(false) → loadTables → auxiliaryQuery) were mutating the main result pane, so merely reopening the panel could wipe the current query result on a transient restore failure or clear an unrelated error on success. auxiliaryQuery now takes a quiet flag that suppresses its clear() / renderError / clearError / logoEl side effects, and loadTables passes it for restore loads (set_current = false); explicit opens/refreshes are unchanged. Thread resolved; syntax verified with node --check.

This is a genuine PR-introduced regression, so it was worth fixing, but the PR is already human-approved (@pamarcos) and has now iterated through 8 automated-review rounds. The panel-lifecycle logic is heavily guarded at this point — worth considering a merge rather than continuing the review loop, unless a future finding is a clear new correctness bug.

Comment thread programs/server/play.html Resolved
… generation

The per-connection staleness guard (`connectionGeneration`) only reached
`loadDatabases` and `loadTables` after their `await auxiliaryQuery`
returned, but `auxiliaryQuery` itself runs `clear` / `renderError` /
`clearError` (and toggles the logo) on a non-quiet request before the
caller can drop a stale result. So a `system.databases` load, or an
explicit `system.tables` load started against one server, could - after
the user edits `url` / `user` / `password` to a different server -
complete and overwrite the user's current result pane (or clear a current
error) with a response from the previous connection.

`auxiliaryQuery` now takes the connection `generation` captured by the
caller before the request started and skips its result-pane side effects
once that generation no longer matches the active `connectionGeneration`.
`loadDatabases` passes the generation it already captured; `loadTables`
now captures it before awaiting. Restore (quiet) loads are unaffected
because they already suppress these side effects, and the common path
(no connection change) is unchanged because the generation still matches.

Addresses the AI review's `Request changes` finding at
`programs/server/play.html:3207-3224`.

`node --check` on the extracted inline script passes.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

continue-pr: pushed f6896971012 addressing the AI review's Request changes finding at programs/server/play.html:3207-3224.

Gap: the connection-generation guard (connectionGeneration) reached loadDatabases / loadTables only after their await auxiliaryQuery returned, but auxiliaryQuery runs clear / renderError / clearError (and toggles the logo) on a non-quiet request before the caller can drop a stale result. So a system.databases load, or an explicit (non-quiet) system.tables load, started against server A could — after the user edits url / user / password to server B while it is in flight — complete and either overwrite the current result pane (on failure) or clear a current error (on success) with a response from the previous connection.

Fix: auxiliaryQuery now takes the generation captured by the caller before the request started and skips its result-pane side effects once that generation no longer matches the active connectionGeneration. The check is re-evaluated after the await (via a small closure), since the connection can change mid-flight. loadDatabases passes the generation it already captured; loadTables captures it before awaiting. Restore (quiet) loads are unaffected (they already suppress these side effects) and the common no-connection-change path is unchanged (generation still matches). node --check on the extracted inline script passes.

CI: the only red on the previous commit was Stress test (amd_debug) → Logical error: Block structure mismatch in A stream (STID 0993-386d) — a master-wide flake tracked by #108142. Over the last 30 days it hit 208 distinct PRs and fired 23 times on master itself, so it is impossible for a play.html-only change to cause a C++ JoinStep block-structure mismatch. Related: #108142

Fresh CI is running on f6896971012. With this the PR is human-approved (@pamarcos) with no outstanding review findings — merge-ready once CI is green.

Comment thread programs/server/play.html
/// just-performed open in the set so it is still restored on reopen.
if (database.isConnected) {
if (database.dataset['opened']) opened_databases.add(elem.database);
else if (!was_remembered) opened_databases.delete(elem.database);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The was_remembered exemption fixes transient restore failures, but it also makes a previously remembered database impossible to forget once its system.tables load starts failing persistently.

Concrete trace:

  1. Expand db, so it is stored in opened_databases.
  2. Close the panel.
  3. Make loadTables(db) fail on every reopen (for example a remote-database exception or a privilege change that still leaves db visible in system.databases).
  4. Reopen the panel: the restore keeps db remembered because was_remembered == true, but the row never reaches dataset['opened'], so the collapse path at 3411-3413 is unavailable.
  5. Click the still-collapsed row manually: this branch runs again with was_remembered == true, so another failure still does not clear the set.

At that point every future reopen silently reissues the same failing system.tables query and there is no panel action that clears the remembered state. Can we preserve the entry only for background restore (set_current = false), but drop it on a failed explicit click, or otherwise add an explicit way to forget a row that never actually re-opened?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accurate trace — but I'm going to leave this as-is deliberately, because the fix trades one edge case for another and the residual harm here is minimal.

The two directions are in tension, and the current was_remembered rule is the fixed point that satisfies both earlier findings:

  • Finding fix docs #6 (89ea47e65c2): a brand-new open that fails must be dropped, so it isn't auto-retried.
  • Finding Error fetching data from materialized view #7 (689aa6c75d1): a previously expanded database whose restore hits a transient failure must not be forgotten, or one blip would silently break the PR's contract ("restored databases survive").

else if (!was_remembered) is the single rule that satisfies both. Your suggestion — "drop on a failed explicit click, preserve only on background restore" — is implementable as a one-liner (else if (set_current) instead of else if (!was_remembered), since a restore is the only set_current = false open and only fires for already-remembered databases). It would satisfy #6, #7 and this finding.

I'm not taking it, for two reasons:

  1. The residual harm is minimal. A background restore runs quietly (8e0d7435a15: loadTables passes quiet = !set_current), so a persistently-failing restore issues one wasted system.tables query per panel reopen with no visible error and no result-pane disruption. The only path that surfaces the error is an explicit click — which is user-initiated, so surfacing it is correct — and the entry is fully cleared on any connection change (resetConnectionScopedState). The scenario itself is narrow: a database that stays visible in system.databases while system.tables throws persistently (a privilege change or a broken remote database).

  2. set_current-based dropping reopens finding Error fetching data from materialized view #7 from a different angle. A remembered database on a flaky connection would be forgotten the first time the user clicks it during an outage, even though they never collapsed it — which is exactly the regression Error fetching data from materialized view #7 fixed.

Given the PR is already approved and this is the tenth iteration of the same open/panel-lifecycle staleness family, I'd rather ship the current fixed point than keep re-tuning a quiet, rare edge. Leaving the thread open for a maintainer to overrule if they prefer the set_current variant.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

continue-pr: the AI review's tenth Request changes finding (programs/server/play.html:3399, the was_remembered restore exemption) has been declined deliberately rather than fixed — see the inline reply.

Rationale in brief:

  • It re-opens the finding fix docs #6 ↔ Error fetching data from materialized view #7 tension (drop a failed open vs. keep a remembered database through a transient failure). The current was_remembered rule is the fixed point that satisfies both; the suggested set_current-based variant would satisfy this finding but reopen Error fetching data from materialized view #7 from another angle.
  • The residual harm is minimal: a background restore runs quietly (8e0d7435a15), so a persistently-failing remembered database issues one wasted system.tables query per reopen with no visible error, and the set is cleared on any connection change.
  • The scenario is narrow (a database visible in system.databases whose system.tables throws persistently).

The nine correctness findings before this one (highlight race, connection-generation staleness across openDatabase/loadDatabases/loadTables/auxiliaryQuery, the quiet-restore result-pane fix, and the open-state reconciliation) are all fixed. The PR was approved by @pamarcos on 2c0e72f8171; the approval is stale only because of the intervening fixes. f6896971012 is MERGEABLE, CI is green so far (0 failures on a fresh run, still finishing).

This is the tenth round on the same open/panel-lifecycle staleness family; the marginal value of further re-tuning is near zero. I'd suggest a re-approval and merge over the AI loop, unless a maintainer prefers the set_current variant discussed in the thread.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 2, 2026
Merged via the queue into master with commit 1039465 Jul 2, 2026
174 checks passed
@alexey-milovidov
alexey-milovidov deleted the play-restore-expanded-databases branch July 2, 2026 20:28
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 2, 2026
alexey-milovidov added a commit that referenced this pull request Jul 3, 2026
…ntax-error-position

Resolve conflicts in `programs/server/play.html` between this PR's
syntax-error position highlight and the Web UI features that merged into
`master` in the meantime (query tabs `#107826`, expanded-database restore
`#108964`, and rainbow-bracket / matched-identifier highlighting `#109108`).

- `postSingle` now takes both `params_restore_pending` (tabs feature) and
  `queryStart` (this PR); all call sites use the newer `my_request_num`
  ownership handling and pass both arguments.
- The token render loop keeps `master`'s rainbow-bracket, bold matched-bracket,
  matched-identifier and per-digit-group number rendering, and additionally
  OR-s the `q-err-pos` class onto the token at the reported error position.

The net diff of the branch over `master` is exactly this PR's
syntax-error-highlight feature; no tabs / restore / bracket functionality
was dropped.
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Address review (ClickHouse#108964,
commit 44b7e36): the `opened_databases` set that remembers which
databases the user expanded was page-global, so it survived changes to
`url` / `user` / `password`. Reopening the panel on a different server
or as a different user would then auto-expand same-named databases and
issue unintended `system.tables` queries there.

`system.tables` (like `system.completions`) is connection-scoped, so
clear `opened_databases` from the same input handlers that already
invalidate the cached completions on a connection change. Renamed the
handler `resetCompletions` -> `resetConnectionScopedState` to reflect
that it now resets both pieces of connection-scoped state.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108964&sha=44b7e368c817e3a3873ae053193d99f8e6d6ddda&name_0=PR

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…anded-databases

Web UI: restore expanded databases when reopening the database panel
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ntax-error-position

Resolve conflicts in `programs/server/play.html` between this PR's
syntax-error position highlight and the Web UI features that merged into
`master` in the meantime (query tabs `ClickHouse#107826`, expanded-database restore
`ClickHouse#108964`, and rainbow-bracket / matched-identifier highlighting `ClickHouse#109108`).

- `postSingle` now takes both `params_restore_pending` (tabs feature) and
  `queryStart` (this PR); all call sites use the newer `my_request_num`
  ownership handling and pass both arguments.
- The token render loop keeps `master`'s rainbow-bracket, bold matched-bracket,
  matched-identifier and per-digit-group number rendering, and additionally
  OR-s the `q-err-pos` class onto the token at the reported error position.

The net diff of the branch over `master` is exactly this PR's
syntax-error-highlight feature; no tabs / restore / bracket functionality
was dropped.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants