Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

count_up can leak memory really easily #7

Open
acrylic-origami opened this issue Jul 7, 2017 · 0 comments
Open

count_up can leak memory really easily #7

acrylic-origami opened this issue Jul 7, 2017 · 0 comments

Comments

@acrylic-origami
Copy link
Owner

Suppose we wanted to keep count of requests:

<?hh
use HHReactor\Producer;
use function HHReactor\HTTP\connection_factory;
async function zipper(Awaitable<Connection> $maybe_connection, int $connection_id): Awaitable<Connection> {
  $connection = await $maybe_connection;
  await $connection->write("You are Connection #$connection_id"); // greet
  return $connection;
}
\HH\Asio\join(async {
  $connection_producer = Producer::create(connection_factory(8080));
  foreach(Producer::zip($connection_producer, Producer::count_up(), fun('zipper')) await as $connection) {
    // handle request after greeting
  }
});

Now, count_up currently delays emissions with await HH\Asio\later(), but that's only practical if the consumer is on pace with it. A dense schedule (i.e. lots of resumable wait handles) helps. Here, where the requests are sparse, count_up is a tight loop pushing values into the buffer and hugely leaking memory.

The most immediate solution is to get rid of count_up, which isn't really so useful anyways. However, longer-term, it would be useful to distinguish between this "autonomous" iterator behavior and a consumer-tied iterator like amphp's yield $emit(...) to avoid accidental memory leaks like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant