Skip to content

libp2p.on() silently eats errors/exceptions #751

Closed
@mcclure

Description

@mcclure

Check out this libp2p with preact sample code to 0.29.0 demo (libp2p-preact branch). This is basically a rewrite of the libp2p-in-the-browser example. Run it with

npm install && npx webpack && (cd site && http-server -c-1)

Open this in a browser and you will see it makes lists of connected and discovered nodes. So far so good.

Okay, now edit the file src/index.tsx. Scroll down to line 163 and add a new first line of connectionManager.on(

node.connectionManager.on('peer:connect', (connection:any) => {
  let hfge:any         // New line, to make typescript happy
  hfge.waaf.oiew.fef() // New line

Obviously this is gibberish and will fail. But now try running the program. You will find

  1. The program fails uncleanly; although the problem is Javascript throws an exception when you use the undefined "hfge" object, the visible symptom will be that it appears we only discover and never connect to them.
  2. No error of any kind is printed in the Javascript console (tested on Chrome).

Impact

I would consider this a medium to high problem with the API. It is a problem because silent failures make it very difficult to debug what is failing or even realize something is failing. Exacerbating the problem is

  1. it's really unclear to me what the type is of the connectionManager.on() object, what the type of the object passed to it is, or what the methods on it are— if this is documented anywhere I've not found it in the many months, they are just magic functions in the getting started examples
  2. The signatures of these objects/functions routinely change from release to release. I got stuck on this because I was upgrading my code from 0.27.x to 0.29.x, the holdover code was bad because it used the old method signatures, and the lack of an error caused me to mis-diagnose the problem (ie: I thought the problem was that I was discovering peers but not connecting, but it was a simpler problem that I was connecting to peers but my on-connect handler was crashing).

This makes it unusually likely that users will make trivial errors in these exact functions that node.on will not catch.

Template questions

Version: 0.29.0, as documented in example repo package.lock
uname -a of test system: Darwin Pearl.local 17.7.0 Darwin Kernel Version 17.7.0: Sun Dec 1 19:19:56 PST 2019; root:xnu-4570.71.63~1/RELEASE_X86_64 x86_64
Test version of chrome: 85.0.4183.83 (Official Build) (64-bit)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions