Skip to content

Investigate and fix flakiness of test_final_statistics #1256

Closed

Description

@Pijukatel

It seems that FinalStatistics.request_avg_failed_duration can be zero in some cases during tests even though there are some failed requests.

Example fail:

E       assert datetime.timedelta(0) > datetime.timedelta(0)
E        +  where datetime.timedelta(0) = FinalStatistics(requests_finished=45, requests_failed=5, retry_histogram=[25, 16, 9], request_avg_failed_duration=datetime.timedelta(0), request_avg_finished_duration=datetime.timedelta(microseconds=7503), requests_finished_per_minute=4428, requests_failed_per_minute=491, request_total_duration=datetime.timedelta(microseconds=337613), requests_total=50, crawler_runtime=datetime.timedelta(microseconds=609809)).request_avg_failed_duration
E        +  and   datetime.timedelta(0) = timedelta()```

Activity

  1. self-assigned this
    on Jun 17, 2025
  2. added
    t-toolingIssues with this label are in the ownership of the tooling team.
    on Jun 17, 2025
  3. Pijukatel commented on Jun 18, 2025

    @Pijukatel
    CollaboratorAuthor

    This can actually happen because the RequestProcessingRecord._last_run_at can be the same as datetime.now() called later. Which is pretty suspicions. It can happen even in the test where there is explicit 0.001s sleep, so at least in that test it should not be possible even in theory.

  4. Pijukatel commented on Jun 18, 2025

    @Pijukatel
    CollaboratorAuthor

    Seems to be happening on Windows. It is possible that system clock on Windows has worse resolution and so this would yield same time in some cases (depending in the sampling luck)

      t1 = datetime.now(tz=timezone.utc)
      time.sleep(0.001)
      t2 = datetime.now(tz=timezone.utc)
    
  5. Pijukatel commented on Jun 18, 2025

    @Pijukatel
    CollaboratorAuthor

    Using time.perf_counter() confirms the assumption. While the datetime.now() can still yield the same time, time.perf_counter() with better resolution can detect the change.

  6. added a commit that references this issue on Jun 26, 2025
    9e92f6b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

t-toolingIssues with this label are in the ownership of the tooling team.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions