Fix SystemError in _DeferredCall.get() under GC pressure#38355
Fix SystemError in _DeferredCall.get() under GC pressure#38355arr2036 wants to merge 1 commit intoapache:masterfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
|
Seeing about 4K crashes per day at a 1000PPS rate with ~12 workers. |
There was a problem hiding this comment.
Code Review
This pull request replaces a generator expression with a list comprehension in _DeferredCall.get() to prevent a CPython internal race condition that can cause a SystemError. It also introduces a new test suite, DeferredCallTest, to ensure correct behavior across various argument types. A minor correction was suggested for a method name typo in the test class docstring.
79dafb9 to
f343f1c
Compare
CPython builds the argument tuple incrementally via _PyTuple_Resize when unpacking a generator: f(*(gen)). _PyTuple_Resize guards that Py_REFCNT(v) == 1 before resizing a non-empty tuple. Under sustained allocation pressure a GC cycle can run between generator yields and temporarily increment the refcount on the partial tuple, causing _PyTuple_Resize to call PyErr_BadInternalCall(). Change the generator expression to a list comprehension. CPython builds the list first and passes it to CALL_FUNCTION_EX, which takes the PySequence_Fast list path and never calls _PyTuple_Resize.
f343f1c to
6628a3d
Compare
|
Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment |
|
assign set of reviewers |
|
Assigning reviewers: R: @tvalentyn for label python. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
Summary
_DeferredCall.get()insdk_worker.pyused a generator expression in the argument unpack position:Under sustained load this raises
SystemError: Objects/tupleobject.c:927: bad argument to internal function.Root cause
CPython builds the argument tuple incrementally when unpacking a generator, calling
_PyTuple_Resizeafter each yield to grow the tuple by one slot._PyTuple_Resizeguards:Between generator yields, a GC cycle can run (triggered by allocations inside
arg.get()). The GC can increment the refcount on the partially-built tuple. When_PyTuple_Resizenext fires,Py_REFCNT(v) != 1is true and it raisesSystemError.The call site is
bundle_processor.py:637(state.commit()->to_await.get()->_DeferredCall.get()), which is on the hot path for every bundle that commits state. High-throughput streaming pipelines hit this continuously.Observed stack trace (Python 3.11, Beam 2.73.0, production worker)
Fix
Change the generator expression to a list comprehension:
CPython materialises the full list before the call.
CALL_FUNCTION_EXreceives a list and takes thePySequence_Fastpath, which for a list is a no-op (just incref). The argument tuple is allocated once at its final size viaPyTuple_New._PyTuple_Resizeis never called._DeferredCall.__init__already wraps non-_Futureargs eagerly, so materialising the list before the call preserves existing semantics.Why
_DeferredCall.wait()is unaffectedwait()also uses a generator expression (all(arg.wait(timeout) for arg in self._args)) but passes it toall(), which is a C built-in that consumes the generator without building a resizable tuple. The crash path is specific to*(gen)unpacking in a function call.Tests
Added
DeferredCallTestinsdk_worker_test.pycovering single-arg, multi-arg, non-Future arg wrapping, mixed Future+value, zero-arg, and return value type preservation.The crash itself requires specific CPython internal timing (GC cycle between
PySequence_Tuple's calls to the generator's__next__, against the C-level partial tuple) and cannot be triggered deterministically from pure Python without a C extension or a CPython debug build. The argument for the fix rests on the CPython source and the observed production stack trace above.