Skip to content

server: keep session alive between sequential range requests - #402

Open
anishpentyala wants to merge 3 commits into
claudiodangelis:mainfrom
anishpentyala:range-grace
Open

anishpentyala wants to merge 3 commits into
claudiodangelis:mainfrom
anishpentyala:range-grace

Conversation

@anishpentyala

Copy link
Copy Markdown
Contributor

Problem

qrcp send stops the server as soon as the session's in-flight request count drops to zero. A downloader fetching the file in sequential ranges sends its next chunk request right after the previous one completes, but in between the count is zero: the first chunk gets a 206 and every following one gets a connection error.

Reproduced with the included test (and by hand with two curl range requests carrying the session cookie): the second request fails with connection refused.

The session logic is meant to support exactly this case: "The server stops when that browser's requests (possibly several, for parallel chunks) are done."

Fix

When the last in-progress request of a session was a range request, the server waits a short grace period (2 seconds) before stopping, and stops only if no further request of the session has arrived meanwhile. A counter bumped on every admitted session request guards the timer: a timer left over from an earlier idle moment finds the counter changed and does nothing, so only the newest timer can stop the server. Plain full-file requests still stop the server as soon as they complete, so the common one-shot download flow is unchanged.

Two honest limitations:

  • The grace is a heuristic: a client that pauses longer than 2 seconds between chunks is still refused. Tracking served byte ranges and stopping only at full coverage would be the stricter alternative, at much higher complexity.
  • Any request with a Range header (browsers often send bytes=0-) now leaves the server up for 2 extra seconds after it finishes.

Tests

  • New TestSendSequentialRangesKeepServerUp: two sequential range GETs with the session cookie both return 206 and the correct bytes. Before the fix the second request fails with connection refused; after the fix both succeed.
  • New TestStaleRangeGraceTimerDoesNotStopServer (with the grace shortened via the now-var period): a timer from an earlier idle moment fires while the next chunk's grace is still running, and must not stop the server; the newest timer still does. Fails against a timer without the generation counter ("stale timer stopped the server during the next chunk's grace period"), passes with it.
  • endSessionRequest now takes the range flag, so the existing direct-call tests were updated to pass false, keeping their immediate-stop assertions unchanged.
  • go test ./server/ is green. (The only failure in ./... is config.TestNew, which panics with "no interfaces found" in this sandbox: pre-existing and environmental, unrelated to this change.)

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.

1 participant