agsh / onvif

ONVIF node.js implementation
http://agsh.github.io/onvif/
MIT License
681 stars 231 forks source link

Application crash due to event issues #243

Open bartbutenaers opened 1 year ago

bartbutenaers commented 1 year ago

Hi everybody,

I have already received a couple of issues (from multiple users) that their application (Node-RED) crashes from time to time, due to an exception while listening for events.

[red] Uncaught Exception: 6 Sep 17:30:16
[error] Error: You should create pull-point subscription first! 
   at Cam.pullMessages (/home/pi/.node-red/node_modules/onvif/lib/events.js:188:10) 
   at Cam._eventPull (/home/pi/.node-red/node_modules/onvif/lib/events.js:309:9) 
   at Cam.<anonymous> (/home/pi/.node-red/node_modules/onvif/lib/events.js:100:13) 
   at parseSOAPString (/home/pi/.node-red/node_modules/onvif/lib/utils.js:108:3) 
   at IncomingMessage.<anonymous> (/home/pi/.node-red/node_modules/onvif/lib/cam.js:246:4) 
   at IncomingMessage.emit (node:events:539:35) at IncomingMessage.emit (node:domain:475:12) 
   at endReadableNT (node:internal/streams/readable:1345:12) at processTicksAndRejections

The users tell that it happens from time to time, but cannot explain it. Because their camera is still operation when this exception occurs. They don't observer anything abnormal at those moments...

I don't think I can catch this exception myself in our application, and it needs to be fixed in the onvif library? On the other hand I find it very strange that I am the first one to report this, so perhaps I should catch it (somehow) myself anyway. But not sure how to do that...

All tips are welcome!!

Thanks!!! Bart

agsh commented 1 year ago

@bartbutenaers Hi! I'll try to find a working camera that supports events and investigate this issue. I'll keep you informed. Right now, when skimming through the code, I can't find anything strange:

Maybe in Node-RED some other methods are called before subscribing? I think, first step is to put watcher on this.events.subscription.subscriptionReference and watch for changes. For example, replace the line with events property definition (https://github.com/agsh/onvif/blob/master/lib/cam.js#L94) with something like this:

    this.events = new Proxy({}, {
        set(obj, prop, value) {
            console.log('[events]: setting', prop, value);
            return Reflect.set(obj, prop, value);
        }
    });
bartbutenaers commented 1 year ago

@agsh, Sorry for the delay! The lack of free time is killing me at the moment....

Thanks for having a look at this!! Such problems - that only happen from time to time - are always very nasty to troubleshoot.

Could you please explain a bit more how what you want to achieve with the Proxy? Suppose I ask one of the users (who has the problem from time to time) to update that code snippet, what does he has to look at in his console log?

Maybe in Node-RED some other methods are called before subscribing?

Can you explain this also a bit more? Perhaps I use your library somehow incorrect....

agsh commented 1 year ago

@bartbutenaers Hello! I've prepared a branch with wrapped events property into Proxy object: https://github.com/agsh/onvif/tree/events_logging_test So you can change the onvif dependency for you project: npm i agsh/onvif#events_logging_test To test that proxying is working run the test suite for events in the root of the project: ./node_modules/.bin/mocha ./test/events.js --grep ^Events. This is synthetic tests but it shows the things we are looking for. The point of interest for us is here: image This line on the standard output shows the change to the this.events.subscription.subscriptionReference.address property which is needed for pullMessages method at this line: https://github.com/agsh/onvif/blob/events_logging_test/lib/events.js#L225 So when the error occurs again, just look behind in the process logs to understand how this.events.subscription property disappears.

Also, I noticed that you get an error at line 188 Cam.pullMessages (/home/pi/.node-red/node_modules/onvif/lib/events.js:188:10) But this error in the fresh version is on line 227: https://github.com/agsh/onvif/blob/master/lib/events.js#L227 Please check the version of the package.