Fix shared-state/thread-safety bug in Payment.capture() (mutable default argument)#331
Open
vinothksekar wants to merge 1 commit into
Open
Conversation
capture() used a mutable default argument (data={}) and mutated it
(data['amount'] = amount). The default dict is created once and shared
across every call, every Client instance and every thread, so:
* state leaks between calls: after capture(id, 4242) with no data, the
function's default is permanently {'amount': 4242}; and
* on a Client shared across threads (the documented usage) concurrent
capture() calls race on the shared dict and can post one payment's
amount for another payment.
Fixed by using data=None and building a fresh dict per call (also avoids
mutating a caller-supplied dict). Removes the # nosemgrep suppression
that was hiding the mutable-default warning.
Refs razorpay#134.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Payment.capture()uses a mutable default argument (data={}) and mutates it (data['amount'] = amount). Because the default{}is created once and shared across every call, everyClientinstance, and every thread, captured state leaks between calls and — on aClientshared across threads (the documented usage) — concurrent captures race on the shared dict.This was previously flagged in #134 and silenced with a
# nosemgrepsuppression rather than fixed.Impact
client.payment.capture(id, 4242)with nodata, the function's default is permanently{'amount': 4242}.capture()concurrently can post one payment's amount for a different payment. In a minimal 2-thread reproduction (25 captures each, amounts 100 vs 999), 47 of 50 calls posted the wrong amount. After the fix: 0 of 50.Fix
Use
data=Noneand build a fresh dict per call (this also avoids mutating a caller-supplied dict). The now-unnecessary# nosemgrepsuppression is removed.Tests
Added two regression tests to
tests/test_client_payment.py:test_payment_capture_does_not_leak_state_into_defaulttest_payment_capture_does_not_mutate_caller_dictFull suite:
159 passed, 1 skipped.Refs #134.