fix(music-together): Improve auto-reconnect to fix random disconnections #3688
No reviewers
Labels
No labels
awaiting-reply
breaking changes
bug
cannot-reproduce
dependencies
documentation
duplicate
electron-issue
enhancement
fix-available
good first issue
help wanted
invalid
javascript
need more information
need rebase
official-youtube-music-issue
plugin request
question
release
security
stale
Status: blocked
typo
wontfix
ytmd-issue
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference: YTMD/youtube-music#3688
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "BoghosDavtyan/master"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Hello! As someone new to TypeScript, I've done my best to address an issue I was having with the
music-together
plugin and would appreciate your feedback.Problem/Motivation
I noticed that when using the
music-together
feature, the connection would often drop randomly after a period of time (ranging from a few minutes to a few hours). This required manually re-entering the host ID on all connected clients, which was disruptive. The existing error handling didn't seem to account for recoverable network issues, and there wasn't much logging to help diagnose the problem.Solution
I've modified the
connection.ts
file to build a more resilient connection handling system. The key changes are:reconnectLoop
that automatically tries to re-establish a connection to the PeerJS server if it's lost unexpectedly.peer.on('error', ...)
handler now distinguishes between recoverable network errors (likePeerUnavailable
orServerError
) and fatal errors. It will only attempt to reconnect on recoverable errors.isManualDisconnect
andisReconnecting
flags to ensure the reconnection logic only runs when it should (i.e., not after a user manually disconnects) and to prevent multiple loops from running at once.console.log
andconsole.warn
statements to provide clear feedback on the connection status, disconnection reasons, and reconnection attempts. This should make future troubleshooting much easier.Testing
music-together
session and left it running for several hours, observing that it successfully recovered from network fluctuations without requiring a manual reconnect.Thank you for considering my contribution!
Pull Request Overview
This PR improves the resilience of the
music-together
plugin by implementing automatic reconnection logic to handle random connection drops. The changes focus on distinguishing between recoverable network errors and fatal errors, implementing a smart reconnection loop, and adding better state management to prevent duplicate reconnection attempts.[nitpick] The error type comparison should be extracted into a helper function or constant array to improve readability and maintainability. Consider creating
const RECOVERABLE_ERROR_TYPES = [PeerErrorType.Network, PeerErrorType.PeerUnavailable, PeerErrorType.ServerError]
and usingRECOVERABLE_ERROR_TYPES.includes(err.type)
.The
isManualDisconnect
flag is never reset tofalse
, which means after a manual disconnect, automatic reconnection will be permanently disabled even if the user tries to connect again. The flag should be reset when starting a new connection.@ -84,0 +114,4 @@
}
console.log(
'Music Together: Attempting to reconnect to PeerJS server...',
The while loop condition
!this.peer.destroyed
could create an infinite loop if the peer is never destroyed but also never successfully reconnects. Consider adding a maximum retry count or timeout mechanism to prevent infinite loops.@ -85,2 +134,4 @@
this._mode = 'host';
this.isReconnecting = false;
this.waitOpen.resolve(id);
console.log('Music Together: PeerJS connection opened with ID:', id);
[nitpick] The hardcoded delay value
10000
should be extracted to a named constant likeRECONNECT_RETRY_DELAY_MS = 10000
to improve readability and make it easier to adjust if needed.@ -115,0 +160,4 @@
err.type === PeerErrorType.ServerError)
) {
reconnectLoop(err);
} else {
The change from throwing an error to silently returning when already disconnected could mask programming errors where disconnect() is called unexpectedly. Consider logging a warning or keeping the original behavior for better debugging.
Pull Request Overview
This PR improves the reliability of the music-together plugin by implementing automatic reconnection logic to handle random disconnections that were disrupting user sessions.
Key changes:
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
@ -101,3 +145,1 @@
this.peer.disconnect();
this.peer.destroy();
this.peer.on('disconnected', () => {
This infinite loop could run indefinitely if the peer never becomes disconnected or destroyed, potentially consuming CPU resources. Consider adding a maximum retry count or timeout mechanism to prevent endless reconnection attempts.
The 'disconnected' event handler only logs a warning but doesn't trigger any reconnection logic. This inconsistency with the PR's stated goal of automatic reconnection could be confusing. Consider either triggering reconnectLoop here or removing the misleading warning message.
@ -142,1 +190,4 @@
this.isManualDisconnect = true;
this.isReconnecting = false;
this._mode = 'disconnected';
The 'close' event typically indicates a deliberate disconnection, but this code triggers a reconnection attempt. This could cause unwanted reconnections when the peer is intentionally closed. Consider only triggering reconnectLoop on the 'disconnected' event or adding additional checks.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.