Skip to content

Commit 84dc329

Browse files
committed
Validate token
1 parent 0a31b0c commit 84dc329

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

projects/RabbitMQ.Client/client/impl/AsyncManualResetEvent.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
//---------------------------------------------------------------------------
3131

3232
using System;
33+
using System.Diagnostics.CodeAnalysis;
3334
using System.Threading;
3435
using System.Threading.Tasks;
3536
using System.Threading.Tasks.Sources;
@@ -117,13 +118,38 @@ public void Reset()
117118
_valueTaskSource.Reset();
118119
}
119120

120-
void IValueTaskSource.GetResult(short token) =>
121+
void IValueTaskSource.GetResult(short token)
122+
{
123+
if (token != _valueTaskSource.Version)
124+
{
125+
ThrowIncorrectTokenException();
126+
}
127+
121128
_valueTaskSource.GetResult(token);
129+
}
130+
131+
ValueTaskSourceStatus IValueTaskSource.GetStatus(short token)
132+
{
133+
if (token != _valueTaskSource.Version)
134+
{
135+
ThrowIncorrectTokenException();
136+
}
137+
138+
return _valueTaskSource.GetStatus(token);
139+
}
122140

123-
ValueTaskSourceStatus IValueTaskSource.GetStatus(short token) =>
124-
_valueTaskSource.GetStatus(token);
141+
void IValueTaskSource.OnCompleted(Action<object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags)
142+
{
143+
if (token != _valueTaskSource.Version)
144+
{
145+
ThrowIncorrectTokenException();
146+
}
125147

126-
void IValueTaskSource.OnCompleted(Action<object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags) =>
127148
_valueTaskSource.OnCompleted(continuation, state, token, flags);
149+
}
150+
151+
[DoesNotReturn]
152+
static void ThrowIncorrectTokenException() =>
153+
throw new InvalidOperationException("ValueTask cannot be awaited multiple times.");
128154
}
129155
}

0 commit comments

Comments
 (0)