Skip to content

Commit e1fa36e

Browse files
fix: Metrics: Exception when tracking Named Messages [MTT-5380] (#2426)
* MTT-5830 Fix NetworkMetrics were tracking named message received events but it is possible for message handlers to unregister themselves. We now cache the name of the message handler before invoking it so the Network Metrics can access the name even if its removed. --------- Co-authored-by: Noel Stephens <[email protected]>
1 parent e0bd7eb commit e1fa36e

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
3030

3131
### Fixed
3232

33+
- Fixed an issue where Named Message Handlers could remove themselves causing an exception when the metrics tried to access the name of the message.(#2426)
3334
- Fixed registry of public `NetworkVariable`s in derived `NetworkBehaviour`s (#2423)
3435
- Fixed issue where runtime association of `Animator` properties to `AnimationCurve`s would cause `NetworkAnimator` to attempt to update those changes. (#2416)
3536
- Fixed issue where `NetworkAnimator` would not check if its associated `Animator` was valid during serialization and would spam exceptions in the editor console. (#2416)

com.unity.netcode.gameobjects/Runtime/Messaging/CustomMessageManager.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,18 @@ internal void InvokeNamedMessage(ulong hash, ulong sender, FastBufferReader read
151151
// We dont know what size to use. Try every (more collision prone)
152152
if (m_NamedMessageHandlers32.TryGetValue(hash, out HandleNamedMessageDelegate messageHandler32))
153153
{
154+
// handler can remove itself, cache the name for metrics
155+
string messageName = m_MessageHandlerNameLookup32[hash];
154156
messageHandler32(sender, reader);
155-
m_NetworkManager.NetworkMetrics.TrackNamedMessageReceived(sender, m_MessageHandlerNameLookup32[hash], bytesCount);
157+
m_NetworkManager.NetworkMetrics.TrackNamedMessageReceived(sender, messageName, bytesCount);
156158
}
157159

158160
if (m_NamedMessageHandlers64.TryGetValue(hash, out HandleNamedMessageDelegate messageHandler64))
159161
{
162+
// handler can remove itself, cache the name for metrics
163+
string messageName = m_MessageHandlerNameLookup64[hash];
160164
messageHandler64(sender, reader);
161-
m_NetworkManager.NetworkMetrics.TrackNamedMessageReceived(sender, m_MessageHandlerNameLookup64[hash], bytesCount);
165+
m_NetworkManager.NetworkMetrics.TrackNamedMessageReceived(sender, messageName, bytesCount);
162166
}
163167
}
164168
else
@@ -169,15 +173,19 @@ internal void InvokeNamedMessage(ulong hash, ulong sender, FastBufferReader read
169173
case HashSize.VarIntFourBytes:
170174
if (m_NamedMessageHandlers32.TryGetValue(hash, out HandleNamedMessageDelegate messageHandler32))
171175
{
176+
// handler can remove itself, cache the name for metrics
177+
string messageName = m_MessageHandlerNameLookup32[hash];
172178
messageHandler32(sender, reader);
173-
m_NetworkManager.NetworkMetrics.TrackNamedMessageReceived(sender, m_MessageHandlerNameLookup32[hash], bytesCount);
179+
m_NetworkManager.NetworkMetrics.TrackNamedMessageReceived(sender, messageName, bytesCount);
174180
}
175181
break;
176182
case HashSize.VarIntEightBytes:
177183
if (m_NamedMessageHandlers64.TryGetValue(hash, out HandleNamedMessageDelegate messageHandler64))
178184
{
185+
// handler can remove itself, cache the name for metrics
186+
string messageName = m_MessageHandlerNameLookup64[hash];
179187
messageHandler64(sender, reader);
180-
m_NetworkManager.NetworkMetrics.TrackNamedMessageReceived(sender, m_MessageHandlerNameLookup64[hash], bytesCount);
188+
m_NetworkManager.NetworkMetrics.TrackNamedMessageReceived(sender, messageName, bytesCount);
181189
}
182190
break;
183191
}

0 commit comments

Comments
 (0)