@@ -80,6 +80,7 @@ public class Subscription: IEnumerable, IEnumerator, IDisposable {
80
80
protected bool m_noAck ;
81
81
82
82
protected readonly object m_consumerLock = new object ( ) ;
83
+ protected readonly object m_eventLock = new object ( ) ;
83
84
protected volatile QueueingBasicConsumer m_consumer ;
84
85
protected string m_consumerTag ;
85
86
@@ -169,8 +170,11 @@ public void Close()
169
170
///null.</summary>
170
171
public void Ack ( )
171
172
{
172
- if ( m_latestEvent != null ) {
173
- Ack ( m_latestEvent ) ;
173
+ lock ( m_eventLock )
174
+ {
175
+ if ( m_latestEvent != null ) {
176
+ Ack ( m_latestEvent ) ;
177
+ }
174
178
}
175
179
}
176
180
@@ -185,16 +189,19 @@ public void Ack()
185
189
///</remarks>
186
190
public void Ack ( BasicDeliverEventArgs evt )
187
191
{
188
- if ( evt == null ) {
189
- return ;
190
- }
192
+ lock ( m_eventLock )
193
+ {
194
+ if ( evt == null ) {
195
+ return ;
196
+ }
191
197
192
- if ( ! m_noAck ) {
193
- m_model . BasicAck ( evt . DeliveryTag , false ) ;
194
- }
198
+ if ( ! m_noAck ) {
199
+ m_model . BasicAck ( evt . DeliveryTag , false ) ;
200
+ }
195
201
196
- if ( evt == m_latestEvent ) {
197
- m_latestEvent = null ;
202
+ if ( evt == m_latestEvent ) {
203
+ m_latestEvent = null ;
204
+ }
198
205
}
199
206
}
200
207
@@ -203,8 +210,11 @@ public void Ack(BasicDeliverEventArgs evt)
203
210
///null.</summary>
204
211
public void Nack ( bool requeue )
205
212
{
206
- if ( m_latestEvent != null ) {
207
- Nack ( m_latestEvent , false , requeue ) ;
213
+ lock ( m_eventLock )
214
+ {
215
+ if ( m_latestEvent != null ) {
216
+ Nack ( m_latestEvent , false , requeue ) ;
217
+ }
208
218
}
209
219
}
210
220
@@ -214,8 +224,11 @@ public void Nack(bool requeue)
214
224
///null.</summary>
215
225
public void Nack ( bool multiple , bool requeue )
216
226
{
217
- if ( m_latestEvent != null ) {
218
- Nack ( m_latestEvent , multiple , requeue ) ;
227
+ lock ( m_eventLock )
228
+ {
229
+ if ( m_latestEvent != null ) {
230
+ Nack ( m_latestEvent , multiple , requeue ) ;
231
+ }
219
232
}
220
233
}
221
234
@@ -232,16 +245,19 @@ public void Nack(BasicDeliverEventArgs evt,
232
245
bool multiple ,
233
246
bool requeue )
234
247
{
235
- if ( evt == null ) {
236
- return ;
237
- }
248
+ lock ( m_eventLock )
249
+ {
250
+ if ( evt == null ) {
251
+ return ;
252
+ }
238
253
239
- if ( ! m_noAck ) {
240
- m_model . BasicNack ( evt . DeliveryTag , multiple , requeue ) ;
241
- }
254
+ if ( ! m_noAck ) {
255
+ m_model . BasicNack ( evt . DeliveryTag , multiple , requeue ) ;
256
+ }
242
257
243
- if ( evt == m_latestEvent ) {
244
- m_latestEvent = null ;
258
+ if ( evt == m_latestEvent ) {
259
+ m_latestEvent = null ;
260
+ }
245
261
}
246
262
}
247
263
@@ -265,21 +281,24 @@ public void Nack(BasicDeliverEventArgs evt,
265
281
///</remarks>
266
282
public BasicDeliverEventArgs Next ( )
267
283
{
268
- try {
269
- // Alias the pointer as otherwise it may change out
270
- // from under us by the operation of Close() from
271
- // another thread.
272
- QueueingBasicConsumer consumer = m_consumer ;
273
- if ( consumer == null || m_model . IsClosed ) {
274
- // Closed!
284
+ lock ( m_eventLock )
285
+ {
286
+ try {
287
+ // Alias the pointer as otherwise it may change out
288
+ // from under us by the operation of Close() from
289
+ // another thread.
290
+ QueueingBasicConsumer consumer = m_consumer ;
291
+ if ( consumer == null || m_model . IsClosed ) {
292
+ // Closed!
293
+ m_latestEvent = null ;
294
+ } else {
295
+ m_latestEvent = ( BasicDeliverEventArgs ) consumer . Queue . Dequeue ( ) ;
296
+ }
297
+ } catch ( EndOfStreamException ) {
275
298
m_latestEvent = null ;
276
- } else {
277
- m_latestEvent = ( BasicDeliverEventArgs ) consumer . Queue . Dequeue ( ) ;
278
299
}
279
- } catch ( EndOfStreamException ) {
280
- m_latestEvent = null ;
300
+ return m_latestEvent ;
281
301
}
282
- return m_latestEvent ;
283
302
}
284
303
285
304
///<summary>Retrieves the next incoming delivery in our
@@ -328,29 +347,32 @@ public BasicDeliverEventArgs Next()
328
347
///</remarks>
329
348
public bool Next ( int millisecondsTimeout , out BasicDeliverEventArgs result )
330
349
{
331
- try {
332
- // Alias the pointer as otherwise it may change out
333
- // from under us by the operation of Close() from
334
- // another thread.
335
- QueueingBasicConsumer consumer = m_consumer ;
336
- if ( consumer == null || m_model . IsClosed ) {
337
- // Closed!
338
- m_latestEvent = null ;
339
- result = null ;
340
- return false ;
341
- } else {
342
- BasicDeliverEventArgs qValue ;
343
- if ( ! consumer . Queue . Dequeue ( millisecondsTimeout , out qValue ) ) {
350
+ lock ( m_eventLock )
351
+ {
352
+ try {
353
+ // Alias the pointer as otherwise it may change out
354
+ // from under us by the operation of Close() from
355
+ // another thread.
356
+ QueueingBasicConsumer consumer = m_consumer ;
357
+ if ( consumer == null || m_model . IsClosed ) {
358
+ // Closed!
359
+ m_latestEvent = null ;
344
360
result = null ;
345
361
return false ;
362
+ } else {
363
+ BasicDeliverEventArgs qValue ;
364
+ if ( ! consumer . Queue . Dequeue ( millisecondsTimeout , out qValue ) ) {
365
+ result = null ;
366
+ return false ;
367
+ }
368
+ m_latestEvent = qValue ;
346
369
}
347
- m_latestEvent = qValue ;
370
+ } catch ( EndOfStreamException ) {
371
+ m_latestEvent = null ;
348
372
}
349
- } catch ( EndOfStreamException ) {
350
- m_latestEvent = null ;
373
+ result = m_latestEvent ;
374
+ return true ;
351
375
}
352
- result = m_latestEvent ;
353
- return true ;
354
376
}
355
377
356
378
///<summary>Implementation of the IEnumerable interface, for
@@ -376,10 +398,14 @@ IEnumerator IEnumerable.GetEnumerator()
376
398
///</remarks>
377
399
object IEnumerator . Current {
378
400
get {
379
- if ( m_latestEvent == null ) {
380
- throw new InvalidOperationException ( ) ;
401
+ lock ( m_eventLock )
402
+ {
403
+
404
+ if ( m_latestEvent == null ) {
405
+ throw new InvalidOperationException ( ) ;
406
+ }
407
+ return m_latestEvent ;
381
408
}
382
- return m_latestEvent ;
383
409
}
384
410
}
385
411
@@ -394,7 +420,10 @@ object IEnumerator.Current {
394
420
///</remarks>
395
421
bool IEnumerator . MoveNext ( )
396
422
{
397
- return Next ( ) != null ;
423
+ lock ( m_eventLock )
424
+ {
425
+ return Next ( ) != null ;
426
+ }
398
427
}
399
428
400
429
///<summary>Dummy implementation of the IEnumerator interface,
0 commit comments