Vyper @nonreentrant: Guarding Withdraw Functions
Prevent reentrancy on Vyper withdraw functions with @nonreentrant, plus gas costs and verification steps.
25 Dec 2025, 12:35 UTC

Problem and Takeaway
A withdrawal function that sends Ether to msg.sender before clearing the caller's balance can be re-entered while the external transfer is still in progress. A malicious contract's fallback function can call withdraw() again and drain funds repeatedly. The useful takeaway is that decorating the function with @nonreentrant locks a storage slot for the duration of the call, so any re-entry into that same function reverts.
How @nonreentrant Locks a Function
Vyper implements @nonreentrant by writing to a dedicated storage slot before the function body runs and clearing it when the function returns. While the slot is set, a second call to the same decorated function from within the first execution triggers a revert. The guard is per-function and per-key: calling a different nonreentrant function is allowed, but an unprotected function in the same contract is not covered.
Worked Example: Guarded Withdraw
The following example assumes Vyper 0.3.x. It tracks user balances, zeroes the balance before the external call, and guards the whole operation with @nonreentrant:
# @version ^0.3.10
balances: HashMap[address, uint256]
@external
@nonreentrant
def withdraw():
amount: uint256 = self.balances[msg.sender]
self.balances[msg.sender] = 0
send(msg.sender, amount)
Because the balance is cleared before send, a re-entrant call finds a zero balance and cannot withdraw again. If the recipient contract calls withdraw() from its fallback during the transfer, the guard slot is still locked and the transaction reverts. Confirm the correct ETH-transfer built-in for your compiler version before deploying; send may differ across Vyper releases.
Costs, Limits, and Verification
Each @nonreentrant function pays for at least one extra storage write to set the guard and one to clear it, so gas usage is higher than an unguarded equivalent. Reserve it for functions that move value or modify critical state, not for cheap read-only operations.
The decorator does not secure business logic or protect unguarded entry points. A contract can still be re-entered through a different public function that lacks the decorator. Compiler upgrades can also change guard gas costs and storage layout, so re-audit after changing Vyper versions.
Verify the guard with these steps:
- Deploy the contract and a malicious test contract that calls back into
withdraw()during its fallback. Assert that the outer withdrawal transaction reverts. - Compile with your target Vyper version and inspect the storage layout to confirm a nonreentrant slot exists for the guarded function.
- Estimate gas for
withdraw()with and without@nonreentrantto measure the storage-write overhead in your deployment context.
Closing
Apply @nonreentrant to external functions that send Ether, update balances, or otherwise change critical state. Treat it as one layer of defense: combine it with checks-effects-interactions ordering, limit external calls, and re-run verification after any compiler upgrade.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.