race condition

A bug whose outcome depends on the timing of two things happening at once.

In more detail

It usually appears when two operations run concurrently or asynchronously and the code assumes an order that is not guaranteed. For example, two programs that each check a rule and then act can both pass the check before either acts, breaking the rule. Fixes make the check and the action one indivisible step, such as a lock, a database constraint or a conditional write, or make the order explicit and ignore results that arrive too late.

Examples

In user interfaces a slow response or timer can arrive after the state it was meant for has changed; state machines and effect cleanup prevent this.
Two workers processing the same outbox event at once; a unique constraint lets the database settle it.
Go: never rely on goroutine scheduling order; sort results when report order matters
If a React component fetches user 1 and then user 2, the reply for user 1 may arrive last; cancel it with an AbortController or an ignore flag in the Effect cleanup.
A lost update in a database is one kind of race condition.
In this course: In tests, racing an assertion against a moving clock or a service that is still starting is a classic source of flakiness. Fixed inputs and explicit readiness checks remove the race instead of hiding it with sleeps.

Related terms

Taught in

References

Certification courses