Skip to content

fix(fpc): graceful StopListen in the HTTPApplication provider - #635

Draft
edsontmarques wants to merge 1 commit into
HashLoad:masterfrom
edsontmarques:fix/fpc-httpapplication-graceful-stop
Draft

edsontmarques wants to merge 1 commit into
HashLoad:masterfrom
edsontmarques:fix/fpc-httpapplication-graceful-stop

Conversation

@edsontmarques

Copy link
Copy Markdown

Problem and change

The FPC HTTPApplication provider (the default provider for FPC console applications) does not override StopListen. It inherits the abstract one, which raises StopListen not implemented, although doc/deployment.md stops this shape with THorse.StopListen. Listen therefore never returns, and a service cannot shut down in order.

This patch gives the provider StopListen and StopListenGraceful, following the order documented in doc/graceful-shutdown.md:

  1. mark the shutdown (SetIsShuttingDown);
  2. stop accepting;
  3. switch keep-alive off, so idle keep-alive connections end at once instead of waiting out their 15 s window;
  4. drain the requests in flight, up to the timeout;
  5. release Listen, which returns only after the drain.

StopListen uses a 5 s default; StopListenGraceful(ATimeoutMS) takes the limit explicitly. A second call is a no-op.

A stop requested before Run starts accepting, for instance inside the Listen callback, cannot be delivered through Terminate, because there is no accept loop to break yet. The accept loop now runs with a 100 ms idle timeout, and OnAcceptIdle stops accepting once a stop was requested, so that early stop is still honoured and Listen returns without serving.

TCustomWebApplication.Terminate forwards to the handler only the first time it is called, so the embedded server is switched off directly as well (FPC 3.3.1 and later, behind a version guard). The waits use atomic flags rather than TEvent: an event created at unit initialization raises ESyncObjectException in FPC programs built without cthreads.

Horse has been useful in our projects, and this is a focused fix we would like to contribute back, in the same spirit as #632. The docs list this provider among the ones that drain (English and Portuguese).

Validation performed

All runs target Linux x86_64, with FPC 3.3.1-22012-g6564bb9b34-unpushed.

Check Result
New tests/fpc-graceful-shutdown, drain mode slow request in flight answers 200; idle keep-alive connection sees EOF in under 1 s; StopListenGraceful returns before Listen
New tests/fpc-graceful-shutdown, in-onlisten mode Listen returns without serving
Same check against the sources at 41c4b1c (this PR's base) Fails: drain hangs until the script's timeout (exit 124); in-onlisten raises StopListen not implemented
Existing regressions in tests.yml (exception propagation, WebSocket EAGAIN, router lifetime, URL decoding, header hash, stream factory, provider config) Pass

Reproduce from the repository root:

sh tests/fpc-graceful-shutdown/run-graceful-shutdown-test.sh

The script is registered in .github/workflows/tests.yml, after the router lifetime regression. HORSE_GRACEFUL_PORT overrides the port (default 9911).

Validation still needed

  • FPC 3.2.2: the keep-alive and embedded-server handling sit behind the existing 3.3.1 guard (on 3.2.2 the provider does not enable keep-alive), so 3.2.2 relies on Terminate plus the idle check. I could not run it here.
  • Windows: not run.
  • Delphi: not affected, since the provider is FPC-only.

🤖 Generated with Claude Code

The FPC HTTPApplication provider inherited the abstract StopListen,
which raises "StopListen not implemented", so Listen never returned.
StopListen and StopListenGraceful now follow the documented order:
mark the shutdown, stop accepting, turn keep-alive off, drain the
requests in flight and signal the end. Listen returns only after the
drain. A 100 ms accept-idle closes the race between OnListen and Run.
Waits use atomic flags: a TEvent created at unit initialization
breaks FPC programs built without cthreads.

The new tests/fpc-graceful-shutdown check fails against the previous
version (drain times out, in-onlisten exits 217) and runs in CI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants