Closed ArshanKhanifar closed 2 years ago
Hey @ArshanKhanifar. It looks like the PR at #2409 addresses this as well. Linking here to track this there as well. I'm in the middle of writing tests for sync and tweaking some things on the sync side but that PR should be good to go soon. Please feel free to join the conversation there.
sorry which PR, I think you accidentally linked this issue haha.
hah, sorry... I fixed it
For clarification, the sync code will probably change to resemble the async code a bit more except it should be more straightforward / no need for the asynccontextmanager
Understood, thanks.
Resolved in #2409 - still needs to be ported to v5
but was merged into web3.py v6
beta and should be out in that next release.
I can see multiple similar issues (#1847 , #2409) but I guess they're not merged in yet. The issue persists even with the non-async Web3 module with no middlewares. Here's my reproducible example where this issue happens every single time.
pip freeze
outputWhat was wrong?
Segfault when multi-threading with multiple RPCs:
from web3 import Web3
def get_block_number(rpc: str): w3 = Web3(Web3.HTTPProvider(rpc)) block_number = w3.eth.get_block_number() return block_number
def get_bn_chains(): with ThreadPoolExecutor(max_workers=len(rpcs)) as exc: tasks = [exc.submit(get_block_number, rpc) for rpc in rpcs] results = [r.result() for r in tasks] print(f"results: {results}") return results
if name == 'main': rpcs = [ "https://api.mycryptoapi.com/eth", "https://api.avax.network/ext/bc/C/rpc", "https://rpc.ftm.tools", "https://polygon-rpc.com", "https://arb1.arbitrum.io/rpc", "https://mainnet.optimism.io", "https://bsc-dataseed1.binance.org", "https://mainnet.aurora.dev", "https://eth.bd.evmos.org:8545", "https://http-mainnet.hecochain.com", "https://evm.cronos.org", "https://mainnet.boba.network", "https://rpc.gnosischain.com", "https://rpc.gnosischain.com", "https://evm.kava.io", "https://rpc.callisto.network" ]
/Users/arshankhanifar/miniconda3/envs/bt_defi_tx/bin/python /Users/arshankhanifar/tpc_aux_services/etl_services/bt_defi_tx/samples/find_segfault.py first round results: [15303145, 18385655, 44492119, 31667322, 19742334, 18360836, 20268554, 71583519, 2655226, 17696348, 4068760, 764415, 23595692, 23595687, 1025761, 10469308] second round
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
import threading from concurrent.futures import ThreadPoolExecutor from typing import Callable
import web3._utils.request from web3 import Web3
lock = threading.Lock()
def locked(fn: Callable) -> Callable: def inner(*args, *kwargs): with lock: return fn(args, **kwargs)
web3._utils.request.cache_session = locked(web3._utils.request.cache_session) web3._utils.request._get_session = locked(web3._utils.request._get_session)
def get_block_number(rpc: str): w3 = Web3(Web3.HTTPProvider(rpc)) block_number = w3.eth.get_block_number() return block_number
def get_bn_chains(): with ThreadPoolExecutor(max_workers=len(rpcs)) as exc: tasks = [exc.submit(get_block_number, rpc) for rpc in rpcs] results = [r.result() for r in tasks] print(f"results: {results}") return results
if name == 'main': rpcs = [ "https://api.mycryptoapi.com/eth", "https://api.avax.network/ext/bc/C/rpc", "https://rpc.ftm.tools", "https://polygon-rpc.com", "https://arb1.arbitrum.io/rpc", "https://mainnet.optimism.io", "https://bsc-dataseed1.binance.org", "https://mainnet.aurora.dev", "https://eth.bd.evmos.org:8545", "https://http-mainnet.hecochain.com", "https://evm.cronos.org", "https://mainnet.boba.network", "https://rpc.gnosischain.com", "https://rpc.gnosischain.com", "https://evm.kava.io", "https://rpc.callisto.network" ]
/Users/arshankhanifar/miniconda3/envs/bt_defi_tx/bin/python /Users/arshankhanifar/tpc_aux_services/etl_services/bt_defi_tx/samples/find_segfault.py first round results: [15303155, 18385721, 44492225, 31667386, 19742473, 18360979, 20268599, 71583620, 2655299, 17696393, 4068829, 764419, 23595719, 23595719, 1025782, 10469321] second round results: [15303155, 18385721, 44492225, 31667386, 19742473, 18360980, 20268599, 71583621, 2655300, 17696394, 4068829, 764419, 23595719, 23595719, 1025783, 10469321]
Process finished with exit code 0