Skip to content

Finish API generation in serve when the client gives up - #4932

Merged
reakaleek merged 5 commits into
mainfrom
fix-serve-api-docs-restar-72b3f607
Nov 18, 2026
Merged

reakaleek merged 5 commits into
mainfrom
fix-serve-api-docs-restar-72b3f607

Conversation

@reakaleek

@reakaleek reakaleek commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

docs-builder serve now finishes generating the API pages even when the client that asked for /api/ times out or disconnects. A polling script no longer restarts the build on every attempt.

Affects: CLI, API reference

Prompt summary: Navigating to /api in serve seems to restart the API docs generation from scratch whenever a build is still in progress. A polling script therefore runs forever. The goal is to make a request join the generation in flight.

Why

A request to /api/ ran the generation on a token linked to its own HTTP request. When the client gave up, the build was cancelled, the pages were marked stale, and the next request started a full build again. A poller with a timeout shorter than the build time never sees it finish.

What

One generation owned by the server

A new SharedGeneration class runs one generation at a time on a server-lifetime token. A request waits on the run with WaitAsync and its own token, so a disconnect ends only that request's wait. Concurrent requests join the same run. The 5-minute CancelAfter and the semaphore are gone. The timeout caused the same restart loop for a slow build without any poller.

A request never gets output older than a change it saw

A request leaves only when no run is in flight and the pages are fresh, and it decides that under a lock. A spec, overlay or hot-reload change that lands mid-run therefore costs the request one more run, as the semaphore version did. A request that wakes after another request started that run joins it and does not return early.

Failures retry

ReloadableGeneratorState marks the pages stale when any step of a run fails, including the check for changed specs. SharedGeneration also starts a new run after a faulted one, so a failure surfaces once and the next request goes ahead.

Verify

dotnet test --project tests/Elastic.Documentation.Build.Tests --treenode-filter "/*/*/SharedGenerationTests/*"
dotnet build src/tooling/docs-builder/docs-builder.csproj
dotnet .artifacts/bin/docs-builder/debug/docs-builder.dll serve --path docs --port 3987
# in another terminal; each request gives up after 2 seconds
until curl -sf --max-time 2 localhost:3987/api/ -o /dev/null; do sleep 1; done

On main the loop never exits. With this change it exits once the one build finishes, about two minutes on this machine for the docs/ specs. A second request then returns in about a millisecond. After touch docs/api/docs-builder-elasticsearch/op-async-search-get.md, the next request regenerates once, and several concurrent requests share that one build.

Out of scope: the hot-reload path (InvalidateApiReferences from dotnet watch) was not exercised end to end. SharedGenerationTests covers the same stale-again behavior with a controllable generation.

🤖 Generated with Claude Code

A request to /api/ ran the generation on its own cancellation token. A client that timed out cancelled the build, marked the pages stale, and the next request started it over. A poller with a short timeout never saw a finished build. The generation is now one task that the server owns. Requests wait on it with their own token, so a disconnect stops only that wait.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@reakaleek
reakaleek requested a review from a team as a code owner November 18, 2026 08:56
@reakaleek reakaleek added the bug label Nov 18, 2026
@reakaleek
reakaleek requested a review from theletterf November 18, 2026 08:56
reakaleek and others added 3 commits November 18, 2026 09:58
The stale flag is cleared before the check for changed specs, which sat outside the try block. An IO error there left the pages marked fresh and the faulted task in place, so every later /api/ request rethrew the same error. The try block now covers the whole body, and a failure marks the pages stale again.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
A request that arrived during a generation joined it, even when a spec or overlay edit had marked the pages stale since the run began. It received pages built from the old inputs and had to reload to see its edit. The request now waits for another run while the pages are stale.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The lifecycle that keeps a build running when its client gives up was tangled into ReloadableGeneratorState, so only a full API build could exercise it. SharedGeneration now holds that coordination and takes the build as a delegate. Tests cover a cancelled request, concurrent requests, a change that lands mid-run, a failed run and shutdown. A failed run is also retried by the next request without relying on the build to mark the output stale.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

A caller checked the stale flag after its run ended, outside the lock. When another caller had already started the next run, that run had cleared the flag, so the first caller returned while pages were still being rewritten in place. The caller now decides under the lock: it leaves only when no run is in flight and the output is fresh.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@reakaleek
reakaleek merged commit 99d87db into main Nov 18, 2026
44 checks passed
@reakaleek
reakaleek deleted the fix-serve-api-docs-restar-72b3f607 branch November 18, 2026 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant