Skip to content

Commit 7cd57e3

Browse files
authored
Merge pull request #728 from bugsnag/removeDateTimeNowUsage
Backport Release v6.3.4
2 parents 83a3f77 + 74d5830 commit 7cd57e3

File tree

8 files changed

+80
-17
lines changed

8 files changed

+80
-17
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
4+
## 6.3.4 (2023-07-06)
5+
6+
## Bug Fixes
7+
8+
- Remove unnecessary usages of DateTimeOffset.Now to prevent iOS app hangs resulting from Unity native code accessing non-thread safe methods. [#725](https://github.com/bugsnag/bugsnag-unity/pull/725)
9+
10+
311
## 6.3.3 (2023-06-13)
412

513
## Bug Fixes

build.cake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var target = Argument("target", "Default");
55
var solution = File("./BugsnagUnity.sln");
66
var configuration = Argument("configuration", "Release");
77
var project = File("./src/BugsnagUnity/BugsnagUnity.csproj");
8-
var version = "6.3.3";
8+
var version = "6.3.4";
99

1010
Task("Restore-NuGet-Packages")
1111
.Does(() => NuGetRestore(solution));

src/BugsnagUnity/MaximumLogTypeCounter.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,24 @@ public class MaximumLogTypeCounter
1010

1111
private Dictionary<LogType, int> CurrentCounts { get; }
1212

13-
private DateTimeOffset FlushAt { get; set; }
13+
private double FlushAt { get; set; }
1414

15-
private TimeSpan MaximumLogsTimePeriod => Configuration.MaximumLogsTimePeriod;
15+
private double MaximumLogsTimePeriod => Configuration.MaximumLogsTimePeriod.TotalSeconds;
1616

1717
private Dictionary<LogType, int> MaximumTypePerTimePeriod => Configuration.MaximumTypePerTimePeriod;
1818

1919
private readonly object _lock = new object();
2020

21+
private void SetFlushTime()
22+
{
23+
FlushAt = Time.ElapsedSeconds + MaximumLogsTimePeriod;
24+
}
25+
2126
public MaximumLogTypeCounter(Configuration configuration)
2227
{
2328
Configuration = configuration;
2429
CurrentCounts = new Dictionary<LogType, int>();
25-
FlushAt = DateTimeOffset.Now.Add(Configuration.MaximumLogsTimePeriod);
30+
SetFlushTime();
2631
}
2732

2833
public bool ShouldSend(UnityLogMessage unityLogMessage)
@@ -47,7 +52,7 @@ public bool ShouldSend(UnityLogMessage unityLogMessage)
4752
if (unityLogMessage.CreatedAt > FlushAt)
4853
{
4954
CurrentCounts.Clear();
50-
FlushAt = DateTimeOffset.Now.Add(MaximumLogsTimePeriod);
55+
SetFlushTime();
5156
return true;
5257
}
5358
return false;

src/BugsnagUnity/Payload/AppWithState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ internal AppWithState(Dictionary<string, object> cachedData) : base(cachedData){
5656

5757
internal AppWithState(Configuration configuration) : base(configuration)
5858
{
59-
Duration = TimeSpan.FromSeconds(Time.realtimeSinceStartup);
59+
Duration = TimeSpan.FromSeconds(UnityEngine.Time.realtimeSinceStartup);
6060
}
6161

6262
}

src/BugsnagUnity/Time.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Diagnostics;
3+
4+
namespace BugsnagUnity
5+
{
6+
// A simple timer used internally to avoid thread complications when using the unity Time API
7+
internal class Time
8+
{
9+
10+
private static object _swLock = new object();
11+
12+
private static Stopwatch _sw;
13+
14+
internal static Stopwatch Timer
15+
{
16+
get
17+
{
18+
if (_sw == null)
19+
{
20+
lock (_swLock)
21+
{
22+
if (_sw == null)
23+
{
24+
_sw = Stopwatch.StartNew();
25+
}
26+
}
27+
}
28+
return _sw;
29+
}
30+
}
31+
32+
internal static double ElapsedSeconds => Timer.Elapsed.TotalSeconds;
33+
34+
}
35+
}
36+

src/BugsnagUnity/UniqueLogThrottle.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,25 @@ public class UniqueLogThrottle
1818
/// <summary>
1919
/// Used to track when the counter should be flushed next
2020
/// </summary>
21-
private DateTime FlushAt { get; set; }
21+
private double FlushAt { get; set; }
2222

2323
/// <summary>
2424
/// The configuration for unique log counts and times
2525
/// </summary>
2626
private Configuration Configuration { get; }
2727

28-
private TimeSpan UniqueLogsTimePeriod => Configuration.SecondsPerUniqueLog;
28+
private double UniqueLogsTimePeriod => Configuration.SecondsPerUniqueLog.TotalSeconds;
29+
30+
private void SetFlushTime()
31+
{
32+
FlushAt = Time.ElapsedSeconds + UniqueLogsTimePeriod;
33+
}
2934

3035
public UniqueLogThrottle(Configuration configuration)
3136
{
3237
Configuration = configuration;
3338
Counter = new Dictionary<UnityLogMessage, int>(new UnityLogMessageEqualityComparer());
34-
FlushAt = DateTime.UtcNow.Add(Configuration.SecondsPerUniqueLog);
39+
SetFlushTime();
3540
}
3641

3742
/// <summary>
@@ -43,7 +48,6 @@ public UniqueLogThrottle(Configuration configuration)
4348
public bool ShouldSend(UnityLogMessage unityLogMessage)
4449
{
4550
bool shouldSend;
46-
4751
lock (_lock)
4852
{
4953
shouldSend = !Counter.ContainsKey(unityLogMessage);
@@ -57,7 +61,7 @@ public bool ShouldSend(UnityLogMessage unityLogMessage)
5761
if (unityLogMessage.CreatedAt > FlushAt)
5862
{
5963
Counter.Clear();
60-
FlushAt = DateTime.UtcNow.Add(UniqueLogsTimePeriod);
64+
SetFlushTime();
6165
shouldSend = true;
6266
}
6367
}

src/BugsnagUnity/UnityLogMessage.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,29 @@ public class UnityLogMessage
1010
{
1111
public UnityLogMessage(string condition, string stackTrace, LogType type)
1212
{
13-
CreatedAt = DateTime.UtcNow;
13+
CreatedAt = Time.ElapsedSeconds;
1414
Condition = condition;
1515
StackTrace = stackTrace;
1616
Type = type;
1717
}
1818

19+
20+
public UnityLogMessage(Exception exception)
21+
{
22+
CreatedAt = Time.ElapsedSeconds;
23+
Condition = exception.Message == null ? string.Empty : exception.Message;
24+
StackTrace = exception.StackTrace == null ? string.Empty : exception.StackTrace;
25+
Type = LogType.Exception;
26+
}
27+
1928
public string Condition { get; }
2029

2130
public string StackTrace { get; }
2231

2332
public LogType Type { get; }
2433

25-
public DateTime CreatedAt { get; }
34+
public double CreatedAt { get; }
35+
36+
2637
}
2738
}

tests/UnityEngine/Time.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
using System;
2-
namespace UnityEngine
1+
namespace UnityEngine
32
{
43
public class Time
54
{
65

76
public static float realtimeSinceStartup { get; set; }
8-
7+
98
}
10-
}
9+
}

0 commit comments

Comments
 (0)