hhvm / hsl-experimental

Experimental features for the Hack Standard Library
MIT License
23 stars 10 forks source link

HSL IO: if a timeout is specified, make sure it's at least 1ms #104

Closed fredemmott closed 4 years ago

fredemmott commented 4 years ago

Fixes #100

Test Plan:

use namespace HH\Lib\Experimental\{TCP, Network};

async function server(TCP\Server $server): Awaitable<void> {
  $conn = await $server->nextConnectionNDAsync();
  while (true) {
    \var_dump('+');
    $t = microtime(true);
    $rest = '';

    try {
      \var_dump('reading :)');
      $rest = await $conn->readAsync(1, 0.1);
    } catch (\Throwable $e) {
      \var_dump('timeout');
    } finally {
      $elapsed = microtime(true) - $t;
      \var_dump('server left readAsync after '.$elapsed.' seconds');
    }

    \var_dump('~');

    if ('' !== $rest) {
      \var_dump('server read: '.$rest);
    }
  }
}

async function client(int $port): Awaitable<void> {
  $conn = await TCP\connect_nd_async('127.0.0.1', $port);
  await $conn->writeAsync("hello, world!");
  \var_dump('gave the server some data, now going in a loop :)');
  while (true) {
    await \HH\Asio\later();
  }
}

<<__EntryPoint>>
async function main(): Awaitable<void> {
  require_once('vendor/autoload.hack');
  \Facebook\AutoloadMap\initialize();

  $server = await TCP\Server::createAsync(
    Network\IPProtocolVersion::IPV4,
    '127.0.0.1',
    0,
  );

  list($_, $port) = $server->getLocalAddress();

  concurrent {
    await server($server);
    await client($port);
  }
  ;

  \var_dump('End of program');
}

This hangs forever without this change.

I've not been able to make a smaller repro case, or one that's suitable for a unit test: doing so requires < 0.5ms timing accuracy, which I've not been able to achieve in anything simpler.

facebook-github-bot commented 4 years ago

@fredemmott merged this pull request in hhvm/hsl-experimental@ffbafdfc3d53fc1426f8e88129513e20320c95bb.