Hacker News new | ask | show | jobs
by flagxor 3221 days ago
Event loops have these too, they just go by different names:

* non-determinism -> order in which setTimeouts run

* race conditions -> callbacks happen in an unexpected order

* deadlock -> broken callback chain

2 comments

Yeah, but nobody relies on the execution order of disparate callbacks to achieve their results in JS. You're going to get burned in like two seconds if you try to do anything you listed. Lower level concurrency primitives in other languages allow developers to build fragile solutions which work 99% of the time until they deadlock and everything blows up. The concurrency model in JS is very explicit about being "no guaruntees."
There are many reasons why complex software fails in the 1% case. Concurrency is not the only reason. In my experience it is not the top reason. Our top hard-to-repro or no-repro crashes in JavaScriptCore are from non determinism introduced by the workload itself. Inside our engine we have many sources of nondeterminism that manifests even with concurrency features disabled.
Are you sure that setTimeouts are run in a non-deterministic order?

Callbacks can only come back in an unexpected order if you are using I/O.

A broken callback chain is not the traditional definition of "deadlock".

> Are you sure that setTimeouts are run in a non-deterministic order?

Certainly, if they are issued from different callbacks. :-) And in relation to how they may happen to be interleaved with I/O.

Also add to the non-determinism bucket: the order in which messages arrive from separate Workers.

> Callbacks can only come back in an unexpected order if you are using I/O.

Which abounds and takes many forms both in the browser and node.js.

> A broken callback chain is not the traditional definition of "deadlock".

Indeed, but it is the same class of developer hazard at play: waiting for an event to happen than will never come. Admittedly, only a subset of dropped callback chains correspond to the strict definition of deadlock: those where a chain is involved in marking a state/resource as available.

For example: I ignore the next button press on a ui that disables the button pending a reply because I accidentally failed to update the button state when a network request failed.

> Certainly, if they are issued from different callbacks. :-) And in relation to how they may happen to be interleaved with I/O.

That's non determinism from I/O, not from the callback or the setTimeout. setTimeouts are called in order based on schedule time.

And you can't blame non-determinism in JS on I/O. No language in existence is deterministic based on that measure.

This seems rather racy in Chrome, despite the I/O being deferred to the end :-)

  (function() {
    var x = '';
    setTimeout(function() { setTimeout(function() { x += 'a'; }, 6); }, 4);
    setTimeout(function() { setTimeout(function() { x += 'b'; }, 5); }, 5);
    setTimeout(function() { console.log(x); }, 50);
  })();
It's a fair point that generally I/O is the source of the bulk of the non-determinism in most JS programs. But if that I/O non-determinism is present (as it is in most useful programs), event loops aren't a panacea to avoid peril from it.

Concurrency introduces another source of non-determinism. But just as good programs use care with the ideally limited amount of code responding to events, good concurrent programs use similar care with access to shared state.

I'm unsure if the strawman syntax proposed encourages that care, but it is interesting that it can at least be done without breaking basic safety + performance. With SharedArrayBuffer on the way, we'll get all the non-determinism of parallel execution peeping through to JS without a particularly JS friendly syntax, so it's at least worth thinking about if something should be added to JS.

I object to the term "similar care". There's a big difference in what it takes to understand thread issues compared to single-threaded event-based systems.

For instance, if I'm not mistaken, your setTimeout thing is relying on precise timing. But this is documented when you look up setTimeout. It's not difficult to understand.

How to use locks and conditions/monitors correctly IS difficult to understand. You can't just read a few lines in the API documentation and proceed on to write correct code, beyond small toy examples.

setTimeout is obviously a simpler primitive to understand in isolation than monitors/conditionvars, but I would argue that when used in equally complex scenarios, similar challenges emerge.

Something like Dinning Philosophers is no less tricky to express + understand with an event loop, and with JS's async await probably would most cleanly be expressed in a style that mirrors a monitors/conditionvars version.

The fact that JS has added async await suggests demand for the convenience of concurrent blocking threads. While async-await manages to separate out non-async code to a degree, once an await happens, the global state can also be arbitrarily mutated no differently than with threads.

  function block() { return new Promise(function(a,b) { setTimeout(a, 0); }); }
  var x = 0;
  (async function foo() {
     x = 1;
     await block();
     console.log(x);
  })().then();
  (async function bar() {
     x = 2;
  })().then();
Concurrency is hard, but event loops aren't a magic wand.