Skip to content

Commit

Permalink
Fix semaphore on wrong loop edge case (#83)
Browse files Browse the repository at this point in the history
* fix: edge case with semaphore attached to wrong loop

* chore: bump version
  • Loading branch information
BobTheBuidler authored Sep 26, 2023
1 parent 160b537 commit 4dc923a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
8 changes: 4 additions & 4 deletions multicall/call.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
from web3 import Web3

from multicall import Signature
from multicall.constants import Network, w3, ASYNC_SEMAPHORE
from multicall.constants import Network, w3
from multicall.exceptions import StateOverrideNotSupported
from multicall.loggers import setup_logger
from multicall.utils import (chain_id, get_async_w3, run_in_subprocess,
state_override_supported)
from multicall.utils import (_get_semaphore, chain_id, get_async_w3,
run_in_subprocess, state_override_supported)

logger = setup_logger(__name__)

Expand Down Expand Up @@ -112,7 +112,7 @@ async def coroutine(self, args: Optional[Any] = None, _w3: Optional[Web3] = None
if self.state_override_code and not state_override_supported(_w3):
raise StateOverrideNotSupported(f'State override is not supported on {Network(chain_id(_w3)).__repr__()[1:-1]}.')

async with ASYNC_SEMAPHORE:
async with _get_semaphore():
output = await get_async_w3(_w3).eth.call(
*await run_in_subprocess(
prep_args,
Expand Down
2 changes: 1 addition & 1 deletion multicall/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,4 @@ class Network(IntEnum):

# NOTE: If we run too many async calls at once, we'll have memory issues.
# Feel free to increase this with the "MULTICALL_CALL_SEMAPHORE" env var if you know what you're doing.
ASYNC_SEMAPHORE = asyncio.Semaphore(int(os.environ.get("MULTICALL_CALL_SEMAPHORE", 1000)))
ASYNC_SEMAPHORE = int(os.environ.get("MULTICALL_CALL_SEMAPHORE", 1000))
9 changes: 4 additions & 5 deletions multicall/multicall.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
from web3 import Web3

from multicall import Call
from multicall.constants import (ASYNC_SEMAPHORE, GAS_LIMIT,
MULTICALL2_ADDRESSES, MULTICALL3_ADDRESSES,
MULTICALL3_BYTECODE, w3)
from multicall.constants import (GAS_LIMIT, MULTICALL2_ADDRESSES,
MULTICALL3_ADDRESSES, MULTICALL3_BYTECODE)
from multicall.loggers import setup_logger
from multicall.utils import (await_awaitable, chain_id, gather,
from multicall.utils import (_get_semaphore, await_awaitable, chain_id, gather,
run_in_subprocess, state_override_supported)

logger = setup_logger(__name__)
Expand Down Expand Up @@ -82,7 +81,7 @@ async def fetch_outputs(self, calls: List[Call], ConnErr_retries: int = 0, id: s
if calls is None:
calls = self.calls

async with ASYNC_SEMAPHORE:
async with _get_semaphore():
try:
args = await run_in_subprocess(get_args, calls, self.require_success)
if self.require_success is True:
Expand Down
8 changes: 7 additions & 1 deletion multicall/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import asyncio
from concurrent.futures import ProcessPoolExecutor
from functools import lru_cache
from typing import Any, Awaitable, Callable, Coroutine, Dict, Iterable

import eth_retry
Expand All @@ -9,7 +10,8 @@
from web3.eth import AsyncEth
from web3.providers.async_base import AsyncBaseProvider

from multicall.constants import AIOHTTP_TIMEOUT, NUM_PROCESSES, NO_STATE_OVERRIDE
from multicall.constants import (AIOHTTP_TIMEOUT, NO_STATE_OVERRIDE,
NUM_PROCESSES)

chainids: Dict[Web3,int] = {}

Expand Down Expand Up @@ -94,3 +96,7 @@ def state_override_supported(w3: Web3) -> bool:
if chain_id(w3) in NO_STATE_OVERRIDE:
return False
return True

@lru_cache(maxsize=1)
def _get_semaphore() -> asyncio.Semaphore:
return asyncio.Semaphore()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "multicall"
version = "0.7.7"
version = "0.7.8"
description = "aggregate results from multiple ethereum contract calls"
authors = ["banteg"]

Expand Down

0 comments on commit 4dc923a

Please sign in to comment.