What is a mutex — and why single-threaded code needs a lock

The word "mutex" usually lives in textbooks about threads. So it seems: JavaScript is single-threaded, I don't need this.
Then a user opens your app in two tabs. Both decide to refresh the login token at the same moment. One succeeds, the other sends an already-used token — and the person gets kicked out of their account. No threads involved.
The cure for this is exactly a mutex. Let's figure out what it is.
What a mutex is
A mutex is a lock that lets only one party at a time reach a resource. The name comes from mutual exclusion.
Picture a café restroom with one key at the counter. Take the key — go in. Everyone else waits until the key comes back. Two people can't go in at once — there's only one key.
Code works the same way:
- Code wants to work with a shared resource — say, a balance or a token.
- First it takes the lock.
- Does the work.
- Releases the lock.
While the lock is taken, everyone else waits their turn. The piece of code between "take" and "release" is called the critical section.
Why you need it with a single thread
Because the bug doesn't come from threads, it comes from waiting. That's a race condition.
Look at a typical payment:
async function pay() {
const paid = await checkIfPaid() // waiting for the server...
if (!paid) await charge() // ...then charging
}
The user double-clicked. The first call reached await and paused for the server. While it waits, the second call starts. Both asked "already paid?", both heard "no". Both charged the card.
There was one thread. But async code gives up control at every await — and the neighboring call slips through that gap. A mutex closes the gap: the second call waits until the first one finishes completely.
A mutex in the browser: Web Locks
Browsers have a built-in mutex that few people know about — navigator.locks. It works in all modern browsers. And crucially — across tabs of the same site.
Here's the fix for that two-tab story:
await navigator.locks.request("refresh-token", async () => {
if (tokenIsFresh()) return // another tab already refreshed it
const token = await refreshToken()
saveToken(token)
})
How it works:
- The first tab takes the lock named
"refresh-token"and refreshes the token. - The second tab gets in line and waits.
- When its turn comes, it checks: is the token already fresh? Yes — so there's nothing to do.
Note the check inside. Without it, the second tab would simply wait its turn and refresh the token anyway. The lock forms the queue; the check inside stops the work from happening twice.
Another bonus: the lock is released automatically when the function finishes. Even if an error was thrown inside.
A mutex in the database
Money, stock levels, seats in a hall — all of that lives in the database. And it has its own lock. In Postgres it's FOR UPDATE:
BEGIN;
SELECT balance FROM accounts WHERE id = 42 FOR UPDATE;
-- the row is locked: other queries on it wait
UPDATE accounts SET balance = balance - 100 WHERE id = 42;
COMMIT; -- lock released
Until the transaction ends, a second query on the same row will stop and wait. Two simultaneous charges can't both read the old balance.
This is sturdier than a browser lock. A user can open the site on a phone and a laptop at once — the browser lock won't see that. The database sees everyone.
Where a mutex bites
A lock is strong medicine, and it has two side effects.
- Forget to release — everyone's stuck. If you take a lock by hand and the code crashes before "release", the queue waits forever. So release in
finally, or use tools that release on their own, likenavigator.locks. - Two locks in different order — a deadlock. The first holds lock A and waits for B. The second holds B and waits for A. Both wait for each other forever. The rule is simple: if you need several locks, always take them in the same order.
And don't lock more than you need. The longer you hold a lock, the longer the line. Inside the critical section — only what truly touches the shared resource.
How is a mutex different from a semaphore?
A mutex lets one in. A semaphore lets up to N in at once — like a parking lot with ten spaces. A semaphore is handy for limiting, say, the number of parallel requests to an API.
Isn't disabling the button after a click enough?
For a double click in one tab — yes, that's the first and cheapest step. But a button won't save you from a second tab, a second device or a network retry. That needs a lock on the server or in the database.
Does a mutex slow the app down?
A little — while one party works, others wait. But only those reaching for the same resource wait. If the critical section is short, nobody will notice the difference, and there won't be any double charges.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





