intel / iotivity-node

Node.js bindings for IoTivity
https://www.iotivity.org/
42 stars 44 forks source link

[REG]require('iotivity-node') hangs in non-REPL mode #113

Closed qiuzhong closed 7 years ago

qiuzhong commented 7 years ago

Steps:

  1. In node REPL mode, require('iotivity-node') works well:

    # node
    > require('iotivity-node');
    { platform: [Getter/Setter],
      device: [Getter/Setter],
      client: 
       Client {
         getDeviceInfo: [Function],
         getPlatformInfo: [Function],
         create: [Function],
         retrieve: [Function],
         update: [Function],
         delete: [Function],
         findDevices: [Function],
         findPlatforms: [Function],
         findResources: [Function],
         _events: { newListener: [Function], removeListener: [Function] },
         _eventsCount: 2 },
      server: Server {} }
  2. Run this require statement in node with -e options, it will hang

    # node -e "require('iotivity-node');"
    

    It doesn't return.

Test environments

gabrielschulhof commented 7 years ago

That's because setInterval() is called on startup. There is, however, no requirement that iotivity-node work with node -e.

qiuzhong commented 7 years ago

Running test.js with one require('iotivity-node') statement still hangs.

gabrielschulhof commented 7 years ago

Yes, I know. There is no way of exiting, because we have no specification for shutting down the stack. Thus, your only way out is process.exit(0).

qiuzhong commented 7 years ago

Thanks. With process.exit(0) it can exit. I am curious why in iotivity-node-1.1.1-3 on Ostro image, node test.js can return in several seconds.

qiuzhong commented 7 years ago

@gabrielschulhof

For the same require statement:

$ cat test.js
require('iotivity-node');

For iotivity-node 1.1.1-3, node test.js can return.

# node test.js
# echo $?
0

For iotivity-node 1.2.0-1, node test.js will hang.

# node test.js

So is this the spec change and acceptable? Could you give us an explanation for this?

gabrielschulhof commented 7 years ago

IIRC 1.1.1 was structured such that you had to

var ocf = require( "iotivity-node" );
var client = ocf( "client" );

Thus, require( "iotivity-node" ) merely loaded the module, but did not start the setInterval() until you called ocf( "client" ).

In contrast, 1.2.0 is structured such that setInterval() is started as part of the iotivity-node loading process. That is, setInterval() is started by calling require( "iotivity-node" ). This is in accordance with the latest spec, whereby no extra function call beyond the require( "iotivity-node" ) is necessary in order to initialize iotivity-node for use either as a client or as a server.

AFAIK there is no requirement that I defer initialization until the library is actually used.