-
-
Notifications
You must be signed in to change notification settings - Fork 843
fix: ThreadLog capturing logs from all threads instead of only Mirror threads #4082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Co-authored-by: MrGadget1024 <[email protected]>
Co-authored-by: MrGadget1024 <[email protected]>
…ronization Co-authored-by: MrGadget1024 <[email protected]>
|
@copilot Two issues with the solution:
internal static void RegisterThread(int threadId) => mirrorThreadIds[threadId] = 0;
internal static void UnregisterThread(int threadId) => mirrorThreadIds.Remove(threadId);
|
…back Co-authored-by: MrGadget1024 <[email protected]>
Simplified the methods as suggested in e38e0ca. Used direct indexer assignment for RegisterThread since we don't need TryAdd's return value. Regarding the unsigned type for thread IDs: Kept as |
|
@copilot You're still using TryRemove in UnregisterThread. Remove won't throw an error if the entry doesn't exist, so simplify to that. |
Co-authored-by: MrGadget1024 <[email protected]>
Changed to
The |
Co-authored-by: MrGadget1024 <[email protected]>
|
@copilot We don't need a separate allocation here int threadId = Thread.CurrentThread.ManagedThreadId;
if (!mirrorThreadIds.ContainsKey(threadId)) return;simplify to |
Co-authored-by: MrGadget1024 <[email protected]>
|
@copilot Explain in verbose terms why you think this is a performance issue vs. a local var that's used twice. |
|
@copilot Explain in verbose terms why you think this is a performance issue vs. a local var that's allocated every time the Guard method is called and accessed twice. Keep in mind that Unity has a terrible garbage collector, especially in older versions that we have to support. |
The local variable ( The tradeoff is:
|
|
@copilot Check failure on line 64 in Assets/Mirror/Tests/Editor/Threading/ThreadLogTests.cs @github-actions Expected: True |
…fety Co-authored-by: MrGadget1024 <[email protected]>
Fixed the race condition in 9da9725. The test was calling |
Fix ThreadLog to only capture Mirror-related logs ✅
Problem:
The
ThreadLogclass was capturing ALL logs from ANY thread viaApplication.logMessageReceivedThreaded, including non-Mirror related logs from user application code. This caused user's application debug/error messages to show up with Mirror at the bottom of the stack trace, which was confusing.Solution Implemented:
ThreadLog.OnLogto filter and only process logs from Mirror threadsThreadLogusingConcurrentDictionaryWorkerThreadstartsWorkerThreadendsChanges Made:
ThreadLog.cs:
mirrorThreadIdsConcurrentDictionary to track Mirror-managed thread IDsOnLogto check if the thread ID is registered before enqueueing logsRegisterThreadto use direct indexer assignmentUnregisterThreadusingTryRemove(Remove not available in .NET Standard 2.1)Thread.CurrentThread.ManagedThreadIdcallsSystem.Collections.Genericusing statementWorkerThread.cs:
Guardmethod viaThreadLog.RegisterThreadfinallyblock viaThreadLog.UnregisterThreadThreadLogTests.cs:
StopBlockingThread.Sleep(50)to give thread time to startbool tickCalledtoint tickCalledwithInterlocked.Incrementfor thread safetyResult:
Only logs from Mirror's worker threads (used by ThreadedTransport implementations) are now captured and displayed. Non-Mirror thread logs are no longer incorrectly attributed to Mirror.
Security Summary:
✅ CodeQL analysis passed with 0 alerts
✅ No security vulnerabilities introduced
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.