Repository navigation
fix(worker): calls for a graph that is opening wait for the open - #13864
yuxi-liu-wired wants to merge 5 commits into
Conversation
| (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))) |
There was a problem hiding this comment.
🟡 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.
Was this helpful? React with 👍 or 👎 to provide feedback.
| (p/let [_ (p/race [(get @*opening-graphs repo) (p/delay graph-open-wait-ms)])] | ||
| (apply f args)) |
There was a problem hiding this comment.
🟡 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.
Was this helpful? React with 👍 or 👎 to provide feedback.
| (and (string? repo) (get @*opening-graphs repo)) | ||
| (p/let [_ (p/race [(get @*opening-graphs repo) (p/delay graph-open-wait-ms)])] | ||
| (apply f args)) |
There was a problem hiding this comment.
🔍 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.
Was this helpful? React with 👍 or 👎 to provide feedback.
|
Addressed in the latest commit:
|
68ff17a to
273493a
Compare
18cf604 to
0b444d7
Compare
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.
0b444d7 to
2e15cd4
Compare
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, andTypeError: 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-blocksreturned nil, so the sidebar's Contents page fails to load);get-view-data,favorited-page?andget-all-page-titles.Guarding each handler can't give the right answer: a nil from
build-graphis 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-dbcall 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.frontend.worker.search/search-blocksreturns nothing without a conn or search db;get-view-data,favorited-page?andget-all-page-titlesreturn nil (false) without a conn, like the read handlers next to them.build-graphandget-blockskeep 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.cljssearch-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.