NashTech Blog

Flaky Tests When Scaling Parallel Execution: A 4-to-8-Worker Investigation

Table of Contents

Flaky tests when scaling parallel execution: A 4-to-8-worker investigation

Automation testing, Parallel execution, Playwright, Behave, Flaky tests

Our test suite had been rock-solid for weeks at 4 parallel workers. Then, to cut down pipeline time, we bumped it to 8 workers – and things fell apart. Tests started failing at random. No pattern. Re-run the same test alone, and it passed every time.

What we actually learned wasn’t “how to fix two bugs.” It was that both bugs came from the same wrong assumption: we’d built parts of the system as if it were synchronous, when it wasn’t. That’s the thread running through everything below – each bug is just a different flavor of the same mistake.

Figure 1: Scaling from 4 to 8 workers is where the flakiness showed up.

The numbers, before and after

Rough figures, but enough to show the tradeoff we were making:

SetupRun timePass rate
4 workers~18 min~100%
8 workers (before fix)~11 min~87%
8 workers (after fix)~11 min~100%

We cut runtime by almost 40% – but at the cost of exposing two bugs that had been sitting there the whole time. After the fix, we kept the speed and got the stability back.

Why “just add a retry” isn’t the fix

The first instinct when a test gets flaky is to add a retry or bump the timeout. The problem is retried doesn’t actually fix a flaky test – it just hides the symptom well enough that CI goes green.

Here’s a quick way to tell the difference between a reasonable retry and one that’s papering over a real bug: if retrying cuts down the failure rate but never gets it to 0%, there’s still a root cause you haven’t found.

Root cause #1: A race condition on a shared data file

Our framework used a single data.json file as the source of truth for tracking record changes (CREATE / UPDATE / DELETE) across the change-management test suite.

Before

# data.json - ONE file shared by ALL workers
{
  "record_101": {"status": "UPDATED", "field": "amount"},
  "record_102": {"status": "DELETED"}
}

def update_record_and_verify(context, record_id, new_value):
    data = load_json("data.json")
    data[record_id] = {"status": "UPDATED", "field": new_value}
    save_json("data.json", data)

    # ... UI action to update the record ...

    data = load_json("data.json")
    assert data[record_id]["status"] == "UPDATED"

Why higher load made this worse

To be precise: JSON itself has no concept of “atomic” – it’s just a data format. The real problem is that writing the file (save_json) isn’t guaranteed atomic when multiple processes touch it at once.

Every call to that function runs three steps in sequence: load → modify → save. With 8 workers running in parallel, Worker A’s three steps can get interleaved with Worker B’s:

Figure 2: A race condition on a shared JSON file – Worker B’s write silently overwrites Worker A’s change.

