@@ -132,22 +132,21 @@ private async Task DisposeCore()
132
132
133
133
private async Task LoopAccept ( CancellationToken ct )
134
134
{
135
- try
135
+ while ( ! ct . IsCancellationRequested )
136
136
{
137
- while ( ! ct . IsCancellationRequested )
138
- {
139
- await Accept ( ct ) ;
140
- }
141
- }
142
- catch ( OperationCanceledException ex ) when ( ex . CancellationToken == ct )
143
- {
144
- // Ignore
137
+ await TryAccept ( ct ) ; /// this method doesn't throw, and in case of non-<see cref="OperationCanceledException"/> exceptions,
138
+ /// it will notify the <see cref="_newConnection"/> observer.
145
139
}
146
140
147
141
_newConnection . OnCompleted ( ) ;
148
142
}
149
143
150
- private async Task Accept ( CancellationToken ct )
144
+ /// <summary>
145
+ /// This method returns when a new connection is accepted, or when cancellation or another error occurs.
146
+ /// In case of cancellation or error, it will dispose of the underlying resources and will suppress the exception.
147
+ /// In case of an error (not a cancellation), it will notify the observer about the error.
148
+ /// </summary>
149
+ private async Task TryAccept ( CancellationToken ct )
151
150
{
152
151
var slot = _serverState . CreateConnectionSlot ( ) ;
153
152
@@ -159,11 +158,14 @@ private async Task Accept(CancellationToken ct)
159
158
var newConnection = await taskNewConnection ;
160
159
_newConnection . OnNext ( newConnection ) ;
161
160
}
161
+ catch ( OperationCanceledException ex ) when ( ex . CancellationToken == ct )
162
+ {
163
+ await slot . DisposeAsync ( ) ;
164
+ }
162
165
catch ( Exception ex )
163
166
{
164
167
await slot . DisposeAsync ( ) ;
165
168
_newConnection . OnError ( ex ) ;
166
- return ;
167
169
}
168
170
}
169
171
0 commit comments