Fix send queue deadlocks and improve stability during disconnects (no API breaking changes) #105
+476
−362
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.
Title: Fix send queue deadlocks and improve stability during disconnects (no API breaking changes)
Description:
Summary
This PR improves reliability of the .NET/Native (non-WebGL) implementation by:
SynchronizationContextbeing available.The goal is to prevent hangs/freezes and “await never completes” scenarios seen during poor network conditions, disconnects, or teardown.
Problems addressed
1) Potential deadlocks / hangs in send path
The current implementation uses a mixed approach (locks +
Monitor.TryEnter(m_Socket)+Task.Wait(...)inside an async method). In practice, a synchronous wait inside an async send pipeline can stall the calling thread and/or hang when the socket enters a closing/aborted state while sends are queued.This PR moves to a single async send queue:
ConcurrentQueue.SendAsync(...)normally.Taskthat resolves when that send completes or fails.This avoids synchronous waits and minimizes lock contention.
2) Queued sends can wait forever after close
If the connection drops while sends are queued, callers awaiting
Send(...)/SendText(...)can get stuck if nothing completes those tasks.This PR ensures:
Open, new sends fail quickly rather than silently queueing.3) Main-thread dispatch robustness
MainThreadUtil.Run(...)previously relied on posting to a capturedSynchronizationContext. In some Unity runners / initialization orders, the context can be null or not associated with the Unity main loop.This PR changes
Run(...)to enqueue coroutines and start them duringUpdate(), ensuring they always start from the Unity main thread. Exceptions while starting coroutines are caught and logged (matching a “low drama” error-handling expectation).Compatibility / behavior
Connect()behavior remains the same as upstream (still blocks until the receive loop ends). This PR does not changeConnect()semantics for existing call sites.Notes on design
RunContinuationsAsynchronouslyto avoid running user continuations inline on the pump.Testing
Tested in Unity by:
DispatchMessageQueue()on the main thread.