Worker A: load (reads the whole file)
Worker B: load (reads the whole file - same moment)
Worker A: modify (updates record_101 in memory)
Worker B: modify (updates record_102 in memory)
Worker A: save (overwrites the file - only A's change survives)
Worker B: save (overwrites the file again - A's change is GONE)

This is a classic lost update: both workers load correctly, both modify correctly, but because save overwrites the whole file, Worker A’s change vanishes with no error thrown anywhere. At 4 workers, the odds of two write sequences overlapping at just the wrong moment were still there – just a lot lower. The race condition doesn’t disappear at low load. It just gets fewer chances to fire.

After: worker-ID isolation

def update_record_and_verify(context, record_id, new_value):
    worker_file = f"change_data_worker_{context.worker_id}.json"

    data = load_json(worker_file)
    data[record_id] = {"status": "UPDATED", "field": new_value}
    save_json(worker_file, data)

    data = load_json(worker_file)
    assert data[record_id]["status"] == "UPDATED"

Each worker gets its own file. The load-modify-save sequence can no longer collide with another worker’s sequence, no matter how many runs in parallel.

Root cause #2: Reading the DOM before Angular finished rendering

Before

@then('the sorted result in the table should be updated')
def verify_sort_result(context):
    context.page.click("th.mat-column-amount .mat-sort-header-arrow")
    first_row_value = context.page.text_content("td.mat-column-amount >> nth=0")
    assert first_row_value == context.expected_first_value

Angular Material doesn’t update the DOM the instant you click sort. It has to run through a change detection cycle before re-rendering the table. On a dev machine with light load, that cycle finishes fast enough that this always passed. On CI with 8 workers sharing CPU, the cycle took longer – and the assertion sometimes read the DOM before the re-render finished, even though sorting worked perfectly fine.

Figure 3: Playwright’s polling assertion – expect() waits out the render cycle instead of reading the DOM immediately.

After: a polling assertion

from playwright.sync_api import expect

@then('the sorted result in the table should be updated')
def verify_sort_result(context):
    context.page.click("th.mat-column-amount .mat-sort-header-arrow")
    first_row = context.page.locator("td.mat-column-amount").nth(0)
    expect(first_row).to_have_text(context.expected_first_value)

Playwright’s auto-wait isn’t just about waiting for an element to show up. It auto-retries a bunch of assertions – to_have_text(), to_be_visible(), to_have_value(), and others – checking the condition over and over until it’s true or the timeout hits. That’s a bigger deal than most people realize; auto-wait covers a lot more than “did the element appear.” The full list of retrying assertions is in Playwright’s own docs, worth a skim if you’ve only ever used to_have_text().

On the Angular side, the change detection cycle behind all this is described in Angular’s official documentation, if you want the full picture of why the DOM doesn’t update instantly.

The thread connecting both bugs

Neither bug looked like the other on the surface – one’s about file I/O, the others about DOM rendering. But they share the same root: we’d written code as if things happened instantly, when in reality both the write and the render take time. That assumption only breaks once you throw enough concurrent load at it.

Infrastructure tweaks (secondary, not the fix)

After fixing the two root causes, we made a few infrastructure adjustments to improve stability further:

  • Dynamic timeout scaling – longer timeouts as worker count goes up, since more workers means more CPU contention and slower UI response times.
  • @AUTORETRY_N tagging – controlled retries on steps sensitive to network latency, not applied blanket-wide.
  • Headless mode tuning – cutting unnecessary render overhead on CI.

These only help once the actual root causes are fixed. Applying them first would’ve just been another way of hiding the symptom.

A framework for debugging flaky tests at scale

QuestionIf the answer is “yes”Where to look
Does it fail more as worker count goes up?YesShared state / race condition
Does it fail right after a UI action?YesTiming / async rendering
Does it still fail after retries?YesRoot cause not yet fixed

Lessons learned

  • ✓ Shared state is always the first suspect when scaling parallel execution
  • ✓ Retries should handle transient failures, not stand in for finding the root cause
  • ✓ Polling assertions beat snapshot assertions in async UIs
  • ✓ CI is always harsher than your local machine, load-wise
  • ✓ Scaling doesn’t create new bugs – it just gives existing ones a chance to show up

FAQ

What is a flaky test? A test that produces inconsistent results – pass one run, fail the next – without any change to the test code or the app under test.

Why do flaky tests show up more often at higher worker counts? Because a lot of them are race conditions or timing issues, and the odds of them actually firing go up with concurrent load. They can still happen at low load – just less often.

Is retrying a valid way to fix a flaky test? Only as a supplement after the root cause is fixed. If retries don’t get the failure rate to 0%, there’s still something unresolved underneath.

Should I just use time.sleep() to fix a flaky test? Fine for a quick debug session. In a real test suite, a fixed sleep usually just slows everything down without removing the root cause – it can’t adapt to how much load the system is actually under.

How does Playwright’s expect() different from a regular assertion? It re-checks the condition repeatedly over a window of time instead of reading a value once and comparing it – which matches how async UIs like Angular Material actually behave.

Wrapping up

Most of the time, a flaky test isn’t random. It has a root cause you can track down if you keep digging. Scaling from 4 to 8 workers didn’t create new bugs in our case – it just exposed two assumptions that were already wrong: a shared file write that wasn’t atomic across processes, and a DOM read that assumed instant updates.

Before you scale your own suite up, it’s worth asking: what parts of your system are you assuming are synchronous, when they actually aren’t?

Picture of minh.nguyenhoang1@nashtechglobal.com

minh.nguyenhoang1@nashtechglobal.com

Suggested Article

Scroll to Top