Skip to content

Conversation

@haukex
Copy link

@haukex haukex commented Nov 4, 2025

Hi, I would like to propose that the transaction() helper gets an additional argument max_tries (perhaps even with a default value) to prevent infinite loops, as can happen fairly easily by accident:

vk = Valkey()
def incr(pipe: Pipeline):
    next_value = int(pipe.get('key')) + 1
    pipe.multi()
    vk.set('key', next_value)  # WRONG!
vk.transaction(incr, 'key', max_tries=100)  # would eventually raise error instead of infinite loop

If you like this change, I can also suggest some documentation updates etc. if you like.

@mkmkme
Copy link
Collaborator

mkmkme commented Nov 4, 2025

Hey,

Thanks for your contribution! Could you please sign off your commit? You can find how to do it here, in the failed check report.

@mkmkme
Copy link
Collaborator

mkmkme commented Nov 4, 2025

Also I would really appreciate if you could provide a test for your changes

@haukex
Copy link
Author

haukex commented Nov 7, 2025

@mkmkme Thanks, I've amended and force pushed the commit to include documentation and tests. Edit: Also rebased. Edit 2: And fixed Signed-off-by. Edit 3: And a final update with a minor improvement to the tests. Done editing for now 😄

@haukex haukex force-pushed the trans-inf-loop branch 3 times, most recently from 6b9b7e2 to a2c31d3 Compare November 7, 2025 10:15
This new `max_tries` argument in the `transaction` helper allows one to
more easily prevent (accidental) infinite loops. Commit includes
documentation and tests.

Signed-off-by: Hauke Daempfling <haukex@zero-g.net>
mkmkme
mkmkme previously approved these changes Nov 8, 2025
Copy link
Collaborator

@mkmkme mkmkme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @haukex !
This is a very well written PR that I am happy to approve!

@bogdanp05 @amirreza8002 could you also have a look from your side and raise your concerns if you have any? For me it looks good to be merged.

@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is 8.16327% with 45 lines in your changes missing coverage. Please review.
✅ Project coverage is 59.39%. Comparing base (c291080) to head (baffe45).

Files with missing lines Patch % Lines
tests/test_asyncio/test_pipeline.py 10.00% 18 Missing ⚠️
tests/test_pipeline.py 10.00% 18 Missing ⚠️
valkey/client.py 0.00% 5 Missing ⚠️
valkey/asyncio/client.py 0.00% 4 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (c291080) and HEAD (baffe45). Click for more details.

HEAD has 303 uploads less than BASE
Flag BASE (c291080) HEAD (baffe45)
305 2
Additional details and impacted files
@@             Coverage Diff             @@
##             main     #240       +/-   ##
===========================================
- Coverage   76.28%   59.39%   -16.89%     
===========================================
  Files         129      129               
  Lines       33906    33955       +49     
===========================================
- Hits        25864    20167     -5697     
- Misses       8042    13788     +5746     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@amirreza8002
Copy link
Contributor

i haven't worked with this method, but it looks ok, tho added a comment

Signed-off-by: Hauke Daempfling <haukex@zero-g.net>
@haukex
Copy link
Author

haukex commented Nov 12, 2025

After thinking about this a bit more, I think I actually prefer the updated version if max_tries and tries > max_tries: that raises an error immediately if a negative value is passed. This seems more logical to me since a negative number of tries doesn't make sense - and could even be considered worth raising a ValueError. Plus, it leaves the door open for perhaps assigning negative numbers a different meaning in the future instead of immediately assigning it the meaning "an infinite number of tries". But this is still just my opinion and I'll defer to what you prefer.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants