Skip to content

LTS: flush and finish open runs as interrupted on SIGTERM - #65

Merged
shubhamkd merged 4 commits into
nightwatchjs:mainfrom
adityasonani-bs:lts-sigterm-interrupted
Oct 26, 2026
Merged

shubhamkd merged 4 commits into
nightwatchjs:mainfrom
adityasonani-bs:lts-sigterm-interrupted

Conversation

@adityasonani-bs

@adityasonani-bs adityasonani-bs commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

What

Adds an LT-only SIGTERM path for runs on BrowserStack's Load Testing Service (LTS). When the load-testing runner stops a run partway (a user stop, a timeout, or the runner's own end-of-duration cut), the plugin now finishes the tests that were still open as interrupted, with the steps they had completed, instead of dying on the signal and losing them.

The path is installed in before, after launchTestSession, only when all of these hold:

  • helper.isLoadTestingSession() is true;
  • BROWSERSTACK_TESTHUB_UUID and BROWSERSTACK_TESTHUB_JWT are set (a TestHub build exists);
  • BROWSERSTACK_LTS_SDK_PID_FILE is set (only the LTS runner exports it);
  • no BrowserStack Local tunnel is running.

Without all four, nothing is installed and the plugin behaves exactly as today, so non-LT users see no change.

Once installed, the plugin writes the PID file (bare PID, mode 0644, via a tmp file and an atomic rename), registers one SIGTERM handler, and removes the PID file on exit. On SIGTERM the handler (idempotent; a second signal is ignored):

  1. ignores new TestRunStarted / TestRunFinished events;
  2. builds one TestRunFinished with result interrupted for each open run, from that run's own TestRunStarted payload (same identity fields, so the same test_hash and the same Tests-tab row), with finished_at = the cut time, duration_in_ms = the time to the cut and, for LCNC runs, a snapshot of the completed global.__bstack_steps in meta.steps; each run is marked finished first, so a late real finish or the teardown sweep cannot write over it;
  3. awaits the in-flight events and drains the request queue;
  4. POSTs the interrupted finishes in their own request, strictly before the build-stop PUT (stopping the build invalidates the build JWT);
  5. stops the build and exits 143.

Bounds: every request on this path has its own wall-clock timeout (2000 ms, through a new totalTimeout option of makeRequest that bounds the whole call; only this path sets it, so timeout keeps its connect/idle meaning for every other caller), and the whole flush has a deadline (8000 ms). If the deadline runs out, unsent work is dropped from the end: the build stop first, then the interrupted POST. Each leaves today's outcome (an open build, a test left pending). meta.steps is capped at 126976 characters, 4096 under TestHub's 131072-character meta limit; if the steps do not fit, the earliest completed steps that fit are kept. after() and the handler share a teardown-owner flag: whichever starts first owns the sweep, the queue drain and the build stop. The path never calls browser.* and ignores EPIPE on stdout and stderr.

No version bump in this PR; the release is left to the maintainers.

Diff: 8 files, +1125 -10, of which 910 lines are new tests (test/src/test-observability/ltsStopPath.js 719, test/src/utils/ltsRequestTimeout.js 191).

Why

On the Load Testing Service a Nightwatch run can be cut while a test is still running. The plugin and Nightwatch handle no signals, so the cut test stayed pending, the steps an LCNC test had completed were lost, and so were the results in the plugin's last ~2 s batch. With this change the cut test is reported interrupted (an existing TestHub result, counted under Other; no new status), its completed steps are attached, and the last batch is delivered.

Testing

  • npm test (mocha --recursive --timeout 10000, with BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY unset): 159 passing, 0 failing (110 existing plus 49 new), on Node 18.18.0.
  • npm run eslint: clean.
  • Validated end to end on BrowserStack's internal regression environment. That run used the same runtime code, before the comments and test titles were cleaned up for upstream.

The LT SIGTERM flush has to finish inside the plugin's stop deadline, and
makeRequest had no request-level bound: @cypress/request's `timeout`
covers only the connect and the gaps between bytes, so a response that
trickles in, or a call queued behind the 2-socket agent, could hold a
stage until the 45s socket timeout.

makeRequest now honours config.timeout as a wall-clock bound (abort and
reject), and the queue drain passes one through. Callers that set no
timeout send exactly the config they sent before.
A test the runner cuts mid-run has to land in TestHub as one
`interrupted` row, the same row its TestRunStarted opened. The synthetic
teardown finish lacks file_name, location, vc_filepath, integrations and
product_map, so it re-hashes into a second Tests-tab row.

On LT runs the plugin now keeps each run's TestRunStarted payload and
builds the interrupted finish from it: the same identity fields, the
lowercase 'interrupted' literal TestHub maps by exact match, a
time-to-cut duration_in_ms, and the LCNC steps completed before the cut,
capped as a prefix so TestHub's 131072-char meta limit never drops them
all. The finishes go in their own batch request, and the build stop takes
a per-request timeout. Non-LT runs keep no copy and send what they sent.
When the load-testing runner cuts a Nightwatch / LCNC run (a stop, a
timeout, or its own end-of-duration or rampdown cut) the plugin died on
the group TERM: the cut test stayed pending, its completed LCNC steps
were lost, and so was the last ~2s batch of finished results.

For an LT session with a TestHub build and the runner's PID-file env
(BROWSERSTACK_LTS_SDK_PID_FILE), and without BrowserStack Local, before()
now writes the PID file (bare PID, 0644, atomic, removed on exit) and
installs one SIGTERM handler. On the signal it ignores new test events,
finishes every open run as interrupted and marks it finished, waits for
in-flight events, drains the queue, sends the interrupted finishes in
their own request, stops the build, and exits 143. Each request has its
own timeout and the whole flush is bounded by the stop deadline; when it
runs out the build stop, then the interrupted POST, are the ones dropped.
Whichever of after() and the handler starts first owns the teardown.
EPIPE on stdout or stderr is ignored, and the handler never calls the
browser.

Without all of those conditions nothing is installed, so hand-run
Nightwatch, older images and non-LT users run exactly as before.
@adityasonani-bs
adityasonani-bs force-pushed the lts-sigterm-interrupted branch from b5259c7 to c3e2711 Compare October 23, 2026 09:51
makeRequest's whole-call timer is now keyed on a new `totalTimeout`
option that only the LTS stop path sets (the queue drain, the
build-stop PUT and the interrupted POST). `timeout` keeps
@cypress/request's connect/idle meaning, so test orchestration's
`timeout: 30000` is no longer a 30 s abort of a live response for
non-LT users.

Regression test: "does not abort a live trickling response when only
`timeout` is set" fails on the previous head and passes here.
@shubhamkd
shubhamkd merged commit 3e7dd00 into nightwatchjs:main Oct 26, 2026
6 checks passed
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.

3 participants