Skip to content

Conversation

@fselmo
Copy link
Collaborator

@fselmo fselmo commented May 20, 2025

What was wrong?

Closes #3613

How was it fixed?

  • bugfix Use ContextVar rather than global flag to toggle batching context. This should better isolate the batching context to its own thread-safe context.
  • Add test for concurrent execution of many requests at the same time as a batch request
  • bugfix: Found an issue with PersistentConnectionProvider batching requests where we hadn't yet separated the send and receive functions to deterministically match responses with their requests by id. Fixed that in this PR as well.
  • bugfix: threading id means nothing for async, use loop id
  • test fix: gather sessions deterministically to check order

🎉 🎉 Bonus 🎉 🎉

I was able to switch our core tests to run via pytest-xdist insanely fast by sorting any set parametrized params or using tuples / lists instead, where appropriate. I am kind of shocked we hadn't gone this route before but this is a pretty significant update.

Todo:

Cute Animal Picture

Screenshot 2025-05-20 at 18 59 53

fselmo added 2 commits May 19, 2025 16:41
- Give some thread safety to batch request context by using
  ``ContextVar`` instead of setting a global state. This actually
  simplifies the logic quite a bit since we don't need to manage
  the batch context state at all.

- Removes ``_is_batching`` from provider classes.
@fselmo fselmo force-pushed the context-vars-for-batching-context branch 2 times, most recently from cde06fb to 7e20530 Compare May 20, 2025 23:54
fselmo added a commit to fselmo/web3.py that referenced this pull request May 21, 2025
fselmo added a commit to fselmo/web3.py that referenced this pull request May 21, 2025
@fselmo fselmo force-pushed the context-vars-for-batching-context branch from c3cf179 to 69ee30c Compare May 21, 2025 00:10
@fselmo fselmo marked this pull request as ready for review May 21, 2025 01:00
@fselmo fselmo requested review from kclowes, pacrob and reedsa May 21, 2025 01:00
fselmo added a commit to fselmo/web3.py that referenced this pull request May 21, 2025
@fselmo fselmo force-pushed the context-vars-for-batching-context branch from 69ee30c to bd7e3c7 Compare May 21, 2025 04:41
fselmo added a commit to fselmo/web3.py that referenced this pull request May 21, 2025
@fselmo fselmo force-pushed the context-vars-for-batching-context branch from bd7e3c7 to d5c9ec7 Compare May 21, 2025 05:07
fselmo added a commit to fselmo/web3.py that referenced this pull request May 21, 2025
@fselmo fselmo force-pushed the context-vars-for-batching-context branch from d5c9ec7 to 179a210 Compare May 21, 2025 05:12
fselmo added 7 commits May 22, 2025 09:23
- Remove sets from parametrization since they can be in random order.
  Either use sorted or change to tuples / lists.

- run with ``-n auto`` and set ``--maxprocesses=15`` for core test runs.
- Separate the batch send and batch receive functions for persistent
  connection providers in order to deterministically cache the request
  information and be able to retrieve it without any request id guesswork.

- Remove the idea of a batching request counter / id 🎉

- Correct typing expectations for ``BatchRequestInformation``
- turn off concurrent batch requests test for LegacyWebsocketProvide
- turn off batching test for EthereumTesterProvider
- Asynchronous code making use of a threading ``id`` for the cache key
  doesn't make as much sense as using a loop ``id``. Running the core
  tests with ``pytest-xdist`` runners actually seems to have identified
  a bug in this code that was previously well hidden. Replace the threading
  id with loop id when generating unique cache keys in the http session
  manager.
@fselmo fselmo force-pushed the context-vars-for-batching-context branch from 1542d9d to 3a05c6a Compare May 22, 2025 15:30
Copy link
Collaborator

@kclowes kclowes left a comment

Choose a reason for hiding this comment

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

LGTM! I left one nit, feel free to take or leave!

Copy link
Contributor

@reedsa reedsa left a comment

Choose a reason for hiding this comment

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

lgtm

- Now that we separate send and receive for batching, we no longer have
  to create a workaround for response formatters to contract calls. We
  pass all response formatters together as we do with regular contract
  calls, to be applited to the response as it comes back.

- from comment on PR ethereum#3705: move skip marker to decorator
@fselmo fselmo force-pushed the context-vars-for-batching-context branch from 29e517c to 1289343 Compare May 22, 2025 16:46
@fselmo fselmo merged commit 89d8145 into ethereum:main May 22, 2025
85 checks passed
fselmo added a commit that referenced this pull request May 22, 2025
@fselmo fselmo deleted the context-vars-for-batching-context branch May 22, 2025 16:53
simone1999 added a commit to IceCreamSwapCom/Web3Python that referenced this pull request May 24, 2025
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.

New batching feature completely breaks thread safety

4 participants