Skip to content

Fixes #37889: Wait for the metrics worker's in-flight item before summarizing - #37890

Merged
Khairajani merged 3 commits into
mainfrom
fix/operation-metrics-flush-race
Nov 16, 2026
Merged

Khairajani merged 3 commits into
mainfrom
fix/operation-metrics-flush-race

Conversation

@Khairajani

@Khairajani Khairajani commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #37889

  • A summary read now records what is queued, then waits (up to 1s) for the item the worker already took. Before, it treated an empty queue as flushed, so that item went missing. This is the 999 == 1000 flake in test_high_concurrency_accuracy.
  • Recording and merging per-thread metrics now share the lock, so a merge can't take a thread's metrics while they are being written.
  • Every item taken off the queue is marked done, so the wait always ends.
  • Added test_summary_waits_for_the_item_the_worker_is_recording. It holds the worker mid-record, so it fails on main every time and passes with this change.

🤖 Generated with Claude Code

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; the previous test-race finding is fixed.

Summary

The PR makes summary reads wait for metrics the worker has already taken.

  • Metrics summaries wait up to one second for unfinished worker items.
  • Per-thread metric updates and merges use the same lock.

The previous test-race finding is fixed. No new actionable issues were found.

Diagram

sequenceDiagram
  participant Reader as Summary reader
  participant Queue as Metrics queue
  participant Worker as Metrics worker
  Worker->>Queue: Take an item
  Reader->>Queue: Drain queued items
  Reader->>Queue: Wait for unfinished items, up to 1 second
  Worker->>Worker: Record item under shared lock
  Worker->>Queue: Mark item done
  Queue-->>Reader: Wake waiting reader
  Reader->>Reader: Merge metrics and build summary
Loading

Reviews (2) · Last reviewed commit: "docs: state when a metrics summary read ..." · Reviewed by Greptile

@github-actions

Copy link
Copy Markdown
Contributor

❌ PR checklist incomplete

This PR cannot be merged until the following are addressed on its linked issue:

The fields live on the linked issue in the Shipping project (open the issue → right sidebar → Projects). After you set them, re-run this check (or push a commit) — issue/project changes do not re-trigger it automatically.

Maintainers can bypass this check by adding the skip-pr-checks label.

@Khairajani Khairajani added the safe to test Add this label to run secure Github workflows on PRs label Nov 16, 2026
… race test deterministic

- Release the held worker only once the summary waits on the queue, so the
  regression test cannot pass without the fix.
- Read thread ids under the lock in merge_all_threads.
- Update the docstring and field comment that still called recording lock-free.
- Add decision records for the 1s flush wait and the shared record/merge lock.
@gitar-bot

gitar-bot Bot commented Nov 16, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 🟡 Medium risk 2 closed / 2 findings

Serializing metric writes with merges and waiting up to one second for in-flight queue work could delay summaries or omit metrics on timeout.

Fixes the flaky 999 == 1000 summary race by having summary reads wait up to 1s for the item the metrics worker already took, and by sharing the metrics lock between recording and merging. Both previously unrecorded decisions are now resolved, and a regression test covers the mid-record case. No open issues found.

✅ 2 closed
✅ Quality: Unrecorded decision: summary reads wait at most 1s for the metrics worker

📄 ingestion/src/metadata/utils/operation_metrics.py:47-48 📄 ingestion/src/metadata/utils/operation_metrics.py:489-493
FLUSH_TIMEOUT_SECONDS = 1.0 is a new timeout chosen by judgment. When it runs out, get_summary/get_workflow_timing/reset go ahead without the worker's in-flight item, so it is a trade between accuracy and latency. Nothing in docs/decisions/ covers operation-metrics flushing. The rule comes from the description ("a summary read now records what is queued, then waits (up to 1s) for the item the worker already took") and the comment at L489-490. A record would say: "A metrics summary read waits at most 1 second for the metrics worker to finish the items it has already dequeued. It never blocks unbounded on Queue.join, and after the timeout it reports what has been recorded so far."

✅ Quality: Unrecorded decision: recording and merging must share the metrics lock

📄 ingestion/src/metadata/utils/operation_metrics.py:327-334 📄 ingestion/src/metadata/utils/operation_metrics.py:347-348
Correctness now depends on mutual exclusion. _record_sync writes _thread_metrics under self._lock, and merge_thread_metrics pops a thread's data under that same lock, so a merge cannot take a thread's metrics while they are being written. This reverses the old design of per-thread recording without a lock. The class docstring (L171-172) and the field comment at L190 ("Per-thread metrics for lock-free recording") still describe the old design, which invites a later change that drops the lock again. No record in docs/decisions/ covers this, and the timeout record would not cover it either. The rule comes from the description: "Recording and merging per-thread metrics now share the lock, so a merge can't take a thread's metrics while they are being written." A record would say: "Every write to OperationMetricsState._thread_metrics and every pop of a thread's entry from it hold OperationMetricsState._lock." Also update the docstring and the L190 comment so they no longer call recording lock-free.

Verification

Rules No rules evaluated

Functional validation Not enabled

Set up

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Compact
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source

@Khairajani
Khairajani merged commit 930926a into main Nov 16, 2026
54 of 57 checks passed
@Khairajani
Khairajani deleted the fix/operation-metrics-flush-race branch November 16, 2026 12:31
Khairajani added a commit that referenced this pull request Nov 17, 2026
…re summarizing (#37899)

* Fixes #37889: Wait for the metrics worker's in-flight item before summarizing (#37890)

* fix(ingestion): wait for the metrics worker's in-flight item before summarizing

* fix(ingestion): record the metrics flush and lock decisions, make the race test deterministic

- Release the held worker only once the summary waits on the queue, so the
  regression test cannot pass without the fix.
- Read thread ids under the lock in merge_all_threads.
- Update the docstring and field comment that still called recording lock-free.
- Add decision records for the 1s flush wait and the shared record/merge lock.

* docs: state when a metrics summary read can take the full flush wait

(cherry picked from commit 930926a)

* docs: the metrics flush ADR bounds only the wait, not the whole read
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

safe to test Add this label to run secure Github workflows on PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Operation metrics summary can miss an operation the worker is still recording

2 participants