Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batch requests over WebSocketService are overwritten by simple requests if send at about the same time #2127

Open
Shohou opened this issue Dec 16, 2024 · 0 comments · May be fixed by #2128
Open
Labels
bug A bug in behaviour or functionality

Comments

@Shohou
Copy link
Contributor

Shohou commented Dec 16, 2024

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:

  1. User sends simple standalone request by creating it with one of Web3j methods, Web3j.ethBlockNumber() for example. It generates id using AtomicInteger nextId field in Request which starts with 0. WebSocketService.sendAsync saves this id in requestForId map for further mapping of arriving response.
  2. User without waiting for response asynchronously creates batch with Web3j.newBatch adds some requests in it and sends it. WebSocketService.sendBatchAsync generates substitute id using AtomicInteger nextBatchId which also starts with 0 and puts it in the same requestForId map overwriting first request which was there.
  3. Response for first request arrives in onWebSocketMessage method and fails with ClassCastException because it expects to find there WebSocketRequest by id 0, but instead gets WebSocketRequests.
  4. 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)
@Shohou Shohou added the bug A bug in behaviour or functionality label Dec 16, 2024
Shohou pushed a commit to Shohou/web3j that referenced this issue Dec 16, 2024
@Shohou Shohou linked a pull request Dec 16, 2024 that will close this issue
3 tasks
Shohou pushed a commit to Shohou/web3j that referenced this issue Dec 16, 2024
Shohou pushed a commit to Shohou/web3j that referenced this issue Dec 17, 2024
…tch ids, take the one used for requests

Signed-off-by: Dmitry Shohov <[email protected]>
Shohou added a commit to Shohou/web3j that referenced this issue Dec 17, 2024
…tch ids, take the one used for requests

Signed-off-by: Dmitry Shohov <[email protected]>
Shohou added a commit to Shohou/web3j that referenced this issue Dec 18, 2024
…ch ids, take the one used for requests

Signed-off-by: Dmitry Shohov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug in behaviour or functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant