Skip to content

Keep the Busy long-poll open when a job completes - #4729

Open
osazemeu wants to merge 5 commits into
actions:mainfrom
osazemeu:keep-busy-poll-open-on-online
Open

osazemeu wants to merge 5 commits into
actions:mainfrom
osazemeu:keep-busy-poll-open-on-online

Conversation

@osazemeu

@osazemeu osazemeu commented Sep 19, 2026 •

Copy link
Copy Markdown

Fixes #4728.

When a job completes, OnJobStatus cancels the open GET /message?status=Busy poll so the next poll reports Online. The service assigns the next queued job at that moment and delivers it on the Busy poll, so the abort drops the message. The redelivery arrives after the assignment expires (acquirejob 409 MissingKey) and the job is eventually cancelled as "not acquired by Runner".

The first commit keeps the poll open on the transition to Online (_runnerStatus is still updated and sent on the next poll). It is the change running in production, see below. The second commit replaces that with a 3 s grace period and is the unverified part of this PR.

Grace commit: on the transition to Online, the open poll is cancelled after a 3 s grace period (CancelAfter) instead of at once. A job the service assigns at completion still arrives on that poll, and the next poll reports Online about 3 s later. The transition to Busy still cancels immediately, as before.

Why a grace at all: @dsmirnoff73 reported on #4728 that on 2.338.2 (ARM64, 8 runners on one host) the first commit left an idle runner waiting about 61 s, because the open poll keeps reporting Busy, and that a 3 s grace removed the wait. That does not reproduce on six org-scoped x64 runners running the first commit: 322 jobs queued within 55 s of a completion were all acknowledged within 5 s (2026-10-31 to 2026-11-07), and MissingKey was 15 across all runners from 2026-10-01 to 2026-11-07. The grace commit is kept as a draft until CI and a canary run agree, and can be dropped if the first commit is enough.

Applied to both BrokerMessageListener and MessageListener, which share the pattern. Each poll has its own token source, disposed in finally, so a pending timer cannot cancel a later poll.

Tests

OnJobStatus_OnlineGivesInFlightPollAGracePeriod_BusyCancelsIt in both BrokerMessageListenerL0 and MessageListenerL0. The mocked poll stays open until the test answers it or its token is cancelled. The test checks that:

  • an Online status event leaves the in-flight Busy poll uncancelled at first;
  • after the grace period that poll is cancelled and the next poll reports Online;
  • a Busy status event still cancels the in-flight poll at once, and the re-issued poll returns its message from GetNextMessageAsync.

Grace commit not yet run locally: the .NET 8.0.425 SDK pinned in global.json is not installed on the author's machine, so CI is the first run of the grace-period version.

Not covered

acquirejob timing out after 100 s and then getting 409 MissingKey on the retry (reported from WSL2 in #4728). That is a separate path from this race and is not changed here.

Feature flag

Not added. The Features flags in Constants.cs reach the runner in job messages, and OnJobStatus runs between jobs, so there is no flag the listener could read at this point.

@osazemeu

Copy link
Copy Markdown
Author

Results from running this patch on two self-hosted runners (v2.337.0 + this change, Linux x64), swapped in at 2026-09-19 09:53Z.

Before (stock 2.337.0): acquirejob 409 MissingKey in the runners' _diag logs: 9 of 150 jobs on 09-13, 17 of 213 on 09-16, 31 of 231 on 09-17, and 63 of 139 on 09-19 before the swap. Six jobs were cancelled with "The job was not acquired by Runner of type self-hosted even after multiple attempts" in the three days before.

After (patched): 0 MissingKey across 349 jobs over about 2.1 days, and 0 "not acquired" cancellations across 47 workflow runs.

The only change between the two periods is this patch. Marking ready for review.

@Jeddyvgf

Jeddyvgf commented Oct 1, 2026

Copy link
Copy Markdown

jest.config.js

@osazemeu
osazemeu force-pushed the keep-busy-poll-open-on-online branch 4 times, most recently from 7a4af2f to 95b52ee Compare October 3, 2026 22:32
@inslayn

inslayn commented Oct 4, 2026

Copy link
Copy Markdown

This patch also worked for me, thanks @osazemeu!

@osazemeu
osazemeu force-pushed the keep-busy-poll-open-on-online branch from 95b52ee to 964126e Compare October 6, 2026 17:49
OnJobStatus cancelled the in-flight GetMessages poll on every status
change. On the Busy to Online transition at job completion, the service
can assign the next queued job and deliver it on that still-open Busy
poll; aborting the request dropped the message, and the redelivery
arrived after the assignment expired (acquirejob 409 MissingKey), so the
job was eventually cancelled as "not acquired by Runner".

An Online status is now recorded and sent on the next poll without
aborting the open one. A Busy transition still cancels as before.
Applied to both BrokerMessageListener and MessageListener.

Fixes actions#4728.
@osazemeu
osazemeu force-pushed the keep-busy-poll-open-on-online branch from 964126e to b20eabd Compare October 6, 2026 17:49
@osazemeu

osazemeu commented Oct 6, 2026

Copy link
Copy Markdown
Author

you're welcome @inslayn 🥳

@hrokling

Copy link
Copy Markdown

Hi maintainers, could someone review this PR and let us know whether anything else is needed before it can be merged? The author reported zero MissingKey errors across 349 jobs after applying the patch, and @inslayn also confirmed it worked for them. Thanks for taking a look!

@Mausino

Mausino commented Nov 3, 2026

Copy link
Copy Markdown

Data point from a production fleet: 40 persistent, repo-scoped Linux x64 runners. In a canary on our repository, the official build lost 13.4 % of jobs with "The job was not acquired by Runner". With the poll kept open on Busy -> Online in BrokerMessageListener.OnJobStatus, the same canary lost 1 job in 551 (0.18 %).
We run that change as a local build, and self-update replaces it with every new runner release, as happened on 2026-11-02. Could a maintainer review this PR, or say what is missing? We can share more data if it helps.

Leaving the Busy poll open until it ended on its own kept the runner
reported as Busy, and the service does not offer jobs to a Busy runner,
so the ~61 s gap moved instead of disappearing. On Online, cancel the
poll after 3 s: a job assigned at completion still arrives on it, and the
next poll reports Online.
@osazemeu
osazemeu marked this pull request as draft November 9, 2026 06:39
@osazemeu
osazemeu marked this pull request as ready for review November 22, 2026 21:59
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.

Persistent runner drops the next job when it aborts its Busy long-poll at job completion ("was not acquired by Runner")

5 participants