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

Node.js 20 problems #907

Open
virgin09 opened this issue Nov 20, 2024 · 14 comments
Open

Node.js 20 problems #907

virgin09 opened this issue Nov 20, 2024 · 14 comments

Comments

@virgin09
Copy link

Up to node.js 18 the API worked wonderful. I am many using it with automated announcements from a JavaScript.

I am very sure that there is a connection between the malfunction of the API and node.js 20. As I am running my installation under Orcacle VirtualBox I can switch back and forth between the node.js versions 18 and 20.

Since node.js 20 I am receiving error messages „error=socket hang up“. They some repeatingly in a random manner. I was unable to identify the reason and source. I have been running many test,
announcements/ text too long?,
wrong strings?,
too fast after one another announcement?
Alle the tests ended nowhere. Randomly the announcements go thru sometimes after the third time, sometimes the first. But many times comes the socket hand up.

I also tried to start from a browser like chrome. As a result the browser notifies me „no connection to the server“. And - despite - it would play the announcement. Then, I get the same error message and it would not play. And the, sometimes, after the second call it works.

I am kind of desperate.

Has anyone experienced the same issue?
How can I solve this problem?
What can I test?

@jsiegenthaler
Copy link
Contributor

I've noticed identical issues but never realized it might be linked to node v18. I tried tracking it down but never got any further than noticing the TTS mp3 files were ok but the http server simply didn't respond sometimes.
I did update all dependencies that I could update without breaking the http api function.
I don't have the skills to hunt further but will monitor this thread to help as much as I can.

@virgin09
Copy link
Author

In my case it’s definitely NOT linked to node v18. It’s solely a node v20 issue.

@jishi
Copy link
Owner

jishi commented Nov 21, 2024

I think this is related to this global change in Node 20:

nodejs/node#43522

The Sonos players probably doesn't like keep-alive, and might not properly respond with a Connection: close. The http api will maintain connections and try to send new requests over the old socket and probably confusing the player.

Since this only affects requests that happen within 30-90 seconds (not sure what the default timeout is), this might appear random.

I'll see if I can reproduce it, I haven't run this project under newer Node.js versions in a long time.

@virgin09
Copy link
Author

@jishi this sounds like a good error diagnosis. Please let me know what tests I should conduct in order to narrow down the problem. I’ll find this API extremely helpful!

@virgin09
Copy link
Author

@jishi Greetings from Germany. As I am not a javascript resp. programmer professional I have been using ChatGPT for help. Here is what KI suggests:

The developer's response provides an insightful explanation of the issue you're experiencing with the node-sonos-http-api under Node.js 20. Here's a breakdown:

Key Points in the Response
Node.js 20 Change:

The issue is likely related to the global change in Node.js 20 introduced in the PR #43522, which modified how HTTP connections are handled, particularly with persistent (keep-alive) connections.
Sonos Player Behavior:

Sonos players may not handle persistent HTTP connections (keep-alive) correctly.
If the player does not properly close the connection (Connection: close header), the node-sonos-http-api attempts to reuse the connection for subsequent requests. This reuse could confuse the player and lead to errors like "No system has yet been discovered."
Randomness of the Issue:

The problem might only occur when requests happen within the time frame that a connection remains open (30–90 seconds, depending on the default keep-alive timeout). This makes the issue seem random and harder to reproduce.
Potential Solutions
Disable Keep-Alive in node-sonos-http-api:

Modify the node-sonos-http-api code to ensure that all HTTP requests include a Connection: close header. This forces each request to use a new connection, avoiding potential conflicts with keep-alive.
Example (in the API code):

javascript
Code kopieren
const options = {
method: 'GET',
headers: {
'Connection': 'close'
}
};
Rollback to an Older Node.js Version:

Temporarily downgrade to Node.js 18 or a version before Node.js 20 to see if the problem disappears. This would confirm that the keep-alive change is the root cause.
Test with Updated Dependencies:

Ensure all dependencies in node-sonos-http-api are updated. Older libraries might not handle newer Node.js behaviors correctly.
Adjust Node.js HTTP Agent Settings:

Use a custom HTTP agent to override default keep-alive behavior globally for node-sonos-http-api.
Example:
javascript
Code kopieren
const http = require('http');

const agent = new http.Agent({ keepAlive: false });
const options = { agent: agent };
Next Steps
Test the suggestions provided to identify the most effective solution.
Communicate with the developer if you find a consistent way to reproduce the issue under Node.js 20.
Monitor the developer's progress on debugging and fixing this compatibility issue.
This issue highlights the need to carefully manage dependencies and their compatibility when upgrading Node.js versions.

