Skip to content

fix: web: browse mode no longer exits while its page is in a background tab - #2753

Open
acinader wants to merge 1 commit into
hledgerorg:mainfrom
acinader:web-idle-timeout
Open

acinader wants to merge 1 commit into
hledgerorg:mainfrom
acinader:web-idle-timeout

Conversation

@acinader

@acinader acinader commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

In the default browse mode, hledger-web exits once no page has pinged it for 2 minutes. Pages ask to ping every 30 seconds, but browsers run the timers of background tabs much less often, so hledger-web regularly exited while its page sat open in a background Safari tab. Coming back, the page looked fine but its links and forms no longer worked.

I measured it on macOS with a small probe page that reported each timer tick, left hidden for about 11 hours:

  • Safari ran a hidden tab's timer every 2 to 5 minutes, settling at about every 4 minutes 20 seconds; in an earlier run it went 8 to 11 minutes between ticks. Any gap over 2 minutes ended the server.
  • Chrome ran it once a minute, inside the 2 minute limit but with little margin.

This PR:

  • raises the timeout to 15 minutes, which outlasts the throttling seen in both browsers;
  • pings as soon as a page is shown again, so returning to a tab counts right away;
  • shows a notice when a ping can't reach the server ("hledger-web is no longer running... To continue, start it again and reload this page."), instead of the page failing on the next click; a later ping that gets through hides it again.
notice

The startup and exit messages, --help, and the manual say 15m. The notice is in the page template, browse mode only.

One case this doesn't cover: after a few hours, Chrome may discard a background tab (it did in my overnight run, document.wasDiscarded was true). Going back reloads the page from the server, which by then has exited. Only a much longer timeout would help with that; 15 minutes seemed a reasonable default, and --serve remains for leaving it running.

The notice uses bootstrap's .alert-warning, which the dark mode in #2752 doesn't restyle yet. Whichever of the two lands second, I'll rebase it and give the notice its dark colors.

Tests: a browser spec case that blocks /_ping, checks the notice appears, then unblocks and shows the page again and checks it goes away. It fails if either the notice or the ping on showing is removed. All 34 browser tests and the yesod suite pass.

How to test

stack build hledger-web
stack exec -- hledger-web -f hledger-web/test/browser/fixture.journal --port 5021

The terminal says "This server will exit after 15m with no browser windows open". Press ctrl-c, then switch away from the hledger-web tab and back: the yellow notice appears. Start it again with the same command and switch back to the first tab: the notice goes away. Leave the page in a background Safari tab for 20 minutes: the server keeps running.

AI usage: Claude Opus 5.5: ~90k output tokens

…nd tab

In the default --serve-browse mode, hledger-web exits once no page has
pinged it for a while. Each page sets a timer to ping every 30 seconds,
but browsers run the timers of pages in background tabs much less often.
Measured on macOS, a hidden Safari tab ran its timer only every 2 to 5
minutes (in one run, every 8 to 11), and a hidden Chrome tab ran it once
a minute. So with the 2 minute timeout, hledger-web regularly exited
while its page sat open in a Safari tab, and the user came back to a
page whose links and forms no longer worked.

- The server now exits after 15 minutes without a ping, not 2.

- The page also pings as soon as it is shown again, so returning to a
  tab keeps the server running without waiting for the next timed ping.

- When a ping can't reach the server, the page shows a notice saying
  hledger-web is no longer running and how to continue, instead of
  failing silently on the next click. A later ping that gets through
  (eg after a restart on the same address) hides it again.

AI usage: Claude Opus 5.5, ~64k output tokens.
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.

1 participant