Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Counter client example hangs after execution #637

Closed
relu91 opened this issue Dec 4, 2021 · 17 comments · Fixed by #744
Closed

Counter client example hangs after execution #637

relu91 opened this issue Dec 4, 2021 · 17 comments · Fixed by #744
Labels
binding-coap Issues related to coap protocol binding bug Something isn't working cli Issues with the command line interface examples Issues related examples in the repository

Comments

@relu91
Copy link
Member

relu91 commented Dec 4, 2021

It seems that there is a resource leak in the counter example. My first impression is that CoAp client does not get properly cleaned up after the interaction with the remote thing. Maybe integrating coapjs/node-coap#317 would help to solve the problem.

How to reproduce

Run node packages/cli/dist/cli.js examples/scripts/counter.js and in another shell node packages/cli/dist/cli.js -c examples/scripts/counter-client.js. The client application hangs without properly exiting.

@relu91 relu91 added bug Something isn't working binding-coap Issues related to coap protocol binding cli Issues with the command line interface examples Issues related examples in the repository labels Dec 4, 2021
@danielpeintner
Copy link
Member

@JKRhb do you know more?

@JKRhb
Copy link
Member

JKRhb commented Dec 6, 2021

Hmm, when I try to execute counter-client.js I get the following error:

Invalid URL: /counter
Fetch error: Error: WoTImpl fetched invalid JSON from 'coap://localhost:5683/counter': Unexpected token I in JSON at position 0

Because of that I am not sure if the error arises from the CoAP library or from the Helper class. Does it terminate when fetching the TD with HTTP instead?

In any case, I think the close method will probably help with resolving issues like these :) I added it now to #634. I am still waiting for the merge of one types related PR over at node-coap, once that one is merged the close method will hopefully land soon.

@relu91
Copy link
Member Author

relu91 commented Dec 6, 2021

Thank you for testing @JKRhb, are you in sync with master? we've just merged a PR that should have solved the issue that you're mentioning.

@JKRhb
Copy link
Member

JKRhb commented Dec 6, 2021

Thank you for testing @JKRhb, are you in sync with master? we've just merged a PR that should have solved the issue that you're mentioning.

Oh, yeah, you are right, I rebuilt the project and now I can reproduce the issue :) The issue seems to already occur when the TD is fetched – I commented out everything within the try block and the example still did not terminate. I guess #634 will probably fix this but it is a bit strange that the Agent does not close automatically as it should when there are no more messages in flight. When using HTTP instead for fetching, the problem does not occur I guess?

@danielpeintner
Copy link
Member

When using HTTP instead for fetching, the problem does not occur I guess?

Correct.
To be sure I tried it locally and with pure HTTP the client closes fine.

@JKRhb
Copy link
Member

JKRhb commented Jan 1, 2022

Edit: I initially thought I found a solution but it turned out to not actually solve the problem :/ I'll have another look into this...

Edit2: It is really odd that the problem only occurs when using CoAP for fetching a TD via the CLI. Using the fetch helper function in other contexts works perfectly fine. Maybe we could replace the fetching protocol in the example with HTTP for now and open another issue for this specific problem?

At least on my system, there seems to be another, IPv6 related issue ("Destination Unreachable" according to Wireshark) causing the client to hang during these lines:

https://github.com/eclipse/thingweb.node-wot/blob/3a9c4ca6fe6eccefbf5ba0930df23fecd8daffbd/examples/scripts/counter-client.js#L37-L39

Does the execution of the client example hang there for you as well?

@danielpeintner
Copy link
Member

Edit2: It is really odd that the problem only occurs when using CoAP for fetching a TD via the CLI.

In my case the client also hangs if I replace

WoTHelpers.fetch("coap://localhost:5683/counter")

with

WoTHelpers.fetch("http://localhost:8080/counter")

Did you mean that?

Does the execution of the client example hang there for you as well?

In my case the client does not hand. Having said that, I do not have any IPv6 addresses so I cannot tell what would happen if I would...

@relu91
Copy link
Member Author

relu91 commented Mar 16, 2022

I've opened the PR above which fixes the problem 😸 , let's wait for review -> merge -> and release

@JKRhb
Copy link
Member

JKRhb commented Mar 17, 2022

@relu91 Can you check if the problem is still present after the merge of coapjs/node-coap#329 and the new node-coap version? :) (Thanks again for the fix btw!)

@relu91
Copy link
Member Author

relu91 commented Mar 17, 2022

Yeah, I was testing it this morning. Good news first: it solves the "hang after fetch" problem. But the example still hangs 😢 . The problem is that node-wot does not usually stop clients after the execution of an affordance. Therefore, the coap client is never closed if you use it for an affordance. Now the problem is when we should close it?

  1. We can't close just before returning from the affordance because the application needs to read the value from the socket (e.g using the value function)
  2. We could in principle close it when the client has fully consumed the stream. I've tried it but it causes some problems with the HTTP client (probably because it is not clear to me if the same client riuse it)
  3. Ideally it would be good to have a destroy method in the ConsumedThing interface that clean up all the resources (closing open clients)

Ideas? opinions?

@danielpeintner
Copy link
Member

3. Ideally it would be good to have a destroy method in the ConsumedThing interface that clean up all the resources (closing open clients)

This would a discussion point for the Scripting API, right?

@relu91
Copy link
Member Author

relu91 commented Mar 17, 2022

This would a discussion point for the Scripting API, right?

yeah, I think we can discuss it there.

@danielpeintner
Copy link
Member

Another question that does come into my mind. "Why" did CoAP not hang in v0.7.x and does hang in v0.8.x. Does it relate again with the "stream" changes ?

@relu91
Copy link
Member Author

relu91 commented Mar 17, 2022

Another option is to consider to have some setting in the coap agent to automatically close the socket when a request/respose cycle is finished. @JKRhb what do you think?

@relu91
Copy link
Member Author

relu91 commented Mar 17, 2022

"Why" did CoAP not hang in v0.7.x and does hang in v0.8.x. Does it relate again with the "stream" changes ?

That's a good question, TBH not sure why, one possible answer:

  • we used to close the client in consumed-thing class since we didn't have to defer it because of streaming modes

@JKRhb
Copy link
Member

JKRhb commented Apr 26, 2022

Just tried out the counter example once more and #744 finally fixed it :) Sorry for the all the trouble :/

@relu91
Copy link
Member Author

relu91 commented Apr 26, 2022

Well done @JKRhb !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
binding-coap Issues related to coap protocol binding bug Something isn't working cli Issues with the command line interface examples Issues related examples in the repository
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants