Lambda Coroutine Captures
Lambda captures are a common source of undefined behavior in coroutine code.
The Problem
The lambda is defined and called in one expression — the () after the closing brace — so started holds the task, not the lambda. That shape is an immediately invoked function expression, or IIFE; it matters again below.
namespace capy = boost::capy;
void process(socket& sock)
{
// The lambda is created and called on the spot. `started` holds the
// task the call returned -- not the lambda itself.
capy::task<> started = [&sock]() -> capy::task<>
{
char buf[1024];
auto [ec, n] = co_await sock.read_some(capy::make_buffer(buf));
}(); // <-- called here, so the lambda is a temporary and dies now
capy::run_async(executor)(std::move(started));
}
This code has undefined behavior. It may crash, corrupt memory, or appear to work until it doesn’t.
Why It Fails
In C++20, lambda coroutine captures are stored in the lambda closure object, not in the coroutine frame. Here is what happens:
-
The lambda closure is created, capturing
sockby reference -
The lambda’s
operator()()is called -
A coroutine frame is allocated (typically on the heap; HALO may elide this)
-
The coroutine suspends at
initial_suspend -
operator()()returns the task -
The lambda closure is destroyed, because it was a temporary
-
Later, the coroutine resumes
-
The coroutine accesses
sockthrough the destroyed closure -
Undefined behavior
The coroutine frame does not contain a copy of the captured sock. It contains a reference to the lambda’s capture storage, which no longer exists.
|
C++ copies a coroutine’s parameters into the coroutine frame, but the closure object is not one of them. The coroutine is the closure’s |
The Safe Pattern: IIFE With Parameters
The solution is to pass values as function parameters instead of lambda captures. Function parameters are copied to the coroutine frame.
namespace capy = boost::capy;
void process(socket& sock)
{
auto task = [](socket* s) -> capy::task<>
{
char buf[1024];
auto [ec, n] = co_await s->read_some(capy::make_buffer(buf));
}(&sock);
capy::run_async(executor)(std::move(task));
}
The parameter s is copied to the coroutine frame before the first suspension, so the pointer remains valid for the coroutine’s lifetime. The socket it points to must still outlive the coroutine.
Complete Example
Broken: Using Captures
class connection_handler
{
socket sock_;
std::string name_;
public:
capy::task<> run()
{
// BROKEN: 'this' captured in lambda, lambda destroyed after invoke
return [this]() -> capy::task<>
{
log("Connection from", name_); // UB: 'this' is dangling
co_await handle_request();
}();
}
};
Correct: Using Parameters
class connection_handler
{
socket sock_;
std::string name_;
capy::task<> handle_request();
public:
capy::task<> run()
{
// CORRECT: 'self' is a parameter, copied to coroutine frame
return [](connection_handler* self) -> capy::task<>
{
log("Connection from", self->name_);
co_await self->handle_request();
}(this);
}
};
When Are Captures Safe?
Captures are only safe when the lambda object outlives the coroutine:
// SAFE: lambda stored in 'handler', outlives coroutine
auto handler = [&sock]() -> capy::task<>
{
char buf[1024];
co_await sock.read_some(capy::make_buffer(buf));
};
// Lambda 'handler' still exists here
// Blocks until coroutine completes
capy::test::run_blocking()(handler());
// Lambda destroyed after coroutine finishes
This pattern is rare. Most async code immediately invokes the lambda and discards it, making captures unsafe.
Rules of Thumb
-
Default to IIFE with parameters for lambda coroutines
-
Never capture by reference (
[&]) in a lambda coroutine unless the lambda outlives the coroutine -
Capturing by value (
[=],[x]) is equally broken: the copy lives in the lambda, not the coroutine frame -
Capturing
thisis particularly dangerous and common: the body reads like a member function, but every member access goes through the closure -
When in doubt, use parameters
Alternative: Named Coroutine Functions
If the IIFE syntax feels awkward, use a named function instead:
class connection_handler
{
socket sock_;
capy::task<> do_handle()
{
// 'this' is an implicit parameter, handled correctly
char buf[1024];
co_await sock_.read_some(capy::make_buffer(buf));
}
public:
capy::task<> run()
{
return do_handle();
}
};
Member function coroutines work correctly because this is an implicit parameter, not a capture. The compiler copies the pointer to the coroutine frame, so the object itself must outlive the coroutine.
Quick Reference
| Pattern | Safety | Notes |
|---|---|---|
|
SAFE |
Parameter |
Member function coroutine |
SAFE* |
|
|
SAFE* |
Reference parameter; |
|
UNSAFE |
Capture |
|
UNSAFE |
Accessed through the destroyed closure |