Skip to content

fix(browserstack-service): close orphaned cucumber hooks that inflate build duration (SDK-7167) - #120

Open
osho-20 wants to merge 2 commits into
mainfrom
fix/sdk-7167-cucumber-hook-duration
Open

fix(browserstack-service): close orphaned cucumber hooks that inflate build duration (SDK-7167)#120
osho-20 wants to merge 2 commits into
mainfrom
fix/sdk-7167-cucumber-hook-duration

Conversation

@osho-20

@osho-20 osho-20 commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

What is this about?

Fixes incorrect (inflated) build durations on the new Test Observability dashboard for WebdriverIO + Cucumber runs ([Aya] customer report: build showed 4h 35m on the new dashboard vs 2h 42m on the old one).

Root cause: some Cucumber AFTER_EACH hooks emit HookRunStarted but their HookRunFinished is never sent. The backend holds the hook open until the project's hook timeout (2h) and the build watchdog inflates the reported duration. Customer SDK debug logs prove the drop is client-side: hookEvents { started: 525, finished: 521 } with zero upload failures — 4 finish events were never created.

Three complementary fixes:

  1. Teardown sweep now covers Cucumber (was mocha-only; the code documented Cucumber as a known gap). Cucumber hook meta is tagged with kind/name/hookType/testRunId at start, scenario meta is tagged in beforeScenario and stamped finished in afterScenario, and sweepUnfinished() emits a terminal HookRunFinished / TestRunFinished for any started-but-unfinished entity before the worker's event queue shuts down.
  2. Open hook runs are journaled like open test runs, so when the worker is killed outright mid-hook (Ctrl-C / CI cancellation) the detached exit cleanup finalizes the orphaned hook with a HookRunFinished (hook_run envelope). Previously only orphaned test runs were finalized.
  3. Hardening: the cucumber hook after path skips (with a warning) when no start was recorded instead of emitting an unmatched finish / throwing a TypeError, and in-flight step state is reset at scenario start so an aborted step can no longer silently drop every later AFTER_EACH hook's events for the rest of the worker.

End-to-end verification on Automate (minimal WDIO 9 + Cucumber project, run interrupted via SIGINT while the After hook sleeps):

Unit tests: 9 new tests (cucumber sweep, hook journaling/finalization, after-path guard, step-state reset). Full vitest suite shows the identical 70 pre-existing environmental failures as clean main — zero regressions. Build + eslint clean.

Related Jira task/s

Release (mandatory for every PR — required for the ready-for-review label)

Version bump: (required — tick exactly one)

  • minor (backwards-compatible feature)
  • patch (bug fix or other small change)

Release notes type: (optional)

  • New Feature
  • Bug Fix
  • Other Improvement

Release notes (customer-facing): (optional but encouraged)

  • Fixed inflated build durations on the Test Observability dashboard for WebdriverIO + Cucumber runs: hooks interrupted mid-run are now closed instead of staying "in progress" until the hook timeout.

Release notes (internal): (required — engineer-facing; what actually changed / why)

  • Extended the mocha-only sweepUnfinished() teardown safety net to Cucumber: hook/scenario meta is tagged with kind + identity at start, afterScenario stamps finishedAt, and the sweep emits terminal HookRunFinished/TestRunFinished for started-but-unfinished cucumber entities (SDK-7167 — orphaned AFTER_EACH starts held hooks open until the 2h backend hook timeout, inflating build duration on the new dashboard).
  • listener.hookStarted/hookFinished now record/clear the open-runs journal, and finalizeOrphanedRuns() emits HookRunFinished (hook_run envelope) for journaled hook entries — so a worker killed outright mid-hook gets its hook finalized by the exit cleanup, not just its test run.
  • Cucumber hook after events with no recorded start are skipped with a warning (no unmatched finish, no TypeError); _cucumberData.steps is reset per scenario so a stuck in-flight step can't misclassify later AFTER_EACH hooks as step-level and silently drop their events.

Checklist

  • Ready to review
  • Has it been tested locally?

PR Validations

Run Tests: Comment RUN_TESTS to trigger sanity tests.

🤖 Generated with Claude Code

… build duration (SDK-7167)

A cucumber hook (typically AFTER_EACH) that emitted HookRunStarted but never
its HookRunFinished stayed open on the Test Observability backend until the
project's hook timeout (2h), inflating the build duration shown on the new
dashboard (customer saw 4h35m for a 2h42m build). Customer SDK debug logs
confirmed the drop is client-side: 525 hook starts vs 521 finishes triggered,
zero upload failures.

Three complementary fixes:

- Extend the teardown sweep (previously mocha-only, documented known gap) to
  cucumber: hook meta is tagged kind/name/hookType/testRunId at start,
  scenario meta is tagged in beforeScenario and stamped finished in
  afterScenario, and sweepUnfinished now emits terminal HookRunFinished /
  TestRunFinished for any started-but-unfinished cucumber entity before the
  worker's event queue shuts down.
- Journal open hook runs like open test runs, so when the worker is killed
  outright mid-hook (Ctrl-C / CI cancellation) the exit cleanup finalizes the
  orphaned hook with a HookRunFinished (hook_run envelope) instead of only
  finalizing the test run.
- Guard the cucumber hook 'after' path against a missing start record (skip
  with a warning instead of emitting an unmatched finish / TypeError), and
  reset in-flight step state at scenario start so an aborted step can no
  longer silently drop every later AFTER_EACH hook's events.

Verified end-to-end on Automate: interrupting a run mid-After-hook with the
published 9.33.0 leaves the hook open (only the test run is finalized);
with this fix the exit cleanup finalizes both ("Finalized 2 orphaned
test/hook run(s)").

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@osho-20
osho-20 requested a review from a team as a code owner August 4, 2026 20:03
@osho-20 osho-20 added bug Something isn't working ready-for-review labels Aug 4, 2026
@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

🔴 SDK PR Review gate is red. Pending:

  • The SDK PR Review Agent has not reviewed the current head commit yet — run the SDK PR Review Agent.

It turns green once the latest SDK PR Review Agent run reports GTG on the current head commit. A native reviewer approval is separately required by branch protection before merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ready-for-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant