Three languages, three versions of the same bug. In JavaScript, a loop schedules three timers that should log 0, 1 and 2, and they log 3, 3, 3. In older Go, a loop starts three goroutines that should print 0, 1 and 2, and they print 3, 3, 3 (or something unpredictable). In Python, a list of three small functions built in a loop all return 2. The common cause is a single idea about closures that is easy to state and easy to forget: a closure captures the variable itself, not a snapshot of its value.
What a closure is
A closure is a function bundled with access to the variables that were in scope where it was defined. That is what lets a callback written inside a function use that function’s local variables even after the outer function has returned. Event handlers, callbacks passed to map or filter, goroutines started with an anonymous function, and decorators in Python all rely on closures.
The important word is access. The closure does not copy the variable’s current value into itself. It keeps a link to the variable. When the closure eventually runs, it reads whatever the variable holds at that moment. If the closure runs immediately, the distinction is invisible. If it runs later (a timer, a goroutine, a function stored for later), the variable may have moved on.
JavaScript: var versus let
A for loop that declares its counter with var creates one variable for the whole function. Each of the three timer callbacks closes over that same variable. The loop runs to completion first, leaving the counter at 3, and only then do the timers fire, because timer callbacks wait in the event loop’s task queue until the current code finishes. All three read the same variable and log 3.
Declaring the counter with let changes the scoping. The language specification creates a fresh binding of the loop variable for each iteration, copying the current value into it. Each callback closes over its own binding, so the output is 0, 1, 2. Before let arrived in ES2015, developers achieved the same thing by wrapping the body in an immediately invoked function that received the counter as a parameter, creating a new scope each time.
Go: a language that changed its rules
Go had the same issue with goroutines. A loop from 0 to 2 starting a goroutine that prints the loop variable shared a single variable across iterations. By the time the goroutines were scheduled, the loop had often finished, so they typically printed 3, 3, 3. Worse, the goroutines read the variable while the loop was writing it, which is a data race; Go’s race detector (the -race flag) reports it.
This was such a common mistake that Go 1.22 changed the semantics: in a module whose go.mod declares Go 1.22 or later, each iteration of a for loop gets its own copy of the loop variable. The same code now prints 0, 1 and 2, though in no guaranteed order, because goroutines run concurrently. Code in older modules keeps the old behaviour, so when you read Go code, check the version line in go.mod before assuming either way. The traditional fix, still seen widely, is to pass the value as an argument to the goroutine’s function, or to shadow the variable inside the loop body.
The same snippet usually has a second problem: it waits with a one-second sleep and hopes the goroutines finish. Sleeping is not synchronisation. On a busy machine one second may not be enough, and on an idle one it wastes time. A sync.WaitGroup, or collecting results over a channel, makes the main function wait exactly as long as needed.
Python: late binding
Build a list of lambdas in a list comprehension over range(3), each returning the loop variable, and call them afterwards. All three return 2. Python closures look up free variables when called, a behaviour often described as late binding, and by then the loop variable holds its final value.
The idiomatic fix uses a default argument: write the lambda with a parameter whose default is the loop variable. Default values are evaluated when the function is defined, so each lambda freezes the current value. (This is the same rule that makes mutable default arguments dangerous, here used on purpose.) functools.partial achieves the same thing more explicitly.
The general pattern, and where else it bites
The bug needs three ingredients: a variable that changes over time, a function that captures it, and a delay between creating the function and running it. Remove any one and the problem disappears. That suggests where to look beyond loops:
- React components. An effect or interval callback that captured a state value from an earlier render keeps seeing that old value, the so-called stale closure. Dependency arrays, functional state updates and refs exist largely to deal with this.
- Java lambdas. Java sidesteps the issue by allowing lambdas and anonymous classes to capture only local variables that are final or effectively final. The compiler refuses to let you capture a variable that changes, which is why loops often copy the counter into a new local first.
- C#. The
foreachloop variable was changed in C# 5 to be fresh per iteration, fixing the same class of bug, but a classicforloop still shares one variable, so closures over its counter behave the old way. - Retry and scheduling code. A callback that reads a configuration variable or a “current user” field when it fires, rather than when it was scheduled, can act on data that has since changed.
Choosing deliberately
Sometimes reading the latest value is exactly what you want: a callback that should always use the current setting, for example. The skill is not to avoid capturing variables but to decide, each time you write a deferred function, whether it should see the value now or the value then. If the answer is “now”, pass the value in as a parameter or copy it into a variable scoped to that iteration. That single question prevents the whole family of bugs, whatever the language.
Written by the RP Strativa Team.
Leave a Reply