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

It looks like sendAndGet() is not thread-safe #25

Open
linyufly opened this issue Jun 1, 2021 · 1 comment
Open

It looks like sendAndGet() is not thread-safe #25

linyufly opened this issue Jun 1, 2021 · 1 comment

Comments

@linyufly
Copy link

linyufly commented Jun 1, 2021

Hi folks,

I tried to use two threads to make different contract calls. Sometimes their responses may interfere with each other.

For this test, I defined the following class.

    private class ContractCallThread extends Thread {
        public ContractCallThread(ContractCall contract, String method, Type<?>... args) {
            this.contract = contract;
            this.method = method;
            this.args = args;
        }

        public void run() {
            this.result = this.contract.call(this.method, this.args).sendAndGet();
        }

        public String result() {
            return this.result;
        }

        private ContractCall contract = null;
        private String method = null;
        private Type<?>[] args = null;
        private String result = null;
    }

The test code is as follows.

      while (true) {
            ContractCallThread getOutputsThread1 = new ContractCallThread(<contract_name>, <method_name>, <args_1>);
            ContractCallThread getOutputsThread2 = new ContractCallThread(<contract_name>, <method_name>, <args_2>);
            getOutputsThread1.start();
            getOutputsThread2.start();
            getOutputsThread1.join();
            String result1 = getOutputsThread1.result();
            <check_result1>
            getOutputsThread2.join();
      }

You'll soon get a result1 corresponding to args_2.

Could you please take a look? Thanks!

-linyufly

@linyufly
Copy link
Author

linyufly commented Jun 1, 2021

Update:

Further experiments showed that the thread-safety risk lies in ContractCall.

The following code avoids the race condition.

            ContractCall contract1 = new ContractCall(cfx, <contract_address>);
            ContractCall contract2 = new ContractCall(cfx, <contract_address>);
            ContractCallThread getOutputsThread1 = new ContractCallThread(contract1, <method_name>, <args_1>);
            ContractCallThread getOutputsThread2 = new ContractCallThread(contract2, <method_name>, <args_2>);

contract1 and contract2 refer to the same contract, but they are different objects.

The following code still has the race condition.

            ContractCall contract1 = new ContractCall(cfx, <contract_address>);
            ContractCall contract2 = contract1;
            ContractCallThread getOutputsThread1 = new ContractCallThread(contract1, <method_name>, <args_1>);
            ContractCallThread getOutputsThread2 = new ContractCallThread(contract2, <method_name>, <args_2>);

-linyufly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant