Skip to content

🐛 improve early request collection reliability in Firefox#4573

Draft
BenoitZugmeyer wants to merge 1 commit intov7from
benoit/debug-no-bs-firefox
Draft

🐛 improve early request collection reliability in Firefox#4573
BenoitZugmeyer wants to merge 1 commit intov7from
benoit/debug-no-bs-firefox

Conversation

@BenoitZugmeyer
Copy link
Copy Markdown
Member

Motivation

The microfrontend e2e test microfrontend > RUM > with beforeSend > expose handling stack for fetch requests › async fails ~70% of the time on firefox-pinned (10 failed / 6 flaky / 4 passed out of 20 reproductions). The same race affects any fetch resource event in production: event.context.handlingStack (and the rest of the request-derived domain context — requestInit, response, error, …) is intermittently undefined for fetch resources on Firefox.

Root cause

When a fetch resolves, two things happen around the same time:

  • the browser dispatches a PerformanceResourceTiming entry to our observer
  • afterSend() resumes from await responsePromise and notifies REQUEST_COMPLETED

On Firefox the observer task can fire before the resolve microtask runs, so the request isn't in the requestRegistry yet when assembleResource() looks it up via requestRegistry.getMatchingRequest(). The lookup returns undefined and the resulting resource event is missing all request-derived domain context.

This was introduced by #4285 always collect early requests (commit 2b71062e7), which switched from request-driven (performance.getEntriesByName() at REQUEST_COMPLETED time) to entry-driven matching.

CI logs of the failing case make the ordering obvious:

[failure run]
[DEBUG resourceCollection assembleResource]
  entryStartTime:90, matchedRequest:null, registrySnapshot:[]   ← runs first, registry empty
[DEBUG requestRegistry REQUEST_COMPLETED]
  url:.../ok, startRel:89, hasHandlingStack:true, nowRel:121     ← arrives too late

Changes

  • startResourceCollection: for request-type entries (fetch / xmlhttprequest initiator), defer the registry lookup by REQUEST_MATCHING_DELAY (50 ms) via setTimeout. In practice the matching REQUEST_COMPLETED arrives within ~30 ms even on slow CI runners, so 50 ms is a generous bound.
  • assembleResource now takes the resolved request directly instead of pulling it from the registry — the matching policy is owned by the call site where the timing context is clear.
  • requestRegistry is unchanged; getMatchingRequest is still synchronous.
  • Unit tests drive the new delay through mockClock.tick(REQUEST_MATCHING_DELAY) inside runTasks.

Test instructions

Repro on the e2e suite (Firefox is the most reliable, but the race is browser-agnostic):

yarn playwright test --config test/e2e/playwright.config.ts --project firefox-pinned \
  --repeat-each=20 \
  -g 'microfrontend RUM with beforeSend expose handling stack for fetch requests async'

CI validation (e2e job scoped to the failing test, --repeat-each=20):

pipeline chromium firefox-pinned webkit-pinned
before fix 20/20 4/20 (10 failed, 6 flaky) 20/20
after fix 20/20 20/20 20/20

Checklist

  • Tested locally
  • Tested on staging
  • Added unit tests for this change.
  • Added e2e/integration tests for this change.
  • Updated documentation and/or relevant AGENTS.md file

@BenoitZugmeyer BenoitZugmeyer changed the base branch from main to thomas.lebeau/no-bs May 7, 2026 16:35
@BenoitZugmeyer BenoitZugmeyer force-pushed the benoit/debug-no-bs-firefox branch from a3581e4 to 8558f01 Compare May 7, 2026 16:36
When a fetch resolves, two things happen around the same time:
- the browser dispatches a PerformanceResourceTiming entry to our observer
- afterSend() resumes from `await responsePromise` and notifies REQUEST_COMPLETED

On Firefox the observer task can fire before the resolve microtask runs, so
the request isn't in the registry yet when assembleResource() looks it up,
and the resource event ends up without handlingStack / requestInit / etc.

Defer the registry lookup for request-type entries by REQUEST_MATCHING_DELAY
(50ms) so the matching REQUEST_COMPLETED has time to land. assembleResource
now takes the resolved request directly instead of pulling it from the
registry. Tests drive the delay through mockClock.tick().
@BenoitZugmeyer BenoitZugmeyer force-pushed the benoit/debug-no-bs-firefox branch from 8558f01 to b645dab Compare May 7, 2026 16:36
@BenoitZugmeyer BenoitZugmeyer changed the title 🐛 fix race between PerformanceObserver and REQUEST_COMPLETED on Firefox 🐛 improve early request collection reliability in Firefox May 7, 2026
@BenoitZugmeyer BenoitZugmeyer changed the base branch from thomas.lebeau/no-bs to v7 May 7, 2026 16:38
@cit-pr-commenter-54b7da
Copy link
Copy Markdown

cit-pr-commenter-54b7da Bot commented May 7, 2026

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum 169.22 KiB 169.26 KiB +39 B +0.02%
Rum Profiler 6.10 KiB 6.10 KiB 0 B 0.00%
Rum Recorder 21.23 KiB 21.23 KiB 0 B 0.00%
Logs 54.56 KiB 54.56 KiB 0 B 0.00%
Rum Slim 127.56 KiB 127.59 KiB +31 B +0.02%
Worker 22.99 KiB 22.99 KiB 0 B 0.00%
🚀 CPU Performance
Action Name Base CPU Time (ms) Local CPU Time (ms) 𝚫%
RUM - add global context 0.0039 0.002 -48.72%
RUM - add action 0.0211 0.0125 -40.76%
RUM - add error 0.02 0.0118 -41.00%
RUM - add timing 0.001 0.0005 -50.00%
RUM - start view 0.0194 0.0123 -36.60%
RUM - start/stop session replay recording 0.0014 0.001 -28.57%
Logs - log message 0.0342 0.0174 -49.12%
🧠 Memory Performance
Action Name Base Memory Consumption Local Memory Consumption 𝚫
RUM - add global context 43.46 KiB 38.90 KiB -4.56 KiB
RUM - add action 67.59 KiB 63.35 KiB -4.24 KiB
RUM - add timing 42.64 KiB 36.74 KiB -5.90 KiB
RUM - add error 75.08 KiB 69.76 KiB -5.32 KiB
RUM - start/stop session replay recording 49.95 KiB 42.04 KiB -7.91 KiB
RUM - start view 497.36 KiB 488.52 KiB -8.84 KiB
Logs - log message 59.14 KiB 54.38 KiB -4.75 KiB

🔗 RealWorld

@datadog-official
Copy link
Copy Markdown

datadog-official Bot commented May 7, 2026

Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

🎯 Code Coverage (details)
Patch Coverage: 83.33%
Overall Coverage: 77.50% (+0.02%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: b645dab | Docs | Datadog PR Page | Give us feedback!

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.

2 participants