Skip to content

fix: Memory leaks in SimpleWebTransport#4098

Open
liamcary wants to merge 5 commits intoMirrorNetworking:masterfrom
liamcary:bugfix/swt_memory_leaks
Open

fix: Memory leaks in SimpleWebTransport#4098
liamcary wants to merge 5 commits intoMirrorNetworking:masterfrom
liamcary:bugfix/swt_memory_leaks

Conversation

@liamcary
Copy link
Contributor

@liamcary liamcary commented Mar 5, 2026

Handle several edge cases where ArrayBuffers were taken from the buffer pool but potentially not returned. Also handle exceptions to prevent Connection.Dispose exiting before releasing sendQueue.

Comment on lines +67 to +74
try
{
receiveThread.Interrupt();
}
catch (Exception e)
{
Log.Exception("[SWT-Connection] receiveThread.Interrupt", e);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you seen Thread.Interrupt throw? I dont think SecurityException should ever be thrown in unity/mono or with the way we use it, because we both create and interrupt the thread there should be no permission issue

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't, but i added it to be safe in case its extremely rare, or happens on windows but not linux, etc. Theoretically, security permission exceptions would have thrown earlier, when starting the thread. It's critical that Dispose continues executing, so i think its a good policy to handle every possible exception, even if it just serves as a reminder to people editing Dispose() in future. For these changes I just went to the docs for each method and added exception handling for methods that have any documented exceptions, and removed exception handling for those that don't.

@liamcary
Copy link
Contributor Author

Copied from the websockets channel on discord, in case there was doubt that these changes were helping:

"I added some temporary logging to my fork to de-maybe-ify some of these changes. Changes in the SendLoop.Dispose() finally block and WebsocketServer.Send() are frequently being triggered, but not for every connection. From the first 14 closed connections, 4 connections were affected and 6 buffers were released that previously would have leaked."

image image image

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

Successfully merging this pull request may close these issues.

2 participants