Closed
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 generatesid
usingAtomicInteger nextId
field inRequest
which starts with 0.WebSocketService.sendAsync
saves this id inrequestForId
map for further mapping of arriving response. - User without waiting for response asynchronously creates batch with
Web3j.newBatch
adds some requests in it and sends it.WebSocketService.sendBatchAsync
generates substitute id usingAtomicInteger nextBatchId
which also starts with 0 and puts it in the samerequestForId
map overwriting first request which was there. - Response for first request arrives in
onWebSocketMessage
method and fails with ClassCastException because it expects to find thereWebSocketRequest
by id 0, but instead getsWebSocketRequests
. - Response for batch request arrives and
WebSocketService.getAndRemoveRequest
throws 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)