-
-
Notifications
You must be signed in to change notification settings - Fork 56
fix: Don't overwrite MainThreadId
#2165
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
Changes from all commits
8bd0e05
35fc680
1735799
a230aef
e4c7699
3097916
b1809a6
3fae73b
5780727
5ac1ecc
7946207
570c83d
a491fea
13d8f43
9a35b06
9b983ba
0790872
36c4527
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
using System; | ||
using System.Threading; | ||
using UnityEngine; | ||
|
||
namespace Sentry.Unity; | ||
|
||
|
@@ -69,8 +68,16 @@ internal static class MainThreadData | |
|
||
public static DateTimeOffset? StartTime { get; set; } | ||
|
||
public static bool IsMainThread() | ||
=> MainThreadId.HasValue && Thread.CurrentThread.ManagedThreadId == MainThreadId; | ||
public static bool? IsMainThread() | ||
{ | ||
if (MainThreadId.HasValue) | ||
{ | ||
return MainThreadId.Equals(Thread.CurrentThread.ManagedThreadId); | ||
} | ||
|
||
// We don't know whether this is the main thread or not | ||
return null; | ||
} | ||
|
||
// For testing | ||
internal static ISentrySystemInfo? SentrySystemInfo { get; set; } | ||
|
@@ -79,7 +86,9 @@ public static void CollectData() | |
{ | ||
var sentrySystemInfo = SentrySystemInfo ?? SentrySystemInfoAdapter.Instance; | ||
|
||
MainThreadId = sentrySystemInfo.MainThreadId; | ||
// Don't overwrite the MainThreadId if it's already been set | ||
MainThreadId ??= sentrySystemInfo.MainThreadId; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This operation is not atomic. How about using |
||
|
||
ProcessorCount = sentrySystemInfo.ProcessorCount; | ||
OperatingSystem = sentrySystemInfo.OperatingSystem; | ||
CpuDescription = sentrySystemInfo.CpuDescription; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it’s an integer that should be set once and visible across threads, I think declaring it as volatile makes sense:
If you really need to make it nullable, then this should also work:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about
Interlocked
? (just learned about it from @Flash0ver yesterday 😁)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understood correctly, the problem could be in thread visibility. Atomic operations should also work, but probably unnecessary.