-
- Notifications
You must be signed in to change notification settings - Fork 1.8k
Closed
Labels
bugA bug in behaviour or functionalityA bug in behaviour or functionality
Description
Request ids generated for batch requests clash with request ids of normal standalone requests
WebSocketService in sendBatchAsync method overwrites id of first request in batch and puts it int requestForId map. Problem is the new id is generated using AtomicInteger called nextBatchId which is different than AtomicInteger used for id generation in Request. So what happens:
- User sends simple standalone request by creating it with one of Web3j methods,
Web3j.ethBlockNumber()for example. It generatesidusingAtomicInteger nextIdfield inRequestwhich starts with 0.WebSocketService.sendAsyncsaves this id inrequestForIdmap for further mapping of arriving response. - User without waiting for response asynchronously creates batch with
Web3j.newBatchadds some requests in it and sends it.WebSocketService.sendBatchAsyncgenerates substitute id usingAtomicInteger nextBatchIdwhich also starts with 0 and puts it in the samerequestForIdmap overwriting first request which was there. - Response for first request arrives in
onWebSocketMessagemethod and fails with ClassCastException because it expects to find thereWebSocketRequestby id 0, but instead getsWebSocketRequests. - Response for batch request arrives and
WebSocketService.getAndRemoveRequestthrows IOException with message "Received reply for unexpected request id: 0"
Steps To Reproduce
Steps described in description.
Expected behavior
I expect it not to fail with exceptions and responses mapped correctly with corresponding requests
Actual behavior
Two exceptions are thrown, and both requests are never completed, not even by timeout
Environment
- Web3j version - 4.12.2
Additional context
Exceptions:
2024-12-16 02:29:40,407 ERROR [org.web3j.protocol.websocket.WebSocketClient] (WebSocketConnectReadThread-75) Failed to process message '[{"jsonrpc":"2.0","id":0,"result":{long body of batch request}}]' from server wss://holesky.infura.io/ws/v3/xxxxxx: java.lang.ClassCastException: class org.web3j.protocol.websocket.WebSocketRequest cannot be cast to class org.web3j.protocol.websocket.WebSocketRequests (org.web3j.protocol.websocket.WebSocketRequest and org.web3j.protocol.websocket.WebSocketRequests are in unnamed module of loader io.quarkus.bootstrap.runner.RunnerClassLoader @6193b845) at org.web3j.protocol.websocket.WebSocketService.processBatchRequestReply(WebSocketService.java:309) at org.web3j.protocol.websocket.WebSocketService.onWebSocketMessage(WebSocketService.java:281) at org.web3j.protocol.websocket.WebSocketService$1.onMessage(WebSocketService.java:155) at org.web3j.protocol.websocket.WebSocketClient.lambda$onMessage$0(WebSocketClient.java:52) at java.base/java.util.Optional.ifPresent(Optional.java:178) at org.web3j.protocol.websocket.WebSocketClient.onMessage(WebSocketClient.java:49) at org.java_websocket.client.WebSocketClient.onWebsocketMessage(WebSocketClient.java:644) at org.java_websocket.drafts.Draft_6455.processFrameText(Draft_6455.java:986) at org.java_websocket.drafts.Draft_6455.processFrame(Draft_6455.java:910) at org.java_websocket.WebSocketImpl.decodeFrames(WebSocketImpl.java:402) at org.java_websocket.WebSocketImpl.decode(WebSocketImpl.java:234) at org.java_websocket.client.WebSocketClient.run(WebSocketClient.java:527) at java.base/java.lang.Thread.run(Thread.java:1583) 2024-12-16 02:29:40,422 ERROR [org.web3j.protocol.websocket.WebSocketClient] (WebSocketConnectReadThread-75) Failed to process message '{"jsonrpc":"2.0","id":0,"result":"0x2cf161"}' from server wss://holesky.infura.io/ws/v3/xxxxx: java.io.IOException: Received reply for unexpected request id: 0 at org.web3j.protocol.websocket.WebSocketService.getAndRemoveRequest(WebSocketService.java:433) at org.web3j.protocol.websocket.WebSocketService.processRequestReply(WebSocketService.java:292) at org.web3j.protocol.websocket.WebSocketService.onWebSocketMessage(WebSocketService.java:279) at org.web3j.protocol.websocket.WebSocketService$1.onMessage(WebSocketService.java:155) at org.web3j.protocol.websocket.WebSocketClient.lambda$onMessage$0(WebSocketClient.java:52) at java.base/java.util.Optional.ifPresent(Optional.java:178) at org.web3j.protocol.websocket.WebSocketClient.onMessage(WebSocketClient.java:49) at org.java_websocket.client.WebSocketClient.onWebsocketMessage(WebSocketClient.java:644) at org.java_websocket.drafts.Draft_6455.processFrameText(Draft_6455.java:986) at org.java_websocket.drafts.Draft_6455.processFrame(Draft_6455.java:910) at org.java_websocket.WebSocketImpl.decodeFrames(WebSocketImpl.java:402) at org.java_websocket.WebSocketImpl.decode(WebSocketImpl.java:234) at org.java_websocket.client.WebSocketClient.run(WebSocketClient.java:527) at java.base/java.lang.Thread.run(Thread.java:1583) Metadata
Metadata
Assignees
Labels
bugA bug in behaviour or functionalityA bug in behaviour or functionality