Skip to content

fix(worker): calls for a graph that is opening wait for the open - #13864

Open
yuxi-liu-wired wants to merge 5 commits into
logseq:masterfrom
yuxi-liu-wired:fix/graph-requests-before-open
Open

yuxi-liu-wired wants to merge 5 commits into
logseq:masterfrom
yuxi-liu-wired:fix/graph-requests-before-open

Conversation

@yuxi-liu-wired

@yuxi-liu-wired yuxi-liu-wired commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Calls for a graph that is opening wait for the open

After a reload the renderer sends calls for the current graph right behind :thread-api/create-or-open-db: block fetches for the page and sidebar it renders, search, view data, the graph view. The worker ran them on a graph that had no conn yet, and what happened depended on whether each handler guarded the missing conn itself. A local key fuzzer that reloads mid-session hit:

  • DB worker API failed: :thread-api/search-blocks Error: No protocol method IDeref.-deref defined for type null, and TypeError: Cannot read properties of null (reading 'exec') from the search db;
  • DB worker API failed: :thread-api/build-graph Error: No protocol method IDeref.-deref defined for type null (the global graph view shows an error instead of the graph);
  • :db/block-fetch-failed {:operation :get-block, :block "Contents", :error "Unexpected get-blocks response count"} (get-blocks returned nil, so the sidebar's Contents page fails to load);
  • the same nil-conn deref in get-view-data, favorited-page? and get-all-page-titles.

Guarding each handler can't give the right answer: a nil from build-graph is stored as a finished, empty graph and leaves the view on "Preparing", an error shows a spurious failure, and every new handler can forget the guard. The calls just came too early.

Change

  • frontend.common.thread-api/remote-function, which runs every thread api, marks a repo as opening while its :thread-api/create-or-open-db call is in flight. A call whose first argument is that repo waits for the open (at most 30 s; one timer per open, cleared when the open settles) before it runs, so it sees the opened graph and returns its real result. Calls for other graphs and calls after the open are unchanged, and a failed open releases its waiters.
  • As the backstop for calls that reach the worker when no open is in flight: frontend.worker.search/search-blocks returns nothing without a conn or search db; get-view-data, favorited-page? and get-all-page-titles return nil (false) without a conn, like the read handlers next to them. build-graph and get-blocks keep their master behaviour there (an error the caller shows, rather than an empty result that would hide a closed graph).

Tests

  • src/test/frontend/common/thread_api_test.cljs: a read sent right behind a slow open sees the opened graph (it ran first on master); a read for another graph does not wait, nor does one after the open; a failed open still lets the read run.
  • src/test/frontend/worker/search_test.cljs search-blocks-before-the-graph-is-open: no throw and no logged error without a conn or search db.
  • src/test/frontend/worker/handler/graph_not_open_test.cljs: view, favourite and page-title reads return nil/false.

Each fails on master and passes with the change; CI's unit suite and lint pass locally.

This replaces #13857 (search) and #13862 (read handlers), whose commits are included here.

@devin-ai-integration devin-ai-integration Bot left a comment

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.

Devin Review found 3 potential issues.

Devin Review

Comment on lines +704 to +709
(if (worker-state/get-datascript-conn repo)
(get-blocks-response repo requests)
;; Before the worker has opened the graph (e.g. right after a reload),
;; answer every request as a missing block: the renderer expects one
;; result per request.
(mapv (fn [{:keys [id]}] {:id id}) requests)))

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.

🟡 Closed graphs silently lose block results

When a block fetch targets a closed graph, get-blocks reports every block missing instead of exposing the absent connection. The caller treats the fetch as successful and displays missing content.

Learn more

A block fetch normally returns one response per request, and an absent block has an empty :block. The new fallback returns the same shape when there is no graph connection at all. resolve-batched-get-block-groups! accepts this response, and <get-block returns nil without an error. This hides an invalid worker state after closing a graph, rather than making the caller handle it.

Example: A caller fetches the Contents block for a graph whose worker connection has been closed. It receives {:id "Contents"} and renders no block, instead of receiving an error that the graph is unavailable.

Recommended fix: Keep the one-result-per-request contract for genuinely absent blocks in an open graph. When worker-state/get-datascript-conn is nil and no open is pending, propagate a missing-connection error rather than returning fabricated missing-block results; coordinate this with the opening gate if pre-open calls need a distinct outcome.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +74 to +75
(p/let [_ (p/race [(get @*opening-graphs repo) (p/delay graph-open-wait-ms)])]
(apply f args))

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.

🟡 Completed opens leave thirty-second timers

Each call waiting for an open starts a p/delay that survives when the open wins p/race. Normal reloads retain one idle timer per waiting call for thirty seconds.

Learn more

The gate races the open promise against a separate thirty-second delay for each request. A promise race selects the first result but does not cancel the losing timer. After a fast open, the calls return promptly while their delays remain scheduled, retaining their timers and promise callbacks until the deadline.

