What Happens If a Transaction Runs out of Gas during Execution?
If a transaction runs out of gas, its execution stops immediately, and all changes to the blockchain state that occurred during the execution are reverted (the transaction fails). However, the sender still pays the transaction fee for the gas that was consumed up to the point of failure.
This mechanism prevents malicious or buggy smart contracts from indefinitely consuming network resources and ensures miners are compensated for the work they performed.