Fix hanging streams #74
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
A race conditions with websocket handling could cause the route handler to "hang" indefinitely, after the underlying connection is closed. This surfaces in two ways:
syncSemaphorecould permanently hold on to a lock for these, eventually leading to no locks being available, and no users able to sync.While the race condition is rare, it does build up over time.
In tests I could trigger the race condition in two places:
observer.registerListener()is called, by settinglifetime: 0on the client. I'm not sure whether this ever happened in practice.if (requestedN <= 0)check. This caused the handler to indefinitely wait for the nextrequest()orcancel()to be triggered.The fix here is to:
AbortControllerinstead of the observer to handle stream cancellation.if (signal.aborted)to see whether the stream is already cancelled, wherever we listen for cancellation.syncSemaphorewithin 30 seconds, instead of just waiting silently.