Example: If 50 requests queue during a graph open that completes in one second, all 50 requests finish, but 50 timer callbacks still run roughly 29 seconds later.

Recommended fix: Use a shared, cancellable timeout for the graph's opening operation or clear each request's timeout when the open settles. Preserve the existing bound when an open does not settle.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +73 to +75
(and (string? repo) (get @*opening-graphs repo))
(p/let [_ (p/race [(get @*opening-graphs repo) (p/delay graph-open-wait-ms)])]
(apply f args))

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.

🔍 Slow opens release calls before readiness

After thirty seconds, call-with-graph-open-gate runs waiting calls even if the graph remains unopened. The gate also covers mutations; review whether those calls need an explicit timeout error rather than execution against an incomplete graph.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

@yuxi-liu-wired

Copy link
Copy Markdown
Contributor Author

Addressed in the latest commit:

  • get-blocks on a closed graph: agreed. With the open gate, reload-time block fetches wait for the open and get real blocks, so the fallback isn't needed; get-blocks is back to its master behaviour (no fabricated missing blocks).
  • Timers: the open now gets one release promise (the open settling, or 30 s) whose timer is cleared when the open settles; waiting calls share it instead of each racing its own delay.
  • Calls released after 30 s run exactly as they do on master today (the gate only adds a bounded wait); a mutation sent then fails the same way it does without this PR.

@yuxi-liu-wired
yuxi-liu-wired force-pushed the fix/graph-requests-before-open branch 3 times, most recently from 68ff17a to 273493a Compare November 3, 2026 12:01
@yuxi-liu-wired
yuxi-liu-wired force-pushed the fix/graph-requests-before-open branch 7 times, most recently from 18cf604 to 0b444d7 Compare November 18, 2026 14:38
A search sent while the graph is still opening (the search dialog right
after a reload) reached frontend.worker.search/search-blocks with a nil
datascript conn or a nil search db. A nil conn threw "No protocol method
IDeref.-deref defined for type null" (shown as "DB worker API failed:
:thread-api/search-blocks"); a nil search db made the sqlite query log
"TypeError: Cannot read properties of null (reading 'exec')". A local
key fuzzer that reloads mid-session hit both.

search-blocks returns nothing until both exist.

search-blocks-before-the-graph-is-open searches with no search db and
with no conn, and asserts an empty result and no logged error; without
the change the nil conn throws and the nil search db logs the TypeError.
Right after a reload the renderer sends graph-scoped read thread-apis
before the worker has opened the graph (worker-state/get-datascript-conn
is still nil). A local key fuzzer that reloads mid-session hit:

- :thread-api/get-blocks returned nil, so frontend.db.async logged
  ":db/block-fetch-failed ... Unexpected get-blocks response count" (the
  right sidebar's Contents page);
- :thread-api/build-graph threw "No protocol method IDeref.-deref
  defined for type null"; get-view-data, favorited-page? and
  get-all-page-titles deref the conn the same way.

get-blocks now answers each request with a missing block ({:id id}),
which the renderer already treats as not found; the others use the
same when-let on the conn as the read handlers next to them and return
nil (false for favorited-page?). Nothing changes once the conn exists.

graph-not-open-test calls these thread-apis for a repo with no conn;
the get-blocks case fails without the change.
A nil from :thread-api/build-graph is stored by load-global-graph! as a
finished graph, which leaves the global graph view on "Preparing" with
no error or retry. Before the graph is open the thrown error is the
better answer (the view shows it with a retry), so build-graph is back
to master's behaviour.
After a reload the renderer sends calls for the current graph (block
fetches for the page and sidebar it renders, search, view and graph
data) right behind :thread-api/create-or-open-db, and the worker ran
them on a graph that had no conn yet: each handler then threw "No
protocol method IDeref.-deref defined for type null", returned nil, or
logged a fetch failure, depending on whether it guarded the missing
conn itself.

remote-function, which runs every thread api, now marks a repo as
opening while its create-or-open-db call is in flight, and a call whose
first argument is that repo waits for the open (at most 30 s) before it
runs. Calls for other graphs and calls after the open are unchanged; a
failed open releases its waiters. The handler-level guards from the
previous commits stay as the backstop for calls that reach the worker
when no open is in flight.

thread-api-test: a read sent right behind a slow open sees the opened
graph (it ran first without the change); a read for another graph does
not wait, nor does one after the open; a failed open still lets the
read run.
The open gate raced each waiting call against its own 30 s delay, so a
fast open left one live timer per waiting call. The open now gets a
single release promise (the open settling, or 30 s), whose timer is
cleared when the open settles; waiting calls share it.

get-blocks without a conn answered every request as a missing block,
which also hides a closed graph behind empty results. With the gate,
reload-time block fetches wait for the open and get real blocks, so
get-blocks is back to its master behaviour.
@yuxi-liu-wired
yuxi-liu-wired force-pushed the fix/graph-requests-before-open branch from 0b444d7 to 2e15cd4 Compare November 19, 2026 12:01

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant