Skip to content

Fix multiprocessing on macOS for throughput_load_driver_test.py - #7244

Open
shreyaskommuri wants to merge 1 commit into
GoogleCloudPlatform:masterfrom
shreyaskommuri:fix-throughput-load-driver-macos
Open

shreyaskommuri wants to merge 1 commit into
GoogleCloudPlatform:masterfrom
shreyaskommuri:fix-throughput-load-driver-macos

Conversation

@shreyaskommuri

Copy link
Copy Markdown

Summary

Fixes #7243. tests/scripts/throughput_load_driver_test.py fails 6 of its 7 tests on
macOS, even run via the documented python -m unittest invocation. Root cause is
identical to #5883 / PR #6055: Python's default multiprocessing start method is
spawn on macOS, not fork. A spawn-started multiprocessing.Process child is a
brand-new interpreter that re-imports its target module from scratch, so it never sees
a mock.patch() applied in the parent process.

throughput_load_driver's tests mock the module-level _RunCommand, but the actual
work happens inside multiprocessing.Process(target=_TimeCommand, ...) children. On
macOS those children call the real _RunCommand, which tries to run a
nonexistent/empty command, crashes inside the child, and never reports a result —
so tests asserting on the number/content of responses see an empty list instead.

PR #6055 already fixed this exact mechanism for real pkb.py benchmark runs, by
forcing fork on macOS inside pkb.py's Main(). The unit test suite never calls
Main(), so that fix doesn't reach it.

This applies the same fix (multiprocessing.set_start_method('fork', force=True)
gated on platform.system() == 'Darwin') at module level in
tests/pkb_common_test_case.py, the shared base class every PKB test case
subclasses, rather than only in the one failing test file — so any future
multiprocessing-based test gets the same protection.

Also updates CHANGES.next.md per CONTRIBUTING.md.

Test Plan

Before the fix, via the documented test invocation:

$ python -m unittest tests.scripts.throughput_load_driver_test -v
...
FAILED (failures=6)

After the fix:

$ python -m unittest tests.scripts.throughput_load_driver_test -v
...
Ran 7 tests in 62.678s
OK

Checked for regressions:

  • Ran every test file directly under tests/ and every file under tests/scripts/
    via the documented per-file python -m unittest invocation. Only
    messaging_service_scripts_e2e_main_process_test.py and
    messaging_service_scripts_e2e_workers_test.py still fail — confirmed via
    git stash that both fail identically with or without this change
    (AttributeError: <module 'os'> does not have the attribute 'sched_setaffinity',
    a pre-existing, unrelated macOS-only gap: that syscall simply doesn't exist on
    macOS. Out of scope for this fix.
  • pyink --pyink-indentation 2 --pyink-use-majority-quotes --unstable --line-length=80 --diff tests/pkb_common_test_case.py:
    the only reformat suggestions are in pre-existing, unrelated code further down the
    file (confirmed present on a clean master checkout too, via git stash); none of
    the lines this PR adds are flagged.
  • git diff -U0 master | lint-diffs: pylint: mine=0, always=0.

Additional Notes

I've signed the Google CLA. AI was used for context and guidance while investigating
and drafting this fix.

Same root cause as issue GoogleCloudPlatform#5883 / PR GoogleCloudPlatform#6055: Python's default
multiprocessing start method is 'spawn' on macOS, not 'fork', so a
multiprocessing.Process child is a fresh interpreter that re-imports
the target module and never sees mock.patch() applied in the parent
test process. throughput_load_driver's tests mock _RunCommand, but
the real work runs inside multiprocessing.Process children, so on
macOS they call the real _RunCommand, crash on a nonexistent command,
and never report a result, failing 6 of 7 tests.

PR GoogleCloudPlatform#6055 fixed this exact mechanism for real pkb.py runs by forcing
'fork' on macOS inside Main(), but the unit test suite never calls
Main(). Apply the same fix at module level in the shared
PkbCommonTestCase base, so any multiprocessing-based test gets the
same protection, not just this one file.

Fixes GoogleCloudPlatform#7243

This branch has not been deployed

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

Labels

None yet

1 participant