Normally, a commercial sale of real estate is handled by a title company and one of the conditions of a purchase and sale contract is a satisfactory proof of ownership. The buyer is protected by (1) the contractual promise of the seller to deliver title in the purchase and sale contract (giving rise to a lawsuit if breached, e.g. requiring disgorgement of any additional profit made in the other transaction), (2) by a warranty of title in the deed provided at closing, (3) by the title company's insurance if the warranty deed is not valid, and (4) by an affidavit saying that the seller didn't do that provided at closing.
In your scenario, (2)-(4) never happen so the contract itself is the only remedy.
A court might very well impose a constructive trust or equitable lien on the proceeds of the sale after it happened as well. If the seller knew he was going to close with someone else when the purchase contract with the buyer was signed, that might also constitute fraud, although it is unlikely that a criminal prosecutor would press charges over what is primarily a breach of contract.
The purchase and sale contract or an affidavit stating its material terms and stating that it is still effective, or both, could be recorded in the real estate records of the county where the property was located prior to closing, but it would be highly usual to do so absent a clear indication that a breach was imminent (because it is very rare for this to happen since the consequences are clear and it is so often an open and shut case) but it could come up if the seller believes that a condition under the original contract was breached and the buyer disagreed. On rare occasions, the purchase contract itself is protected by a confidentiality clause and can't be recorded, in which case an affidavit stating the material, non-confidential facts could be recorded.
Recording a purchase and sale contract and/or affidavit would be effective because, if properly indexed, a recorded document is constructive notice to the world of the facts stated therein. Certainly, no title company would close on the deal if it knew a prior purchase and sale contract that was still in force was out there. There is no specially named instrument to state that a purchase and sale contract is pending. In practice, although not always in theory, notice to the title company or attorney or real estate professional handling the sale to the third party (if this could be identified) would also be sufficient to quash the deal to the third party in most cases.
If the third party buyer takes for substantially equivalent value and had no notice of the prior purchase deal, the third party buyer would have good title; if third party buyer did not purchase for value or had actual or constructive notice of the prior purchase deal, the third party's title would be voidable in a suit to seek specific enforcement of the purchase and sale contract against the seller.
A lis pendens only applies when active litigation is pending regarding title to the property.
CAVEAT: This answer is provided under general U.S. law principles that California usually follows, but I have not checked to see if there is anything idiosyncratic about California law in this situation.