I was about to implement the the reentrancy protection where the function is locked when contract execution is still processing. below is the code

contract Example {
    bool locked;
    function doSomething() public {
        require(locked != true);
        locked = true
        // Do Something
        locked = false

so my question is, how the contract execution works under the hood? If there's 2 transactions coming from different address that execute doSomething() at around the same time and the second transaction is executed when the contract storage variable of bool locked still true because first contract execution is still in process thus setting the locked status to true, then doesn't the 2nd transaction gets reverted? or the contract execution is executed one by one waiting for the first contract execution to finish first and then proceed to execute second contract execution?

1 Answer 1


You don't have to worry about multiple different transactions calling your function at near the same time. With Solidity on Ethereum, only one transaction is executing at a time.

However, you do have to worry about other code inside your own code calling your code again. This is how you can get into a reentrant situation in solidity. For example, if inside the working part of your function, you called another contract, and then that other contract called back to doSomething() again, then the stack trace would be would be inside doSomething twice.

  -> otherCode()
     -> doSomething()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.