SimpleWebTransport: Port memory leak and thread safety fixes from upstream (James-Frowen/SimpleWebTransport#22)#4104
Closed
SimpleWebTransport: Port memory leak and thread safety fixes from upstream (James-Frowen/SimpleWebTransport#22)#4104
Conversation
…ream PR#22 Co-authored-by: MrGadget1024 <9826063+MrGadget1024@users.noreply.github.com> Agent-Logs-Url: https://github.com/MirrorNetworking/Mirror/sessions/32167aa1-1cc0-4815-a73f-cb57882b4de6
Copilot
AI
changed the title
[WIP] Port memory leak and thread safety fixes from upstream
SimpleWebTransport: Port memory leak and thread safety fixes from upstream (James-Frowen/SimpleWebTransport#22)
Mar 26, 2026
Collaborator
|
@MrGadget1024 its still draft dyi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Ports 6 upstream fixes into Mirror's vendored
Assets/Mirror/Transports/SimpleWeb/SimpleWeb/addressing buffer memory leaks, thread-safety races, and unbounded send queue growth.Changes
SendAllearly exit (SimpleWebServer.cs): Skip buffer allocation whenconnectionIds.Count == 0.Buffer release on missing connection (
WebSocketServer.cs): Callbuffer.Release()inSend()when the target connection is not found.Auto-dispose buffers (
SimpleWebClient.cs,SimpleWebServer.cs,SendLoop.cs): Replace manualRelease()calls withusingto guarantee release on exception paths.Thread-safe
QueueSend(Connection.cs,SendLoop.cs,WebSocketServer.cs,WebSocketClientStandAlone.cs): Gate all enqueues behind adisposedLockto prevent sending to a disposed connection.SendLoopnow captures queue refs viaGetSendQueue()once at start.Fragment buffer cleanup (
ReceiveLoop.cs): HoistQueue<ArrayBuffer> fragmentstoLoopscope (eliminates per-message allocation) and drain it in thefinallyblock to release buffers when the loop exits mid-fragmented-message.maxSendQueueSizelimit (SimpleWebTransport.cs,SimpleWebServer.cs,WebSocketServer.cs,Connection.cs,WebSocketClientStandAlone.cs): New serialized field (default 1000) threads through toConnection.QueueSend. When the per-connection queue exceeds the limit the buffer is dropped and the connection is kicked, preventing unbounded memory growth on slow clients.Original prompt
SimpleWebTransport: Port memory leak and thread safety fixes from upstream (James-Frowen/SimpleWebTransport#22)
Port the 6 commits from James-Frowen/SimpleWebTransport PR#22 into Mirror's vendored copy at
Assets/Mirror/Transports/SimpleWeb/SimpleWeb/.Background
Mirror vendors the SimpleWebTransport source directly under
Assets/Mirror/Transports/SimpleWeb/SimpleWeb/. The upstream PR#22 fixed several memory leaks, thread-safety races, and added amaxSendQueueSizefeature to prevent unbounded memory growth on slow connections. All changes must be adapted to Mirror's version of the code (which usesint connIddirectly instead ofIConnection/ConnectionwithconnIdfields, and has a slightly differentSimpleWebServerandWebSocketServerAPI).Changes to port (one commit at a time, in order)
Commit 1 —
fix: adding count == 0 check to SendAll before taking ArrayBufferFile:
Assets/Mirror/Transports/SimpleWeb/SimpleWeb/Server/SimpleWebServer.csIn
SimpleWebServer.SendAll(List<int> connectionIds, ...):This is the only
SendAlloverload that exists in Mirror'sSimpleWebServer. Add the guard.Commit 2 —
fix: simplify legacy SendAll and calling release inside legacy SendFile:
Assets/Mirror/Transports/SimpleWeb/SimpleWeb/Server/WebSocketServer.csIn
WebSocketServer.Send(int id, ArrayBuffer buffer):Commit 3 —
fix: using auto dispose to safely release buffersFile:
Assets/Mirror/Transports/SimpleWeb/SimpleWeb/Client/SimpleWebClient.csIn
ProcessMessageQueue, change theEventType.Datacase from manual release tousing:File:
Assets/Mirror/Transports/SimpleWeb/SimpleWeb/Server/SimpleWebServer.csIn
ProcessMessageQueue(MonoBehaviour behaviour), same change:File:
Assets/Mirror/Transports/SimpleWeb/SimpleWeb/Common/SendLoop.csReplace manual
msg.Release()calls withusing ArrayBuffer _ = msg;pattern in both thebatchSendand non-batch branches. In thebatchSendbranch:Same pattern in the non-batch branch (remove both
msg.Release()calls and addusing ArrayBuffer _ = msg;at the top of the loop body).Commit 4 —
fix: adding thread safe send method to ensure buffer is releasedFile:
Assets/Mirror/Transports/SimpleWeb/SimpleWeb/Common/Connection.csThe current Mirror
ConnectionhassendPendingandsendQueueaspublicfields. The upstream change makes them private and gates access behind a newQueueSendmethod and aGetSendQueueaccessor. However, Mirror'sConnectiondoes NOT haveonSendQueueFullormaxSendQueueSize(those come in commit 6). Apply the subset that's needed here:SetNeedsPong()method (replaces direct field access in ReceiveLoop):QueueSend(ArrayBuffer buffer)method with the dispose-safe lock: