Need help understand error "Filter id 1 does not exist"

I am facing below error when subscribing to smart contract events. After starting the application am able to listen to initial events before it gets into a state where I start getting below exception continuously . Any ideas what going on? I tried with both Http and Websocket, with Smart Contract Wrapper and directly subscribing transactionFlowable().subscribe. Works fine with ethers.js

ERROR pool-9-thread-4 Filter Error sending request

org.web3j.protocol.core.filters.FilterException: Invalid request: Filter id 1 does not exist.

at org.web3j.protocol.core.filters.Filter.throwException(Filter.java:190)

at org.web3j.protocol.core.filters.Filter.pollFilter(Filter.java:140)

at org.web3j.protocol.core.filters.Filter.lambda$run$0(Filter.java:92)

at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)

at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)

at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)

at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)

at java.util.concurrent.ThreadPoolExecutor$Wor

Please can you provide some of the Java code you are running so we have some additional context?

This simple test code also gets into this state after sometime. May be easier to narrow down based on this. This is listening on events generated by a smart contract.

import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;

public class TestSimpleListener {

public static void main(String[] arg) {
    Web3j web3j = Web3j.build(new HttpService("Https://node.XXX.io/9999"));

    web3j.transactionFlowable().subscribe(tx -> {
        if (tx.getTo() != null && tx.getTo().equalsIgnoreCase(“0x3YY…...")) {
            System.out.println("\n Matched:  New tx: id={" + tx.getHash() + "}, block={" + tx.getBlockHash() + "}, from={" + tx.getFrom() + "}, to={" + tx.getTo() + "}, value={" + tx.getValue().intValue() + "}"
                    + "input={" + tx.getInput() + "}\n ");


        } else {
            System.out.println("\n Not matched: id={" + tx.getHash() + "}, to={" + tx.getTo() + "}, from="+tx.getFrom()+"}\n");
        }
    });


}

}
Initially Works and later continuously shows the exception—>

13:23:10.567 [pool-2-thread-4] DEBUG org.web3j.protocol.http.HttpService - Server: nginx/1.18.0 (Ubuntu)
13:23:10.567 [pool-2-thread-4] DEBUG org.web3j.protocol.http.HttpService - Date: Mon, 10 May 2021 20:23:11 GMT
13:23:10.567 [pool-2-thread-4] DEBUG org.web3j.protocol.http.HttpService - Content-Type: application/json; charset=utf-8
13:23:10.567 [pool-2-thread-4] DEBUG org.web3j.protocol.http.HttpService - Transfer-Encoding: chunked
13:23:10.567 [pool-2-thread-4] DEBUG org.web3j.protocol.http.HttpService - Connection: keep-alive
13:23:10.567 [pool-2-thread-4] DEBUG org.web3j.protocol.http.HttpService - Vary: Accept-Encoding
13:23:10.568 [pool-2-thread-4] DEBUG org.web3j.protocol.http.HttpService -
13:23:10.568 [pool-2-thread-4] DEBUG org.web3j.protocol.http.HttpService - {“jsonrpc”:“2.0”,“result”:{“author”:“0x6be02d1d3665660d22ff9624b7be0551ee1ac91b”,“difficulty”:“0x0”,“extraData”:“0x”,“gasLimit”:“0xe4e1c0”,“gasUsed”:“0x0”,“hash”:“0x29f4ee043e33d7b33d7e59c7dcb3a327f65f2e6487f9fa1d85ee3fcd67067174”,“logsBloom”:”0x0000…

….
….“id”:98}

13:23:10.568 [pool-2-thread-4] DEBUG org.web3j.protocol.http.HttpService - ← END HTTP (1457-byte body)
13:23:25.304 [pool-2-thread-1] DEBUG org.web3j.protocol.http.HttpService - → POST https://node.XXX.io/1281
13:23:25.304 [pool-2-thread-1] DEBUG org.web3j.protocol.http.HttpService - Content-Type: application/json; charset=utf-8
13:23:25.304 [pool-2-thread-1] DEBUG org.web3j.protocol.http.HttpService - Content-Length: 75
13:23:25.304 [pool-2-thread-1] DEBUG org.web3j.protocol.http.HttpService -
13:23:25.304 [pool-2-thread-1] DEBUG org.web3j.protocol.http.HttpService - {“jsonrpc”:“2.0”,“method”:“eth_getFilterChanges”,“params”:[“0x01”],“id”:99}
13:23:25.304 [pool-2-thread-1] DEBUG org.web3j.protocol.http.HttpService - → END POST (75-byte body)
13:23:25.317 [pool-2-thread-1] DEBUG org.web3j.protocol.http.HttpService - ← 200 OK https://node.XXX.io/1281 (12ms)
13:23:25.317 [pool-2-thread-1] DEBUG org.web3j.protocol.http.HttpService - Server: nginx/1.18.0 (Ubuntu)
13:23:25.317 [pool-2-thread-1] DEBUG org.web3j.protocol.http.HttpService - Date: Mon, 10 May 2021 20:23:26 GMT
13:23:25.317 [pool-2-thread-1] DEBUG org.web3j.protocol.http.HttpService - Content-Type: application/json; charset=utf-8
13:23:25.317 [pool-2-thread-1] DEBUG org.web3j.protocol.http.HttpService - Content-Length: 90
13:23:25.317 [pool-2-thread-1] DEBUG org.web3j.protocol.http.HttpService - Connection: close
13:23:25.317 [pool-2-thread-1] DEBUG org.web3j.protocol.http.HttpService -
13:23:25.317 [pool-2-thread-1] DEBUG org.web3j.protocol.http.HttpService - {“jsonrpc”:“2.0”,“error”:{“code”:-32603,“message”:“Filter id 1 does not exist.”},“id”:99}

13:23:25.317 [pool-2-thread-1] DEBUG org.web3j.protocol.http.HttpService - ← END HTTP (90-byte body)
13:23:25.328 [pool-2-thread-1] ERROR org.web3j.protocol.core.filters.Filter - Error sending request
org.web3j.protocol.core.filters.FilterException: Invalid request: Filter id 1 does not exist.
at org.web3j.protocol.core.filters.Filter.throwException(Filter.java:190)
at org.web3j.protocol.core.filters.Filter.pollFilter(Filter.java:140)
at org.web3j.protocol.core.filters.Filter.lambda$run$0(Filter.java:92)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExec

Thanks - the code looks fine for connecting via the HttpClient. Is there an single Ethereum node that it’s connecting to at that URL?

Yes. This is a single node of a test net deployed. We tested with Eitherjs code and with that it worked fine.

Any idea what could be the reason? Compatibility issues ? I am using 4.8.4

Is there an earlier message like the above that isn’t followed by the connection being closed? Behind the scenes, Web3j is polling for changes using a filter with an id of 1. It looks like at some point that filter id stope being valid. Hence it would be useful to know if there’s an request for this filter id that is successful.

I suspect ngix Server is causing the HttpClient to gracefully close the connection. If testing without ngix is not an option, the code snippet below allows passing custom HttpClient.

I don’t have ngix setup, we can try:

  1. Set proxy in the commented-out part.

  2. Configure HttpClient to include http_proxy_version in the header Module ngx_http_upstream_module

    `public class HttpServiceTest {

    private static final Logger log = LoggerFactory.getLogger(“HttpLogger”);

    private static HttpLoggingInterceptor getLoggingInterceptor() {
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor(log::debug);
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    return logging;
    }

    public static void main(String[] arg) {

     final OkHttpClient client = new OkHttpClient.Builder()
             .addInterceptor(getLoggingInterceptor())
             //.proxy()
             .build();
    
     Web3j web3j = Web3j.build(new HttpService("http://localhost:9545", client));
    
     web3j.transactionFlowable().subscribe(tx -> {
         if (tx.getTo() != null && tx.getTo().equalsIgnoreCase("")) {
         System.out.println("\n Matched:  New tx: id={" + tx.getHash() + "}, block={" + tx.getBlockHash() + "}, from={" + tx.getFrom() + "}, to={" + tx.getTo() + "}, value={" + tx.getValue().intValue() + "}"
                 + "input={" + tx.getInput() + "}\n ");
     } else {
         System.out.println("\n Not matched: id={" + tx.getHash() + "}, to={" + tx.getTo() + "}, from="+tx.getFrom()+"}\n");
     }
    

    });

}}`

yes I see multiple calls with param as 0x01 with incrementing value of id successful before the error starts. Getting invalidated makes sense. What put me off was it working using etherjs. Will check more.

{“jsonrpc”:“2.0”,“method”:“eth_getFilterChanges”,“params”:[“0x01”],

Thanks for your inputs. Appreciate it. Will investigate this part and will try to isolate.

Forgot to update it here earlier. We had investigated and found the issue with a timeout (based on number of blocks) on the blockchain node. For our test we updated the code and rebuilt the node. Worked fine after.

Thanks for all the inputs and help.

1 Like

Many thanks for the update Udyan.

We have seen another timeout issue from another user recently, I would appreciate it if you can share what code has been updated?