Skip to content

Commit 9b6ba35

Browse files
authored
Merge pull request #873 from dotnet/fixKeyNotFoundException
Avoid throwing `KeyNotFoundException` from `MultiplexingStream.OnContentAsync`
2 parents d749ed2 + 44be259 commit 9b6ba35

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/Nerdbank.Streams/MultiplexingStream.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,11 @@ private enum TraceEventId
226226
/// A fatal exception occurred that took down one channel.
227227
/// </summary>
228228
ChannelFatalError,
229+
230+
/// <summary>
231+
/// Raised when we receive a <see cref="ControlCode.Content"/> message for an unknown or closed channel.
232+
/// </summary>
233+
UnexpectedContent,
229234
}
230235

231236
/// <summary>
@@ -962,11 +967,21 @@ private void OnContentWritingCompleted(QualifiedChannelId channelId)
962967

963968
private async ValueTask OnContentAsync(FrameHeader header, ReadOnlySequence<byte> payload, CancellationToken cancellationToken)
964969
{
965-
Channel channel;
970+
Channel? channel;
966971
QualifiedChannelId channelId = header.RequiredChannelId;
967972
lock (this.syncObject)
968973
{
969-
channel = this.openChannels[channelId];
974+
this.openChannels.TryGetValue(channelId, out channel);
975+
}
976+
977+
if (channel is null)
978+
{
979+
if (this.TraceSource.Switch.ShouldTrace(TraceEventType.Warning))
980+
{
981+
this.TraceSource.TraceEvent(TraceEventType.Warning, (int)TraceEventId.UnexpectedContent, "Ignoring " + nameof(ControlCode.Content) + " message for channel {0} that does not exist.", header.ChannelId);
982+
}
983+
984+
return;
970985
}
971986

972987
if (channelId.Source == ChannelSource.Local && !channel.IsAccepted)

0 commit comments

Comments
 (0)