You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Opt-in idle exit: stop the proxy after the last app exits
#562
I'd like to propose an opt-in idle exit: once the last app that uses the proxy exits, the proxy waits a few seconds and stops, and the next portless run starts it again. With the option off, nothing changes.
Why
portless run auto-starts the proxy as a background daemon. The daemon keeps running after every app has exited, until portless proxy stop or a reboot, and it keeps holding its port the whole time. Two cases where that shows up:
A proxy started from another checkout, or by a tool that has since finished, still owns port 80 or 443. The next thing that wants the port finds it taken, often with no portless app running.
Agents and scripted runs start apps without a terminal and rarely run portless proxy stop afterwards, so each session can leave a proxy behind with nothing to serve. This gets more likely now that --unprivileged lets an agent start the proxy on port 80 without sudo.
Proposal
--idle-exit on proxy start, with PORTLESS_IDLE_EXIT=1 for auto-start, which takes no proxy flags. It is remembered with the other proxy settings, so every auto-started proxy keeps it.
The proxy stops 5 seconds after the last app exits. --idle-exit-after <seconds> / PORTLESS_IDLE_EXIT_AFTER changes the wait. Five seconds is long enough that restarting a dev server in place reuses the running proxy.
Until the first app connects, the proxy stays up for at least 30 seconds, so an auto-start always has time to register the app it was started for.
Static aliases keep the proxy up, since no process stands behind them.
service install refuses the option, because the service manager would restart the proxy as soon as it stopped.
How the proxy knows the last app is gone
Each portless run holds one open connection to the proxy, a lease, for as long as it runs. The operating system closes that connection however the run ends: Ctrl+C, SIGTERM, a closed terminal, SIGKILL, or a crash. The proxy starts the countdown when its last lease closes, and a new lease cancels it. Nothing polls.
routes.json stays the record of routes, and its PID checks are unchanged. The proxy reads it once when a countdown ends, so a server that put itself in the background (#444) keeps the proxy up after its launcher has exited.
The lease endpoint accepts loopback peers and loopback Host values only. It requires an x-portless-lease: 1 header and refuses requests that carry Origin or Sec-Fetch-Site, so a page open in a browser can't hold the proxy open. Leases are capped at 32.
I have an implementation with tests ready and will open it as a PR that links here. Happy to change the interface or the defaults based on feedback.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
I'd like to propose an opt-in idle exit: once the last app that uses the proxy exits, the proxy waits a few seconds and stops, and the next
portless runstarts it again. With the option off, nothing changes.Why
portless runauto-starts the proxy as a background daemon. The daemon keeps running after every app has exited, untilportless proxy stopor a reboot, and it keeps holding its port the whole time. Two cases where that shows up:portless proxy stopafterwards, so each session can leave a proxy behind with nothing to serve. This gets more likely now that--unprivilegedlets an agent start the proxy on port 80 without sudo.Proposal
--idle-exitonproxy start, withPORTLESS_IDLE_EXIT=1for auto-start, which takes no proxy flags. It is remembered with the other proxy settings, so every auto-started proxy keeps it.--idle-exit-after <seconds>/PORTLESS_IDLE_EXIT_AFTERchanges the wait. Five seconds is long enough that restarting a dev server in place reuses the running proxy.service installrefuses the option, because the service manager would restart the proxy as soon as it stopped.How the proxy knows the last app is gone
Each
portless runholds one open connection to the proxy, a lease, for as long as it runs. The operating system closes that connection however the run ends: Ctrl+C, SIGTERM, a closed terminal, SIGKILL, or a crash. The proxy starts the countdown when its last lease closes, and a new lease cancels it. Nothing polls.routes.jsonstays the record of routes, and its PID checks are unchanged. The proxy reads it once when a countdown ends, so a server that put itself in the background (#444) keeps the proxy up after its launcher has exited.The lease endpoint accepts loopback peers and loopback
Hostvalues only. It requires anx-portless-lease: 1header and refuses requests that carryOriginorSec-Fetch-Site, so a page open in a browser can't hold the proxy open. Leases are capped at 32.I have an implementation with tests ready and will open it as a PR that links here. Happy to change the interface or the defaults based on feedback.
All reactions