Does this help in any respect?

@jishi
Copy link
Owner

jishi commented Nov 21, 2024

@virgin09 It's mostly a repetition of what I stated and the potential remedies I was thinking of 😄. But I need some reproducable error so I can validate a fix.

I'm trying to run it using v20.11.0 but I'm not seeing the same behavior. I also run MacOS here, which might affect things.

Could you please state exact 20 version you are running, and which OS/Arch (like, Linux amd64 or arm64 or if it's windows).

@virgin09
Copy link
Author

Thanks for your quick response. I am using node.js 20.18.1. The os is PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian

If you need further information, let me know. Also, if you want me to test anything, I am willing to help.

@virgin09
Copy link
Author

virgin09 commented Nov 23, 2024

@jishi

Here further details of my system when its not working:

PlatformOperating system:linux
Architecture:x64
CPUs:2
Model: Intel(R) Celeron(R) J4125 CPU @ 2.00GHz
RAM:3.8GB
Node.js:v20.18.0
NPM:10.8.2
Disk size: 18.6 GB
Free disk space: 7.6 GB
_nodeCurrent:20.18.0
_nodeNewest:20.18.0
_nodeNewestNext:20.18.0
_npmCurrent:10.8.2
_npmNewest:10.8.2
_npmNewestNext:10.8.2

And here are the details of the system when the API works perfectly:

PlatformOperating system:linux
Architecture:x64
CPUs:2
Model: Intel(R) Celeron(R) J4125 CPU @ 2.00GHz
RAM:3.8GB
Node.js:v18.20.5
NPM:10.8.2
Disk size: 18.6 GB
Free disk space: 6.8 GB
_nodeCurrent:18.20.5
_nodeNewest:18.20.5
_nodeNewestNext:20.18.1
_npmCurrent:10.8.2
_npmNewest:10.8.2
_npmNewestNext:10.8.2

I hope that helps you to validate a fix. And, as mentioned before, I am willing to support. If you want me to conduct some tests, let me know.

@jsiegenthaler
Copy link
Contributor

Anyone have any news here? I'd love to bring this Sonos http api back to a stable working state. Today the first TTS announcement at 0805 worked, second TTS at 0836 was not spoken, however the mp3 file was generated properly (and can be found in \node-sonos-http-api\static\tts ). I'm running Node 22.12.0.

@virgin09
Copy link
Author

virgin09 commented Dec 7, 2024

@jishi good Morning Jishi, have you been able to work on the fix? I would very much appreciate your help! Kind regards from Germany.

@jishi
Copy link
Owner

jishi commented Dec 7, 2024 via email

@jsiegenthaler
Copy link
Contributor

I was testing my install today. I ran this command (ip hidden):
http://xxx.xxx.xxx.xxx:5005/dining%20sonos/favorite/Pure%20Unplugged

Sonos http api responded with:
{"status":"error","error":"http request timed out","stack":"Error: http request timed out\n at Object.invoke (/home/pi/node-sonos-http-api/node_modules/sonos-discovery/lib/helpers/soap.js:99:10)\n at Player.play (/home/pi/node-sonos-http-api/node_modules/sonos-discovery/lib/models/Player.js:446:15)\n at /home/pi/node-sonos-http-api/lib/actions/favorite.js:4:47\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"}

However, the Sonos radio channel started playing around 2 seconds after the error message occurred.
Subsequent commands to play other favorite channels worked without error.
I wonder if this is related...

@virgin09
Copy link
Author

virgin09 commented Dec 7, 2024

Hi, I have been trying to reproduce it but I'm not seeing the same errors, although I'm getting unexpected timeouts which I think stems from the same changes in node.js but I haven't had time to work on a fix yet.

Thanks for your kind reply. There is no rush as my systems still runs perfectly under 18. and also I don’t want to push. But still I would love to update node.js to the latest version. That is why I would appreciate if you will find some time to update and work on the fix in the near future. If I can be of any help don’t hesitate to get in touch. Greetings from Germany.

@virgin09
Copy link
Author

virgin09 commented Jan 4, 2025

Happy New year @jishi. I wanted to ask wether or not you found some time to deal with this issue? Your Sonos api is so much worth for me and others and it would be sad if one cannot use it anymore under newer versions of node.js. It seems there is no other person than yourself who can fix the problem. Hope for your help, Bernd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants